I'm getting a race condition in some Linux::Inotify2 code. Where I'm watching a directory for IN_CREATE events to process the created files. The following works perfectly 99% of the time.

use strict; use warnings; use XML::LibXML; use Linux::Inotify2; my $drop_dir = "/foo/bar"; my $inotify = Linux::Inotify2->new or die "unable to create new inotify object: $!"; $inotify->watch($drop_dir, IN_CREATE, sub { my $e = shift; my $name = $e->fullname; process_drop_file($name); }); 1 while $inotify->poll; sub process_drop_file { my $file = Path::Class::File->new(shift); -f $file or croak "'$file' is not a file"; my $doc = XML::LibXML->new->parse_file("$file"); # Do stuff with it... $file->remove or croak "Couldn't remove '$file'"; }

But then bam-

/foo/bar/test.xml:1: parser error : Start tag expected, '<' not found

The file is there and in tact by the time I can manually check of course but XML::LibXML tried to read it too early. This means it passed the -f but had no content, I think.

Among several other dead-ends, like playing with the cookie and watch args, I tried this-

$inotify->watch($drop_dir, IN_CREATE, sub { my $e = shift; my $name = $e->fullname; $inotify->watch($name, IN_CLOSE, sub { process_drop_file($name); }); });

-in a misguided attempt to move the event check to the file, but of course this could only succeed in the 1 in 100 case where the race condition hits.

What am I missing or doing wrong? Thank you!


In reply to Race condition in Linux::Inotify2 between dir and new file? by Your Mother

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.