chinu_regal has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I need all of your sincere help to solve the issue. Problem: In a multi user environment if the cgi script is writing to a file with appropriate lock in place and in between if the user who has initiated the process has closed the browser before the writing process is complete, then how to ensure to close the the opened files as soon as the browser is closed allowing other users to do some operations on the same file. Thanks in advance, Regards, Chinmaya
  • Comment on How to ensure to close the opened files in cgi if browser is closed

Replies are listed 'Best First'.
Re: How to ensure to close the opened files in cgi if browser is closed
by almut (Canon) on May 31, 2010 at 13:37 UTC

    The webserver should normally terminate the CGI script in case the browser prematurely closes the connection.  So, you could try putting your cleanup code in an END { ... } block, which should execute upon termination of the CGI script.

    In case that doesn't work, you'll probably also have to set up a signal handler (most likely for SIGTERM, but I haven't checked1) calling die or exit, which in turn will make the script terminate in a controlled fashion, so the END block executes.  (Details depend on the webserver, OS, etc.)

    ___

    1 in a private msg Perlbotics noted (thanks!) that according to his observations, it would rather be SIGPIPE — which makes perfect sense, as the script would be trying to output to a broken pipe (presuming it hasn't already finished writing at the time...).