Skip to content

119 fix(ws): an exception in a WebSocket handler must not kill the worker thread - #125

Merged
EdmondDantes merged 2 commits into
mainfrom
119-ws-handler-exception
Jul 13, 2026
Merged

EdmondDantes merged 2 commits into
mainfrom
119-ws-handler-exception

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Fixes #119.

The bug

An uncaught exception in a WebSocket handler took down the whole worker thread. The WS handler coroutine never consumed its coroutine->exception, so it was rethrown at finalize and retired the worker — the same escalation #101 fixed for H1/H2/H3, with src/websocket/ left out of that fix. One throwing connection cost a thread of accept capacity, silently; once every worker had served one, the port refused everything, including plain HTTP.

Two spawn sites had the hole, both fixed:

  • src/websocket/ws_dispatch.c — the HTTP/1 upgrade path;
  • src/http2/http2_strategy.c — the Extended CONNECT path (RFC 8441), which carries its own copy of the handler entry/dispose.

The fix

A failing WS handler now behaves like a failing HTTP handler. The exception is consumed (both escalation flags), logged with class / message / origin — a WS handler has no response body to carry it to the operator — and the failure is reported to the peer in-protocol:

  • before the upgrade committed → an HTTP status instead of a bogus 101 (Throwable::$code when it is a valid 4xx/5xx, else 500), so throw new HttpException('Forbidden', 403) rejects the handshake with a 403;
  • on a live session → a CLOSE 1011 frame (RFC 6455 §7.4.1), no reason payload.

Cancellation (server stop, connection teardown) arrives on the same field but is not a handler failure — it stays a clean teardown, gated on ZEND_COROUTINE_IS_CANCELLED.

Also adds the zend_try bailout firewall both WS entries were missing: a fatal (memory_limit) longjmp'd straight out of the coroutine and killed the worker outright, where the H1/H2/H3 entries already survive it with a 500.

Tests

Three new phpt, each verified to fail on main and pass with the fix:

  • 038-handler-exception-precommit — throw before any WS I/O → 500 (and 403 from Throwable::$code); three in a row, plain HTTP still served afterwards;
  • 039-handler-exception-committed — throw after the upgrade → TEXT then CLOSE(1011); a normal WS session on the next connection still echoes;
  • 040-h2-handler-exception — the same over HTTP/2 Extended CONNECT, second stream on the same connection still echoes.

Full suites green locally: 40/40 websocket, 315/315 server + parser.

The WS handler coroutine never consumed its exception, so it was rethrown
at finalize and retired the whole worker — the same escalation #101 fixed
for H1/H2/H3, with websocket/ left out. One throwing connection cost a
thread of accept capacity, silently; after N of them the port refused
everything, including plain HTTP.

Both WS spawn sites are covered: the H1 upgrade path (ws_dispatch.c) and
the H2 Extended CONNECT path (http2_strategy.c), which had its own copy of
the hole. A failing handler is now reported in-protocol — an HTTP status
pre-commit (Throwable::$code when it is a 4xx/5xx, else 500), CLOSE 1011 on
a committed session — and logged with class/message/origin, since a WS
handler has no response body to carry the exception to the operator.
Cancellation stays a clean teardown, not a failure.

Also adds the bailout firewall both WS entries were missing: a fatal
(memory_limit) longjmp'd straight out of the coroutine and killed the
worker outright.
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 81.91% → 81.94% (+0.03 pp)

File Baseline Current Δ Touched
src/core/http_connection_tls.c 73.74% 73.46% -0.28 pp
src/http2/http2_strategy.c 84.18% 83.82% -0.36 pp ●
src/http3/http3_callbacks.c 82.54% 82.43% -0.11 pp
src/http3/http3_listener.c 75.83% 76.28% +0.45 pp
src/http3/http3_packet.c 85.22% 90.43% +5.22 pp
src/websocket/ws_dispatch.c 80.00% 81.15% +1.15 pp ●

@EdmondDantes
EdmondDantes merged commit 213cb68 into main Jul 13, 2026
9 checks passed
@EdmondDantes
EdmondDantes deleted the 119-ws-handler-exception branch July 13, 2026 15:15
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.

An exception in a WebSocket handler silently kills the worker thread

1 participant