Skip to content

FPM: pm=ondemand leaves a connection in the backlog when it arrives while the last idle child is being killed #25515

Open

Description

@nealobl

Description

With pm = ondemand, a connection that arrives while the master is killing the last idle child for pm.process_idle_timeout is never handled. It stays in the listen backlog until another client connects to the same pool.

The following configuration and client:

[global]
error_log = /proc/self/fd/2

[test]
user = www-data
group = www-data
listen = /run/fpm/test.sock
listen.mode = 0666
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 1s
pm.status_path = /status
# one client, no timeout, requesting roughly at the idle timeout interval
while true; do
  SCRIPT_NAME=/status SCRIPT_FILENAME=/status REQUEST_METHOD=GET \
    cgi-fcgi -bind -connect /run/fpm/test.sock >/dev/null
  date +%T
  sleep "$(awk -v s=$RANDOM 'BEGIN{printf "%.3f", 1+(s%2000)/1000}')"
done

Run in the official php:8.2-fpm / php:8.4-fpm images (apt-get install libfcgi-bin iproute2). Using 10 such pools with one client each makes it show up faster.

Resulted in this output:

the client stops printing; cgi-fcgi blocks forever

$ ss -xln | grep test.sock
u_str LISTEN 1      4096   /run/fpm/test.sock ...

master in epoll_wait (/proc/1/wchan: do_epoll_wait), no child process for the pool, nothing logged

With 10 pools: on 8.2.35 the first pool hung after 20 seconds; on 8.4.27, 5 of 10 pools hung within 5 minutes. With pm.process_idle_timeout = 60s and the same clients, 0 of 10 pools hung in 5 minutes (1553 requests). Opening and closing one extra connection to a stuck socket releases it immediately.

But I expected this output instead:

every request is answered; the master forks a child for the pending connection

Cause, from sapi/fpm/fpm (PHP-8.2):

  1. The idle maintenance heartbeat finds the last idle child idle for longer than pm.process_idle_timeout and sends it SIGQUIT via fpm_pctl_kill_idle_child(), setting child->idle_kill.
  2. Before that child is reaped, a new connection arrives. fpm_pctl_on_socket_accept() loops over wp->children, finds the dying child idle according to fpm_request_is_idle(), which does not look at idle_kill, and returns without forking.
  3. The child handles SIGQUIT in sig_soft_quit(), closes its listening socket and exits without accepting the connection.
  4. The ondemand event is registered with FPM_EV_EDGE, so the master gets no new event for the connection already in the backlog. It stays there until another client connects.

A possible fix is to skip children with idle_kill set in the idle check in fpm_pctl_on_socket_accept().

In production this hits monitoring agents: the Telegraf phpfpm input scrapes every 10s, the same as the default pm.process_idle_timeout, and by default waits without a timeout. On pools with no other traffic it hangs permanently. We saw this on more than 100 hosts. Busy pools recover on the next request, so visitors are rarely affected.

Possibly related: bug #69724 / GH-1308 (edge-triggered ondemand missing connections), GH-11765.

PHP Version

PHP 8.2.35, PHP 8.4.27 (reproduced); also observed in production on PHP 8.2 and 8.4

Operating System

Linux (epoll). Reproduced in the official php:*-fpm Docker images (Debian), production on Debian 13 trixie.

Activity

  1. vstratful commented on Nov 25, 2026

    @vstratful

    We hit the same stall independently and found a second ordering not covered by the proposed fix (skipping idle_kill children in fpm_pctl_on_socket_accept()), along with a deterministic reproduction.

    Second ordering: connection first, then retirement

    1. A connection arrives while the last child is genuinely idle (blocked in accept(), idle_kill not set). fpm_pctl_on_socket_accept() consumes the edge, sees the idle child, and returns without forking.
    2. The kernel wakes the child, but the child has not yet dequeued the connection (under load, this gap is scheduler latency).
    3. In that gap, the ondemand maintenance branch retires the same child for pm.process_idle_timeout and sends SIGQUIT.
    4. sig_soft_quit() closes the child's copy of the listener, and the child exits without accepting. fpm_children_bury() clears restart_child for the idle_kill child, so nothing is forked, and the edge was already consumed in step 1.

    Because the child was not marked idle_kill when the connection arrived, skipping idle_kill children in fpm_pctl_on_socket_accept() does not prevent the stall.

    Deterministic reproduction

    SIGSTOP stands in for the scheduler delay in step 2. Setup: one ondemand pool, pm.process_idle_timeout = 10s, unix socket, pm.status_path = /status, idle server.

    #!/bin/sh
    # usage: repro.sh <master-pid> <socket>
    MASTER=$1 SOCK=$2
    req() { SCRIPT_NAME=/status SCRIPT_FILENAME=/status REQUEST_METHOD=GET \
            cgi-fcgi -bind -connect "$SOCK" >/dev/null; }
    
    req                                   # leaves one idle worker
    W=$(pgrep -P "$MASTER")
    echo "$(date +%T) worker $W idle; STOP"
    kill -STOP "$W"
    sleep 6                               # before pm.process_idle_timeout
    ( req; echo "$(date +%T) first request done (rc=$?)" ) &   # queues; master sees an idle child, forks nothing
    REQ1=$!
    sleep 0.5
    echo "$(date +%T) first request sent; children: $(pgrep -P "$MASTER" | tr '\n' ' ')"
    
    # wait for the master's idle retirement (SIGQUIT = bit 0x4 in ShdPnd), resume before the next tick escalates to SIGKILL
    until [ $(( 0x$(awk '/^ShdPnd/{print $2}' /proc/"$W"/status) & 0x4 )) -ne 0 ]; do sleep 0.01; done
    kill -CONT "$W"
    echo "$(date +%T) SIGQUIT pending; CONT"
    
    sleep 20
    echo "$(date +%T) after 20s:"
    ss -xl | grep "$SOCK" | awk '{print "  ss: state="$2" Recv-Q="$3" Send-Q="$4}'
    pgrep -P "$MASTER" >/dev/null && echo "  workers: $(pgrep -P "$MASTER" | tr '\n' ' ')" || echo "  no workers"
    kill -0 "$REQ1" 2>/dev/null && echo "  first request still waiting" || echo "  first request already finished"
    req; echo "$(date +%T) second request done (rc=$?)"   # both complete now
    wait "$REQ1"

    Output on 8.4.28 (log_level = debug):

    16:30:02 worker 1396664 idle; STOP
    16:30:09 first request sent; children: 1396664
    16:30:13 SIGQUIT pending; CONT
    16:30:33 after 20s:
      ss: state=LISTEN Recv-Q=1 Send-Q=4096
      no workers
      first request still waiting
    16:30:33 first request done (rc=0)
    16:30:33 second request done (rc=0)
    
    DEBUG: fpm_children_bury(), line 310: [pool test] child 1396664 has been killed by the process management after 10.970614 seconds from start
    DEBUG: fpm_pctl_on_socket_accept(), line 553: [pool test] got accept without idle child available .... I forked   <- 16:30:33, the second request
    

    The worker exits gracefully (restart_child cleared, so it is logged as "killed by the process management" and not replaced), the queued connection stays in the backlog with no workers, and it is served only when the second request arrives. If the worker is instead SIGKILLed (the master's escalation one tick after an unanswered SIGQUIT), restart_child stays set and a replacement is forked, so the stall requires a graceful exit.

    Suggested fix

    A check after burying an idle_kill child of an ondemand pool covers both orderings: poll() the pool's listening socket with a zero timeout, and fork if it reports POLLIN (a non-empty backlog). An unneeded fork, when another child would have taken the connection, costs one process that is retired after pm.process_idle_timeout. This works for both unix and TCP sockets, unlike the existing listen-queue helper: cur_lq is only read for FPM_AF_INET (fpm_process_ctl.c#L342) and uses TCP_INFO on Linux, so it is always 0 for unix sockets. The idle_kill skip is still worth keeping, as it avoids waiting for the dying child in the original ordering.

    Additional data points

    • The reproduction above stalls the same way on 8.2.36, 8.3.37, 8.4.28, and 8.5.13 (Remi packages, AlmaLinux 10.3, kernel 6.12) with a standalone master and a plain listen unix socket. No relevant changes on the master branch as of abab936a39.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions