Repository navigation
119 fix(ws): an exception in a WebSocket handler must not kill the worker thread - #125
Merged
Merged
Conversation
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.
Contributor
CoverageTotal lines: 81.91% → 81.94% (+0.03 pp)
|
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.
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, withsrc/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:
Throwable::$codewhen it is a valid 4xx/5xx, else 500), sothrow new HttpException('Forbidden', 403)rejects the handshake with a 403;CLOSE 1011frame (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_trybailout 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
mainand pass with the fix:038-handler-exception-precommit— throw before any WS I/O → 500 (and 403 fromThrowable::$code); three in a row, plain HTTP still served afterwards;039-handler-exception-committed— throw after the upgrade →TEXTthenCLOSE(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.