develooper Front page | perl.perl5.porters | Postings from July 2007

[perl #28824] signal handler can clobber $!

Thread Next
From:
Steve Peters via RT
Date:
July 24, 2007 14:23
Subject:
[perl #28824] signal handler can clobber $!
Message ID:
rt-3.6.HEAD-23341-1185312208-1573.28824-15-0@perl.org
On Thu Apr 15 06:25:56 2004, zefram <!-- x --> at fysh.org wrote:
> This is a bug report for perl from zefram <!-- x --> at fysh.org,
> generated with the help of perlbug 1.34 running under perl v5.8.3.
> 
> 
> -----------------------------------------------------------------
> [Please enter your report here]
> 
> If a signal is caught, and interrupts a system call, and the Perl
>    signal
> handler modifies $!, then this replaces the EINTR from the system
>    call.
> Simple test case:
> 
> >>>BEGIN t0<<<
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> $SIG{TSTP} = sub { print "caught SIGTSTP\n"; $! = 0; };
> 
> my $a;
> my $n = sysread(STDIN, $a, 1);
> print "sysread returned ", defined($n) ? $n : "undef",
> 	"; errno = ", 0+$!, " (", $!, ")\n";
> exit 0;
> >>>END t0<<<
> 
> The program attempts to read one character, and just displays what it
> gets back from sysread.  To perform the test, run the program from a
> terminal and ^Z while it's waiting for input.  I get this behaviour:
> 
> $ ./t0
> caught SIGTSTP
> sysread returned undef; errno = 0 ()
> $
> 
> This is pretty surprising behaviour to the code that was calling
>    sysread;
> returning undef with $! == 0 should be impossible.
> 
> The behaviour is perfectly understandable, and probably someone is
>    going
> to argue that it's correct.  But it's inconsistent with the behaviour
> one gets in C.  Here's the equivalent C program:
> 

The problem is that by the time $! is displayed at the end, several
other operations have occurred.  $! has to be checked as soon as
possible or you risk having it cleared out.  Looking at this, I'm
thinking that this isn't a bug.  Any other opinions?

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About