Repository navigation
HttpServer::stop() in pool mode; workers commanded, not polled (#117) - #128
Merged
EdmondDantes merged 4 commits intoJul 13, 2026
Conversation
…d, not polled The pool parent reaches its workers over a control channel: an atomic command plus one uv_async wakeup per worker, published under the lock that a retiring worker closes its handle under. reload() moves onto it and the deadline watchdog goes back to doing only deadlines — it no longer polls a shared epoch, so it no longer has to tick every second. stop() on a pool parent used to throw. It now retires the cohort and suspends until the pool is down, so it returns once the server is really gone. A standalone stop() still does not suspend: it is called from a request handler, which the shutdown drain waits on. Fixes a hang and a use-after-free on graceful_shutdown(): the parent's await is cancelled while workers still serve, and nothing in the engine stops a busy worker. The parent freed the beacon under them; they read the freed memory, saw it differ from their snapshot, and self-stopped — which is the only reason shutdown appeared to work. The parent now commands them out, and the channel is refcounted so it outlives them.
Contributor
CoverageTotal lines: 81.86% → 82.28% (+0.42 pp)
|
The watcher sees the file the test wrote just before start(), so the pool rotates once at boot and the retiring cohort logs it. That rotation used to be a no-op — workers that loaded after the epoch bump snapshotted the new value and never retired.
EdmondDantes
deleted the
117-httpserverstop-unsupported-in-pool-mode-workers-1-cross-thread-shutdown-not-wired-up
branch
July 13, 2026 20:32
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.
Closes #117.
What was wrong
stop()on a pool parent threw. There was no parent → worker channel at all:reload()(#93) bumped a shared epoch that each worker read from its deadline watchdog tick, which is why that timer was forced to fire at least once a second per worker even when every timeout was disabled. The watchdog exists to reap idle connections; it had been made to carry pool control traffic.What this does
The parent now reaches its workers over a control channel: an atomic command (
NONE/RELOAD/STOP) plus oneuv_asyncwakeup per worker — the one libuv call that is safe from a foreign thread, and the same primitivethread_mailbox(#81) already uses. A retiring worker empties its slot and closes its handle under the same lock the parent's broadcast holds, so the parent can never be insideuv_async_sendon a handle its owner is disposing.reload()moves onto the channel; delivery is immediate instead of within a tick.stop()on a pool parent retires the cohort and suspends until the pool is down — when it returns, the workers have drained, the reactor pool is torn down and the listen sockets are closed. This is what makesAsync\signal(SIGTERM)→stop()work under the built-in pool. A standalonestop()still does not suspend: it is normally called from a request handler, which the shutdown drain waits on — it would be waiting for itself.The bug found on the way
Async\graceful_shutdown()cancels the parent's await while its workers are still serving, and nothing in the engine stops a busy worker (thread_pool_closeonly reaches one that is back atrecv; ours is insidestart()). The parent walked away and freed the shared beacon under their feet — the workers then read the freed memory, saw a value that differed from the epoch they had snapshotted, and self-stopped. That use-after-free is the only reason pool shutdown appeared to work.The parent now commands the cohort out before it leaves, and the channel is refcounted (parent + one per transit shell + one per clone) so it outlives every worker still reading it.
Tests
Full suite: 357 passed, 0 failed.
core/058-pool-stop.phpt: workers=3 —stop()returnstrue,isRunning()is alreadyfalse, the port is free, and it completes well under 2 s (an event, not a 1 s poll).028,059,060expectations updated:stop()under a pool now really terminates the workers, so they exit cleanly and log it where they used to be killed with the process.