Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminates after sigpipe if client close connection [rt.cpan.org #85904] #14

Open
oalders opened this issue Apr 1, 2019 · 2 comments
Open

Comments

@oalders
Copy link
Member

oalders commented Apr 1, 2019

Migrated from rt.cpan.org#85904 (status was 'new')

Requestors:

From victor@vsespb.ru on 2013-06-05 19:36:40
:

This behaviour is observed at least last ~5 years, so I am not sure maybe it's a feature.

I can submit PoC if required.


From victor@vsespb.ru on 2014-06-10 19:11:29
:

On Wed Jun 05 23:36:40 2013, vsespb wrote:
> This behaviour is observed at least last ~5 years, so I am not sure
> maybe it's a feature.
> 
> I can submit PoC if required.

====

use strict;
use warnings;
use HTTP::Daemon;

my $PORT = 55001;

if (fork()) { # parent
	#$SIG{PIPE}=sub{die "HEY\n";};
	my $d = HTTP::Daemon->new(Timeout => 20, LocalAddr => '127.0.0.1', LocalPort => $PORT);
	while (my $c = $d->accept) {
		my $r = $c->get_request;
		my $body = "x" x 100_000;
		my $resp = HTTP::Response->new(200, 'OK', [], $body);
		$c->send_response($resp);
		print STDERR "sent\n";
		$c = undef;  # close connection
	}
	print "DONE\n";
	wait;
} else { # child
	my $sock = IO::Socket::INET->new(PeerAddr => '127.0.0.1', PeerPort => $PORT, Proto    => 'tcp');
	print $sock "GET /\n\n";
	close $sock;
}


====

perl daemonpoc.pl || echo $?
will print 141. this means SIGPIPE (SIGPIPE number is 13 + 128 = 141).

also if you uncomment 
#$SIG{PIPE}=sub{die "HEY\n";};
it will print "HEY"

I think it something that should be at least documented.
people who use high level API like $c->send_response($resp); have insperation that they don't do low level things like writings to the sockets, so they are not expect they need to handle SIGPIPE.


@ArenT1981
Copy link

ArenT1981 commented Nov 15, 2023

Thanks for this heads up. Came across this as I had a head-scratching issue where my Perl server kept quitting, with no apparent error and nothing I was doing was trapping it. Turns out it was this issue, presumably the React client app I had built was closing the connection early due to caching or whatever, in any case, the exiting of the server was completely unnecessary since the client app was in a consistent state.

Putting

$SIG{PIPE} = \&sigpipe_warning;

...

sub sigpipe_warning
{
    print colored("Received SIGPIPE (141). Ignoring.", 'bright_green', 'bold'), "\n";
}

Was all I needed in my code for everything to work as intended, and stop the server quitting early/unnecessarily. Obviously for other applications/use-cases, it may be necessary to do more than simply disregard the error, depending on what you're building and the behaviour you need.

@oalders
Copy link
Member Author

oalders commented Nov 16, 2023

Thanks for this, @ArenT1981!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants