http://www.perlmonks.org?node_id=219620

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

I have a script that runs perfectly for a long while and then dies mysteriously. I've included a lot of debugging print statements, but they haven't been able to isolate the problem. The script runs in a batch file (from a DOS command prompt) that restarts the script when it ends, and most of the time the batch file restarts the script, but sometimes the entire DOS box disappears. There's nothing pertinent in Window's event log. If there is an error on the screen, it's gone by the time I get to it. It runs between 6 and 30 hours between crashes, so I can't just sit and watch it.

What more can I do (besides putting a print statement after every line) to try to isolate the problem?

john

  • Comment on Mysterious script crash in win2k and ActivePerl

Replies are listed 'Best First'.
Re: Mysterious script crash in win2k and ActivePerl
by Mr. Muskrat (Canon) on Dec 13, 2002 at 16:31 UTC

    Try sending all errors to a file instead of the console.

    Something like should do the trick:

    BEGIN { $|++; open(STDERR, ">>", "mysterious_errors.txt"); }

    The >> is so that you append new errors to the file instead of overwriting it each time your program is run.

    Also add error checking with something like:
    do_something_with(@my_args) or die("Couldn't so_something_with ", $!);

      I'm not redirecting STDERR yet. I'll add that, but I think it's a Windows level error, so I doubt I'll see anything there. Still, it's worth a try.
Re: Mysterious script crash in win2k and ActivePerl
by ibanix (Hermit) on Dec 13, 2002 at 16:28 UTC
    Hi john,

    For starters, do you have strict and warnings enabled? You should always be using those.

    If you could post some code (always the best thing to do!) then we might be able to help more. Also, why not output your debug statements to a file, so you can read what happens just before it crashes?

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
      I'll check on strict and warnings -- I think they're on. The code is complicated, but I posted the main loop at http://www.perlmonks.org/index.pl?node_id=217848

      I did find one bug and corrected it this way:

      while($buf=~/(.*\n)/g) { print DEBUG "doing <$1>\n"; ${$Con{$fh}}{getline}($fh,$1); last unless $Con{$fh}; # it's possible that the connection can b +e deleted while there's still something in the buffer } if($Con{$fh}) { ($this->{_})=$buf=~/([^\n\r]*)$/s; if(length($this->{_}) > 10000) { ${$Con{$fh}}{getline}($fh,$this->{_}); ($this->{_})=''; } }
      The problem was that the connection was closed while there was still a line in the buffer. The if($Con{$fh}) tests for that condition.

      john

Re: Mysterious script crash in win2k and ActivePerl
by gmpassos (Priest) on Dec 14, 2002 at 00:38 UTC
    Well, very mysterious!

    First, what is your Perl version?

    If you are usign Perl 5.8.0, disable the signal ALARM:
     $SIG{ALARM} = undef ;
    to avoid some wrong signal from the OS. I don't know if you need this for 5.6.0+ too! Well, this problem with alarm is very specific.

    I saw that you uses sockets. Don't forget to close a socket after use it, or at least lose the $variable with the socket. Because the system, specially on Win32, has a limit for sockets to be opned.

    You have to monitore the memory used by the process too! Maybe it wasn't coleting the garbage of unused variables and objects!

    If you don't need the prompt window, run your script using the wperl.exe! This way doesn't open a console (pipe) for the process, and was 1 thing less to create problems!

    Good luck! ;-P

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      It's 5.6.1 -- i'll try $SIG{ALARM}=undef; just because I don't know what else to try.

      My first guess was that sockets weren't being closed, or were being closed twice. I wrote a bunch of code to check that everything was getting tidied up and it is.

      It uses 100 megs of RAM (according to the task manager), but almost all of that is in two big hashes. The only recycling will be in the socket objects which come and go.

      Maybe I'll try wperl -- couldn't hurt.

      Thanks for trying.
      john

Re: Mysterious script crash in win2k and ActivePerl
by batkins (Chaplain) on Dec 13, 2002 at 20:31 UTC
    try running the script from the command line. you'll be able to see any errors.
      The batch file runs from the command line. There's nothing to see -- the command prompt window is gone when I come back.
        well, then go to the command line and type myscript.bat or whatever its name is.
Re: Mysterious script crash in win2k and ActivePerl
by batkins (Chaplain) on Dec 14, 2002 at 12:12 UTC
    hmmm, are you SURE you're running the script from the command line? do you run cmd and then execute the cript? because if you are, the window shouldn't really ever close.

    but anyway, redirecting STDERR is probably your best bet:

    open(LOG, "+>log.txt"); *STDERR = *LOG; *STDOUT = *LOF; # just in case some errors go to STDOUT ...