Repository navigation
Description
Problem
HttpServer::stop() throws when called on a pool-parent server (config->setWorkers(N) with N > 1):
// src/http_server_class.c, ZEND_METHOD(TrueAsync_HttpServer, stop)
if (server->in_pool_mode) {
zend_throw_exception(http_server_runtime_exception_ce,
"stop() on a pool-parent HttpServer is not supported yet "
"(issue #11). Stop each worker from within its own handler.", 0);
RETURN_FALSE;
}Each pool worker runs its own cloned HttpServer PHP object in its own thread (pool_worker_handler calls start() on the transferred clone), so the parent's stop() has no way to reach them — there is currently no cross-thread signal for "shut down and don't come back."
Found while adding graceful shutdown (Async\signal(SIGTERM/SIGINT) → stop()) to laravel-spawn's TrueAsyncServer: it works correctly for workers=1 (standalone path, not pool mode), but production configs typically run Async\available_parallelism() workers, which hits this exception.
Referenced as "issue #11" in the code comment, but #11 itself (built-in worker pool) is closed — this specific cross-thread-stop gap was never split out into its own tracked issue.
Why this shouldn't be hard
HttpServer::reload() (issue #93, shipped) already solves the identical cross-thread coordination problem for hot-reload:
- Bumps a shared
reload_shared->epoch(atomic). - Each worker notices the bump on its own tick and retires itself.
- The parent awaits every worker's exit via the existing
pool_await_state/all_donemechanism (the same onestart()'s own pool-launch path suspends on). - Only after the old cohort has fully drained does
reload()submit a fresh cohort.
A pool-mode stop() needs the same "workers notice a shared flag on their tick, drain, exit, parent awaits all_done" path, just without the final "submit a fresh cohort" step.
Proposed approach
- Add a
stoppingflag alongside (or reusing)reload_shared, checked wherever a worker currently pollsepochon its tick. - On that flag, a worker drains and exits (same graceful-shutdown path a standalone
stop()already takes) instead of reloading. HttpServer::stop(), whenin_pool_mode, sets the flag and awaitspool_await_state->all_done(mirroring the suspend in the pool-launch path ofstart()), instead of throwing.- No new cohort is submitted afterward —
running/in_pool_modeare cleared like the non-poolstop()already does.
Rough estimate: half a day to a day — most of the hard cross-thread coordination is already proven working by reload(); this is adapting it, not building it from scratch.
Where this was found
laravel-spawn, src/Server/TrueAsyncServer.php::registerShutdownSignals() — currently catches the exception and logs it, since there's nothing more it can do from PHP until this lands. See that PR for the workaround.
Activity
Add graceful shutdown and e2e tests for streaming
Merge pull request #128 from true-async/117-httpserverstop-unsupporte…
fix(websocket): meter publish() on the connection, not the session (#120