Repository navigation
Bound the stop-time log drain so a stalled sink cannot pin the log thread (#121) - #126
Merged
EdmondDantes merged 2 commits intoJul 13, 2026
Conversation
…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.
Contributor
CoverageTotal lines: 81.88% → 81.87% (-0.01 pp)
|
EdmondDantes
deleted the
121-log-sink-whose-receiver-stalls-breaks-process-shutdown-event-loop-must-be-stopped
branch
July 13, 2026 16:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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_quiescewaiting for thatthread 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_teardownruns regardless ofwhat 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_writeis reading that memory in the blocking pool rightnow. 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 bothtransports — the stream path takes libuv's fire-and-forget branch on
UV_ECANCELED, thefile 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, whichtests/.gitignoreexcludes, so run-tests never saw it. That is the test the issue says was"removed rather than committed". Restored as a proper
.phpt.Verification
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_fdmaps anon-socket fd to the FILE path, so a stalled pipe reader parks a
uv_fs_writein libuv'sblocking 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").