Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: cleanup a cancelled CGI script

by saskaqueer (Friar)
on Jun 08, 2004 at 19:32 UTC ( #362515=note: print w/replies, xml ) Need Help??

in reply to cleanup a cancelled CGI script

I tried the solution of using SIGPIPE posted by others above on Windows XP just to try it. Of course, there is no SIGPIPE sent to the script when running under Win32 (well, WinXP anyhow). What happened in my test is that the script doesn't even realize that the client has disconnected. The script finishes running normally, with no change in execution (in a simple test anyhow). Even adding a or die("print failed: $!"); to a print statement that should fail doesn't change anything. My test script below:

#!perl -w $| = 1; BEGIN { $SIG{PIPE} = sub { die "Pipe error: @_\n" }; } use strict; use CGI; my $q = CGI->new(); print $q->header(); print "output #1\n"; sleep 15; # we stop the browser load during the 15 sec. sleep print "output #2\n" or die("print failed: $!"); END { open my $fh, '>>', 'debug.txt'; print $fh "We made it to the END block.\n"; close $fh; }

Replies are listed 'Best First'.
Re^2: cleanup a cancelled CGI script
by zebedee (Pilgrim) on Jun 08, 2004 at 19:58 UTC
    I think (on Windows, anyway) that you'd have to have a sweeper (reaper?) process that comes along every hour or so looking for ZIP files that are older than, say, one hour, and assume that they are incomplete, and delete them. Or you could add them to a database, or send them to another process, whose job it is delete any ZIPs in its list (or database) that are older than x hours.
      Why not just do it in the script when it runs?
      my $tmp_dir = "/path/to/tmpdir"; opendir(TMPDIR,$tmp_dir) || die $!; -M "$tmp_dir/$_" > 1/24 and unlink "$tmp_dir/$_" for (grep /zip/, read +dir(TMPDIR)); closedir(TMPDIR);
      or similar (untested).

      cLive ;-)

        That's going to result in some concurrency problems.
      ++ That's what I would've recommeneded. (Or, similarly, use one of the Win32::Process and on startup, put pid into database, then when pid dies, have a second process kill the pid related to the .zip file...)

      Zak - the office
Re^2: cleanup a cancelled CGI script
by paulbort (Hermit) on Jun 08, 2004 at 20:02 UTC
    You don't specify what web server is running your script. I suspect that Apache will do the same thing on Win32 as *nix, if possible, but that IIS will do its own thing, which probably isn't SIGPIPE. To address the OP's question: if the only concern from these cancelled scripts is stray .ZIP files that need to be deleted, there's a non-Perl solution that you might find useful. You could give the .ZIP files their own temp directory to live in, and use cron to run tmpwatch on that directory. Files that have sat around too long are cleaned up automatically.

    Spring: Forces, Coiled Again!
Re^2: cleanup a cancelled CGI script
by iburrell (Chaplain) on Jun 08, 2004 at 20:28 UTC
    $SIG{PIPE} won't work on Windows. Windows does not implement signals, pipes, and sockets in the same way. The way CGI scripts communicate with the server is different and does not result in a SIGPIPE when the communications is disconnected.

    I don't see the problem if the script finishes normally even if the client disconnects. Can't it delete the temporary file in its normal cleanup? Special code is only needed when the disconnect causes the CGI scripts to be killed before it is done and not run the END code.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://362515]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2021-09-22 09:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found