Skip to content

Bound the stop-time log drain so a stalled sink cannot pin the log thread (#121) - #126

Merged
EdmondDantes merged 2 commits into
mainfrom
121-log-sink-whose-receiver-stalls-breaks-process-shutdown-event-loop-must-be-stopped
Jul 13, 2026
Merged

EdmondDantes merged 2 commits into
mainfrom
121-log-sink-whose-receiver-stalls-breaks-process-shutdown-event-loop-must-be-stopped

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Fixes #121.

The bug

A log sink whose receiver stops reading (a socket nobody drains, a wedged syslog
collector) leaves a write that never completes. The log thread's final drain waited
on it forever: the transport stayed open, the thread never left its loop, and the
process aborted on

ZEND_ASYNC_REACTOR_LOOP_ALIVE() == false && "The event loop must be stopped"

The issue guessed the assert fires on the parent's loop. It does not — under gdb it
is the log thread itself whose scheduler finalizes while its own loop still owns the
open io. The parent is elsewhere, parked in libuv_reactor_quiesce waiting for that
thread to join. That misdirection is likely why the earlier attempt went sideways.

The fix

The stop-time drain now runs against a deadline on the log thread
(HTTP_LOG_CLOSE_DRAIN_BUDGET_MS, 1 s). Past it, writer_teardown runs regardless of
what is still queued, so the io always closes and the thread always leaves its loop. The
stopping thread's existing 3 s budget goes back to being a backstop instead of the thing
that has to fire — which matters, because that path frees the trigger the log thread
would later fire.

The delicate part is the write in flight. Its buffer cannot simply be freed when the io
is closed: for a file sink uv_fs_write is reading that memory in the blocking pool right
now. So teardown hands both buffer and request to the reactor via the request's public
free_cb, and the cancellation completion frees them. That works uniformly for both
transports — the stream path takes libuv's fire-and-forget branch on UV_ECANCELED, the
file path routes through the deferred-dispose branch.

The deadline is checked for the whole closing drain, not just for a stuck write: a
slow-but-progressing peer could otherwise overrun the 3 s budget and hit that same
freed-trigger use-after-free.

Test

The repro was already in the tree as 030-log-drop-counter.php — a bare .php, which
tests/.gitignore excludes, so run-tests never saw it. That is the test the issue says was
"removed rather than committed". Restored as a proper .phpt.

Verification

  • repro exits 0, consistently ~1.2 s (the 1 s deadline plus the 200 ms flush-timer granularity that enforces it)
  • valgrind: 0 definitely-lost bytes, 0 errors — the abandoned buffer and request are reclaimed
  • full phpt suite: 356 tests, 0 failed (the single WARN is the pre-existing XFAIL-that-passes on 047-request-context-concurrent, unrelated)

Not fixed (out of scope)

A pipe sink can stall the same way and is not addressed. log_io_type_for_fd maps a
non-socket fd to the FILE path, so a stalled pipe reader parks a uv_fs_write in libuv's
blocking pool, which cannot be cancelled from our side. This change is safe there (no UAF,
no double free) but cannot rescue it. The issue scopes itself to sockets ("a file sink
cannot stall this way").

…the log thread

A sink whose receiver stops reading leaves a write that never completes. The
log thread's final drain waited on it forever: the transport stayed open, the
thread never left its loop, and the process aborted on the loop-alive assert.

The drain now runs against a deadline on the log thread itself. Past it, the
write in flight is abandoned and the transport torn down regardless — the
abandoned write keeps its buffer until the reactor reports the cancellation,
since a file write is still reading it in the blocking pool. The stopping
thread's budget goes back to being a backstop rather than the thing that fires.
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 81.88% → 81.87% (-0.01 pp)

File Baseline Current Δ Touched
src/core/response_wire.c 92.73% 93.33% +0.61 pp
src/core/worker_dispatch.c 74.22% 76.20% +1.98 pp
src/http3/http3_callbacks.c 82.76% 82.54% -0.22 pp
src/http3/http3_dispatch.c 81.63% 82.09% +0.45 pp
src/http3/http3_static_response.c 69.62% 68.35% -1.27 pp
src/log/http_log.c 69.76% 69.19% -0.57 pp ●

@EdmondDantes
EdmondDantes merged commit 6631e29 into main Jul 13, 2026
8 checks passed
@EdmondDantes
EdmondDantes deleted the 121-log-sink-whose-receiver-stalls-breaks-process-shutdown-event-loop-must-be-stopped branch July 13, 2026 16:19
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

Successfully merging this pull request may close these issues.

Log sink whose receiver stalls breaks process shutdown (event loop must be stopped)

1 participant