-
Breaking News -- Unsafe Signals, Unsafe
2004-03-04 14:03 in /tech/perl
Using unsafe signals in Perl 5.8.1 and later can have some bizarre consequences. For example, this code:
my $time; $time = `date +%H:%M:%S`; chomp $time; print "BEFORE [$time] ( $/ = ", unpack('H*', $/), " )\n"; eval { local $SIG{'ALRM'} = sub { die 'timeout' }; alarm(2); my $out = `sleep 5`; alarm(0); }; $time = `date +%H:%M:%S`; chomp $time; print "AFTER [$time] ( $/ = ", unpack('H*',$/), " )\n"; $/ = "\n"; my $foo = "blah\n"; chomp $foo; print "FOO [$foo] ( $/ = ", unpack('H*',$/), " )\n";
Produces the following:
%% PERL_SIGNALS=unsafe ./chompfail.pl BEFORE [13:38:49] ( $/ = 0a ) AFTER [13:38:51 ] ( $/ = 0a ) FOO [blah] ( $/ = 0a )
With safe signals, it's fine (but doesn't interrupt the sleep.) Also, with perl's
sleep
function it works fine. And, in the perl debugger, it seems to work fine.I'm not sure what's going on here (since it isn't as simple as
$/
being set strangely), but I think the clear lesson is that unsafe signals are to be avoided if you don't want to risk undefined behavior after the signal, which you probably don't if you're running a long-lived server process.
Leave a comment
Please use plain text only. No HTML tags are allowed.