[Melbourne-pm] An intermittent problem with open for append

Toby Corkindale tjc at wintrmute.net
Tue May 27 23:22:35 PDT 2008


On Wed, May 28, 2008 at 05:54:09AM +0000, Tim Hogard wrote:
> 
> Hi,
> 
> I've got a CGI program that has a problem every once in a while.
> 
> The problem code looks like:
> 
> open OUT,">>/home/foo/que/$ip" || push @error, "Cant save details";
> print OUT "$ip:t:date=",scalar localtime,"\n";
> ... then it prints to OUT all the rest of ${ENV} and CGI vars.
> 
> Sometimes apache will record 2 hits on the page (a double click?)
> and most of the time I get two sets of all the data however sometimes
> while running perl 5.8.8 I only get the first or second sometimes.
> This never happens with perl 5.005_02.
> 
> Can anyone explain why perl 5.005 works yet 5.8.8 doesn't?
> I was under the impresson that the ">>" means tell the OS to
> open in append mode, any data written should go in the file
> and not just end up lost.

I can't explain why it works on 5.005 and not 5.8.8, but since you have
mentioned it is a very rare occurence, it is possible that it /would/ occur on
5.005 eventually. Maybe the code just runs slower or faster and flukily avoids
a race condition as a result?

Also, it's worth noting that append isn't always safe for use by multiple
processes - it works by seeking to the end of the file before writing, but
according to the man page, this doesn't work reliably on networked file systems
like NFS.

Also - I think Apache will send a signal to the CGIs running, to kill them if
the connection dies - is it is simply a case that when someone double-clicked,
one of the cgi instances was killed before it could write to the logfile?

cheers,
Toby


More information about the Melbourne-pm mailing list