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

Hello Monks. I'm trying to connect to a mainframe with DBI. I understand almost nothing about mainframes. Most of the time the code below works fine. However, once in a while the code can not connect to the mainframe and just spins the cpu (i can see this with the top command on the linux box) and doesn't time out. When this happens I'd like the code to timeout and exit. The weird thing is I tested the following code on a "dummy" infinite loop and it worked in that case. Any ideas on how i can get this to work? Thank you for your help, Erik
eval{ local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm($timeout); $conn = DBI->connect($connection, $USER, $PASS, { RaiseError => 0, AutoCommit => 1 } ); alarm(0); }; if ($@){ my_die ( "Unable to login : [$@]: [$conn->errstr]"); }

Replies are listed 'Best First'.
Re: alarm function not working here
by ikegami (Patriarch) on Aug 07, 2006 at 20:43 UTC
    Signals, including the alarm signal, will only interrupt Perl when it is safe to do so. It would not be safe to do so in the middle of your database driver's C code, so Perl waits for the driver to return first. If the infinite loop occurs in your database driver's C code, alarm will not help you without switching to unsafe signals. Timeout alarm for regex has some discussion on this subject.