From 9f6b928dbc650a4fed1ce4b9f6edfd2572d54625 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:31:19 +0000 Subject: [PATCH] Net::Server::PSGI: bound response writes with a send timeout process_request limits each request with alarm(timeout_idle) and an ALRM handler that dies, but the response is written with print on a blocking socket. When the alarm interrupts a write(2) that has already sent some bytes, the call returns the partial count, perl defers the signal, and PerlIO goes on to write the rest, blocking again. The alarm therefore never ends a write to a client that has stopped reading, and such a client holds a server process for as long as it keeps the connection open. Set SO_SNDTIMEO on the client socket to timeout_idle before running the application. A write that makes no progress for that long now returns, perl delivers the pending alarm, and the request ends as timeout_idle already documents. This covers array, filehandle and streaming responses, since all of them write to the client socket. The option is skipped on MSWin32, where SO_SNDTIMEO takes a different argument format, and a failure to set it is ignored. Notes for deciding whether to apply this patch: - Apply together with the patch that stops Net::Server::PSGI re-throwing request errors. Without it, the error raised when a stalled write times out kills a PreFork worker or stops a Single server. - A client that keeps reading a little before each timeout still holds a worker. Only writes that make no progress at all are bounded. - The timeout is packed as two native longs (struct timeval). That holds on Linux and the BSDs with standard perl builds but may not on 32-bit builds with a 64-bit time_t. - The option is not set on MSWin32, and a failure to set it is ignored. - Under proto ssleay a write is retried up to five times, so a stalled write can last up to about five times timeout_idle. - Writes through Net::Server::HTTP are not changed by this patch. - Not tested on a running server. It was checked by review and by applying it to Net-Server 2.018. - Applies on its own to Net-Server 2.018, and together with the other CPANSec patches for Net-Server 2.018 when they are applied in CVE-ID order. --- lib/Net/Server/PSGI.pm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Net/Server/PSGI.pm b/lib/Net/Server/PSGI.pm index 9cdda38..8eefbf3 100644 --- a/lib/Net/Server/PSGI.pm +++ b/lib/Net/Server/PSGI.pm @@ -18,6 +18,7 @@ package Net::Server::PSGI; use strict; use base qw(Net::Server::HTTP); use Scalar::Util qw(blessed); +use Socket (); sub net_server_type { __PACKAGE__ } @@ -51,6 +52,7 @@ sub process_request { $self->process_headers; alarm($self->timeout_idle); + $self->_set_send_timeout($self->timeout_idle); my $env = { %ENV }; $env->{'psgi.version'} = [1, 0]; $env->{'psgi.url_scheme'} = ($ENV{'HTTPS'} && $ENV{'HTTPS'} eq 'on') ? 'https' : 'http'; @@ -74,6 +76,17 @@ sub process_request { } } +# The timeout_idle alarm cannot interrupt a write(2) that has already +# sent part of its data, so a client that stops reading would block the +# response write forever. SO_SNDTIMEO makes each blocked write return, +# so the pending alarm is delivered and ends the request. +sub _set_send_timeout { + my ($self, $timeout) = @_; + my $client = $self->{'server'}->{'client'}; + return if ! $timeout || ! $client || $^O eq 'MSWin32'; + eval { setsockopt($client, Socket::SOL_SOCKET(), Socket::SO_SNDTIMEO(), pack('l!l!', $timeout, 0)) }; +} + sub process_psgi_request { my ($self, $env) = @_; my $app = $self->find_psgi_handler($env); -- 2.53.0