Repository navigation
Description
Summary
An uncaught exception thrown inside a WebSocket handler takes down the entire worker thread, silently. One bad connection = one worker gone for the rest of the process. Once every worker has served one such connection, the server stops accepting anything at all — including plain HTTP.
An exception in an HTTP handler is handled correctly (500 response, worker unaffected), so this is specific to the WebSocket path.
Reproduction
$config = (new HttpServerConfig())
->addListener('127.0.0.1', 18110)
->setWorkers(2)
->setWsPingIntervalMs(0);
$server = new HttpServer($config);
$server->addWebSocketHandler(function (WebSocket $ws, HttpRequest $req) {
throw new \RuntimeException('boom from handler');
});
$server->addHttpHandler(function ($req, $res) { $res->setStatusCode(404)->end(); });
$server->start();Then open three WebSocket connections in turn.
Observed (2 workers):
connect #1: ok
[true-async-server] worker exited
[true-async-server] worker exited
connect #2: ok
connect #3: REFUSED / no upgrade
http after: REFUSED
Each WS connection consumes one worker. After two, the port is dead — plain HTTP is refused too.
Control — the same exception from an HTTP handler:
http #1: HTTP/1.1 500 boom from http
http #2: HTTP/1.1 500 boom from http
http #3: HTTP/1.1 500 boom from http
http after: ok
Worker survives, server keeps serving. So the WS path is the outlier.
Why it is nasty
The log line is just:
[true-async-server] worker exited
No class, no message, no stack. http_server_class.c:1947 does have a branch that logs worker thread died: <class>: <message> when EG(exception) is set — but it is not reached, so start() is returning without a pending exception (the comment there calls this "a swallowed bailout while running"). The failure is therefore invisible: capacity silently disappears, and the only symptom is that the server gradually stops accepting connections.
This cost me hours while implementing #2 — a genuine bug in my own code (a WebSocketClosedException from join()) presented as "workers randomly exit with no message", and the real error was never printed.
Expected
An exception escaping a WebSocket handler should behave like one escaping an HTTP handler: terminate that connection (close frame, e.g. 1011 Internal Error), log the class + message, and leave the worker running.
At minimum it must not be silent: whatever kills a worker has to be logged with the exception that caused it.
Notes
- The handler coroutine is spawned per connection; the exception apparently unwinds past the point where
start()can observe it asEG(exception). - Related, but distinct from the closure-cycle segfault fixed in ext/async (
4543681) — this needs no cycle, just athrow.
Activity
Merge branch 'main' into 2-ws-cross-worker-rooms (#2)