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

Updated this description: I'm looking at a program in which a connection to MySQL is established via the DBI module. Then a wget command is issued that takes a long time. During that time, the database connection times out, and must send a signal to the parent. This causes $? and $! to be set. But in addition to those, the system('wget') call returns -1. I know the wget succeeds, because the file is completely downloaded. But since system() returns -1, it looks like the wget failed!

Update2: I think Tye figured this out while we were chatting in the CB: system calls waitpid, and waitpid is interrupted by the signal. So my child wget command keeps on executing the background, while the perl script resumes execution.

I wrote a sample program that shows the same problem, but I think that it might be causing confusion. I don't want the child to receive the signal, nor am I using this alarm as a timeout to make sure the system call doesn't hang. My use of alarm w/ system here may be wrong, as mr_mischief pointed out to me. I'm leaving it here though, because I *think* it demonstrates the same problem.

#!/usr/bin/env perl use strict; use warnings; $SIG{ALRM} = sub { print( "Alarm triggered, making system call...\n" ); unlink('/doesnt/exist'); # this will definitely fail }; alarm( 2 ); my $retval = system('sleep 4'); if( $retval == -1 ) { print( "system() retval = $retval; " . '$?' . " = $?; " . '$!' . " + = $!\n" ); } elsif( $retval == 0 ) { print( "system() returned 0\n" ); } else { print( "system() return $retval\n" ); }
And so here is a test run:
test$ ./ Alarm triggered, making system call... system() retval = -1; $? = -1; $! = No such file or directory
I searched the archives, and I found an old node that may be related.

I tested this on two different systems, with two different perl version. Some system details:
System 1: $ perl -v This is perl, v5.8.8 built for i386-linux-thread-multi libc=/lib/ $ uname -a Linux hostname.removed 2.6.18-8.1.1.el5 #1 SMP Mon Feb 26 20:38:02 EST + 2007 i686 i686 i386 GNU/Linux System 2: test$ perl -v This is perl, v5.8.6 built for i686-linux-64int libc=/lib/ test$ uname -a Linux hostname.removed 2.6.9-55.0.6.ELsmp #1 SMP Tue Sep 4 21:36:00 ED +T 2007 i686 unknown
I haven't been able to find any documentation about this behavior. Is this intended? If so, how can I trust system's return value?