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

[perl #122373] print() to closed IO::Socket filehandle causes interpreter to exit silently

Thread Previous | Thread Next
From:
Tony Cook via RT
Date:
July 23, 2014 03:25
Subject:
[perl #122373] print() to closed IO::Socket filehandle causes interpreter to exit silently
Message ID:
rt-4.0.18-26920-1406085904-1230.122373-15-0@perl.org
On Tue Jul 22 17:38:34 2014, gdg@zplane.com wrote:
> In the attached minimal example, the perl interpreter silently exits
> upon
> attempting a print() to an IO::Socket filehandle that has been closed
> at
> the remote end.
> 
> Attempting such a print() is obviously erroneous, but would expect a
> diagnostic to be issued, rather than silent death.
> 
> Further explanation and instructions on how to reproduce are in the
> script.

That's the  operating system throwing an uncaught SIGPIPE at your process.  Here's the tail of strace output for your script:

write(2, "Client disconnected, now try pri"..., 46) = 46
write(2, "Before print to $cfh:\n", 22) = 22
write(4, "This print() call dies silently "..., 33) = -1 EPIPE (Broken pipe)
--- SIGPIPE (Broken pipe) @ 0 (0) ---
+++ killed by SIGPIPE +++

If you add the following to the code somewhere near the top:

$SIG{PIPE} = sub { print STDERR "**SIGPIPE**\n" };

your script will output something like:

Connection accepted, polling for data...
..
R: abc
..Client disconnected, now try print() to $cfh:
Before print to $cfh:
**SIGPIPE**
We never get here
.Client disconnected, now try print() to $cfh:
Before print to $cfh:
We never get here
.Client disconnected, now try print() to $cfh:
Before print to $cfh:
We never get here
.Client disconnected, now try print() to $cfh:
Before print to $cfh:
We never get here
^C

Alternatively you can tell perl to ignore the signal:

$SIG{PIPE} = "IGNORE";

As to producing a diagnostic when you have SIGPIPE ignored, I added some diagnostic code to your print:

	    print {$cfh} "This print() call dies silently \n"
	      or print STDERR "print failed: $!\n";

which produced:

.Client disconnected, now try print() to $cfh:
Before print to $cfh:
**SIGPIPE**
print failed: Broken pipe
We never get here
.Client disconnected, now try print() to $cfh:
Before print to $cfh:
print failed: Transport endpoint is not connected
We never get here
...

I'll close this ticket in a few days, unless someone points out a bug in perl.

Tony

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=122373

Thread Previous | 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