On 29 Jun 2000, Frank D. Cringle wrote:

> Vivek Khera <[EMAIL PROTECTED]> writes:
> > >>>>> "RE" == Rob Egan <[EMAIL PROTECTED]> writes:
> > 
> > RE> CGI scripts to behave in mod_perl. All it does is capture e-mail addresses,
> > RE> and place them in a text file so we can gather them up later and drop them
> > RE> into a database. If you run the script as a regular CGI (without mod_perl
> > RE> enabled) it works great. But on the mod_perl enabled server, the script
> > RE> throws garbage into the file and overwrites previous entries if a user
> > RE> submits more than one e-mail address in a single session (i.e. they enter
> > 
> > It has nothing to do with the person quitting the browser.  It has to
> > do with your script using global variables.  Turn on "use strict" and
> > perl warnings and see what your error log says.
> > 
> > Then go to the mod perl guide and search for "sticky variables".
> 
> But since they are all global (no use of my), they won't be sticky.
> 
> There is plenty wrong with the script, although I can't specifically
> correlate the symptoms with the mistakes.  Sometimes the file is
> referred to as $results_file and in other places by its literal name.
> The 'if (-e $results_file) open ">..." else open ">>..."' code is
> unnecessary and will break if 2 processes hit the first if
> concurrently.  Just open it in append mode.  If it isn't there it will
> be created.  If you don't have write permission the open will fail.
> The locking is classically broken "# Unlock before you close" No!
> Wrong!.  Just close the file.  That has the desired side-effects of
> first flushing the buffered data and then unlocking the file.

When using file locking one must make sure, that if the script has been
stopped before the close() was called, to use an END block under Registry
or $r->register_cleanup() anywhere. See:
http://perl.apache.org/guide/debug.html#Handling_the_User_pressed_Stop_
http://perl.apache.org/guide/debug.html#Cleanup_Code

Otherwise you might get stuck with a stale lock, which will be never
removed unless the same process will call open() on the same filehandler.
And it'd be the same filehandler only if you use "open, IN ..." style, if
you use Symbol module or perl5.6 filehandler autovivification a new unique
filehandler will be created when the same code will run again.

So close() under mod_perl is not enough when locking is used.

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://perl.org     http://stason.org/TULARC
http://singlesheaven.com http://perlmonth.com http://sourcegarden.org


Reply via email to