Skip to content

HttpServer::stop() in pool mode; workers commanded, not polled (#117) - #128

Merged
EdmondDantes merged 4 commits into
mainfrom
117-httpserverstop-unsupported-in-pool-mode-workers-1-cross-thread-shutdown-not-wired-up
Jul 13, 2026
Merged

EdmondDantes merged 4 commits into
mainfrom
117-httpserverstop-unsupported-in-pool-mode-workers-1-cross-thread-shutdown-not-wired-up

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

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 one uv_async wakeup per worker — the one libuv call that is safe from a foreign thread, and the same primitive thread_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 inside uv_async_send on a handle its owner is disposing.

  • reload() moves onto the channel; delivery is immediate instead of within a tick.
  • The deadline watchdog goes back to doing only deadlines — its cadence again follows the read/write/keepalive timeouts (up to 120 s when all are off).
  • The command is state, not an edge, which closes a latent hang: a worker thread that started after the epoch was bumped snapshotted the new value and would never retire, leaving the parent waiting on it forever.

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 makes Async\signal(SIGTERM) → stop() work under the built-in pool. A standalone stop() 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_close only reaches one that is back at recv; ours is inside start()). 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.

  • New core/058-pool-stop.phpt: workers=3 — stop() returns true, isRunning() is already false, the port is free, and it completes well under 2 s (an event, not a 1 s poll).
  • 028, 059, 060 expectations 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.

…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.
@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 81.86% → 82.28% (+0.42 pp)

File Baseline Current Δ Touched
src/core/http_connection_tls.c 73.46% 73.74% +0.28 pp
src/core/worker_dispatch.c 76.20% 79.32% +3.12 pp
src/core/worker_registry.c 65.12% 77.91% +12.79 pp
src/http3/http3_callbacks.c 82.43% 82.54% +0.11 pp
src/http3/http3_dispatch.c 82.09% 82.77% +0.68 pp
src/http3/http3_listener.c 75.98% 75.83% -0.15 pp
src/http_request.c 83.77% 83.94% +0.17 pp
src/http_server_class.c 71.60% 73.30% +1.70 pp ●
src/http_server_config.c 90.99% 92.23% +1.24 pp
src/log/http_log.c 69.19% 70.48% +1.29 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
EdmondDantes merged commit ebe6a4b into main Jul 13, 2026
8 checks passed
@EdmondDantes
EdmondDantes deleted the 117-httpserverstop-unsupported-in-pool-mode-workers-1-cross-thread-shutdown-not-wired-up branch July 13, 2026 20:32
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.

HttpServer::stop() unsupported in pool mode (workers > 1) — cross-thread shutdown not wired up

1 participant