Creepy perl stat functions on Windows

I get realy strange behavior of POE::Wheel::FollowTail on Windows.
I need tailing some files on Windows and POE::Wheel::FollowTail is great for that.
It works fine, but if file reduced or recreated my script hanged to infinity loop.
I could not find anything about this problem in web and here is it.

The reason in how perl built-in stat function worked on Windows


my $filename = $ARGV[0];
open my $filehandle, "<", $filename;
say "handle: ". join ' ', stat $filehandle;
say " name: ". join ' ', stat $filename;


c:\>perl anyfile.txt
handle: 0 0 33206 1 0 0 0 9 1369992444 1369992444 1369992444
name: 2 0 33206 1 0 0 2 9 1369992444 1369992444 1369992444

I have test it with various version of ActivePerl and Strawberry perl on Windows XP/7, result always same.
Stat function on Windows show different result with filehande and filename in dev and rdev fields (result same on linux!)
(0 for file filehandle and positive number(2 in example) for file by name)

POE::Wheel::FollowTail use stat (with filehandle and filename) to check if file modified or not.
Because result always different POE::Wheel::FollowTail think that file always new and read it again and again.

And workaround is:

*CORE::GLOBAL::stat = sub {
my @s = CORE::stat($_[0]);
return (1,@s[1..5],1,@s[7..$#s]);

1 Comment

Hi! This is the author of POE::Wheel::FollowTail.

Your detailed explanation of the problem has allowed me to fix my module. At least, I think I fixed it. There are tests, and they pass, but they also passed before the change.

To be sure it's fixed, I'd appreciate if you could provide a proper test case or test the current version at github. If possible. I'd like to iron out any other problems before I upload the next CPAN release.

The fix at Github.

In the future, please let me know directly of bugs you find in or related to my code. I'd rather fix bugs, but someone has to report them first. I wouldn't have known about this problem if someone hadn't sent me a link to your post.

That's not to say don't blog about them. You've found a really insidious bug in Windows' stat() behavior. The world also needs to know.

Thanks again!

Leave a comment

About pru-mike

user-pic I blog about Perl.