From ffe8908e4887f1b90b66b6f87a5479ade0729a09 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:32:31 +0000 Subject: [PATCH] Net::Server::Thread: enforce max_servers and survive thread failures The accept loop waited while threads->list() exceeded max_servers, but every handler thread was detached as soon as it was created, and threads->list() does not return detached threads. The count stayed near zero and the limit never applied, so a client holding many connections open made the server clone a Perl interpreter for each one. When threads->new then failed and returned undef, the loop called ->detach on undef and the server died. Keep handler threads joinable instead of detaching them, count the running ones with threads->list(threads::running()) when applying max_servers, and join finished threads with a new reap_threads method on each pass through the loop and while waiting for a free slot. When threads->new fails, log the error and carry on; the parent drops the client as before, which closes the connection. threads::running() and threads::joinable() are called with parentheses because threads is loaded at run time. Threads still handling a request when the server exits are reported by perl as running and unjoined, where before they were detached. Notes for deciding whether to apply this patch: - Handler threads are no longer detached. As with the Fork server type, the check allows up to max_servers + 1 handler threads to run. - When the server exits while threads are still handling requests, perl prints a "Perl exited with active threads" warning, where before the detached threads ended silently. - A failed thread creation is logged and the connection is dropped instead of stopping the server. - 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/Thread.pm | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Net/Server/Thread.pm b/lib/Net/Server/Thread.pm index 89e940c..9f7fbda 100644 --- a/lib/Net/Server/Thread.pm +++ b/lib/Net/Server/Thread.pm @@ -51,11 +51,13 @@ sub loop { while (1) { threads->yield(); + $self->reap_threads; - while (threads->list() > $prop->{'max_servers'}){ + while (threads->list(threads::running()) > $prop->{'max_servers'}){ select undef, undef, undef, .5; # block for a moment (don't look too often) check_sigs(); threads->yield(); + $self->reap_threads; } $self->pre_accept_hook; @@ -68,13 +70,21 @@ sub loop { $self->pre_thread_hook; - threads->new(sub { $self->run_client_connection })->detach; + # handler threads are joined by reap_threads rather than detached, + # so that threads->list counts them against max_servers + if (! threads->new(sub { $self->run_client_connection })) { + $self->log(1, "Could not create thread to handle connection: $!"); + } # parent delete($prop->{'client'}) if !$prop->{'udp_true'}; } } +sub reap_threads { + $_->join for threads->list(threads::joinable()); +} + sub close_children { my $self = shift; my $prop = $self->{'server'}; -- 2.53.0