Skip to content

Do not write WebSocket handshake response to the tail of the pipeline (#17192) - #17201

Merged
normanmaurer merged 1 commit into
5.0from
ws_5
Aug 3, 2026
Merged

normanmaurer merged 1 commit into
5.0from
ws_5

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

WebSocketServerHandshaker writes the 101 Switching Protocols response
via
Channel.writeAndFlush(). The write starts at the tail of the pipeline,
so every
ChannelOutboundHandler placed after WebSocketServerProtocolHandler
sees the
raw HTTP upgrade response, even though handlers there operate on
WebSocket
frames. In #17141 a handler that queues outbound messages until
HandshakeComplete captured the response itself, so the handshake could
never
complete.

WebSocketServerHandshaker already provides ChannelHandlerContext
overloads
for close() for the same reason; this change applies the same pattern
to the
handshake response.

  • Add handshake(ChannelHandlerContext, ...) overloads to
    WebSocketServerHandshaker, following the existing
    close(ChannelHandlerContext, ...) overloads in the same class, and
    route both
    variants through a shared handshake0(ChannelOutboundInvoker, Channel, ...).
  • WebSocketServerProtocolHandshakeHandler now passes its ctx, so the
    response
    is written from the handshake handler's former position.
  • Update the benchmarkserver example to use the new overload.
  • Tests: a regression test asserts a handler behind the protocol handler
    observes no write during the handshake, for both full and non-full
    upgrade
    requests. Existing tests that captured the response behind the protocol
    handler now read it via readOutbound(). Four handshake(null, ...)
    calls
    needed a (Channel) cast to stay unambiguous.

Notes:

  • With the documented pipeline ordering (extension/compression handler
    before
    the protocol handler, as in WebSocketServerInitializer) nothing
    changes: the
    write still traverses WebSocketServerExtensionHandler and the HTTP
    encoder.
    Only handlers behind the protocol handler stop seeing the 101, which is
    the
    bug being fixed.
  • The tail-write behavior of the Channel overloads is unchanged and
    still
    covered by WebSocketServerHandshaker00/08/13Test and
    WebSocketServerHandshakerTest.

Fixes #17141.


Co-authored-by: el-psy-kongroo-d [email protected]
Co-authored-by: Norman Maurer [email protected]

…#17192)

`WebSocketServerHandshaker` writes the 101 Switching Protocols response
via
`Channel.writeAndFlush()`. The write starts at the tail of the pipeline,
so every
`ChannelOutboundHandler` placed after `WebSocketServerProtocolHandler`
sees the
raw HTTP upgrade response, even though handlers there operate on
WebSocket
frames. In #17141 a handler that queues outbound messages until
`HandshakeComplete` captured the response itself, so the handshake could
never
complete.

`WebSocketServerHandshaker` already provides `ChannelHandlerContext`
overloads
for `close()` for the same reason; this change applies the same pattern
to the
handshake response.

- Add `handshake(ChannelHandlerContext, ...)` overloads to
  `WebSocketServerHandshaker`, following the existing
`close(ChannelHandlerContext, ...)` overloads in the same class, and
route both
variants through a shared `handshake0(ChannelOutboundInvoker, Channel,
...)`.
- `WebSocketServerProtocolHandshakeHandler` now passes its ctx, so the
response
  is written from the handshake handler's former position.
- Update the benchmarkserver example to use the new overload.
- Tests: a regression test asserts a handler behind the protocol handler
observes no write during the handshake, for both full and non-full
upgrade
requests. Existing tests that captured the response behind the protocol
handler now read it via `readOutbound()`. Four `handshake(null, ...)`
calls
  needed a `(Channel)` cast to stay unambiguous.

Notes:

- With the documented pipeline ordering (extension/compression handler
before
the protocol handler, as in `WebSocketServerInitializer`) nothing
changes: the
write still traverses `WebSocketServerExtensionHandler` and the HTTP
encoder.
Only handlers behind the protocol handler stop seeing the 101, which is
the
  bug being fixed.
- The tail-write behavior of the `Channel` overloads is unchanged and
still
  covered by `WebSocketServerHandshaker00/08/13Test` and
  `WebSocketServerHandshakerTest`.

Fixes #17141.

---------

Co-authored-by: el-psy-kongroo-d <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Aug 3, 2026
@normanmaurer
normanmaurer merged commit 059f1c0 into 5.0 Aug 3, 2026
13 checks passed
@normanmaurer
normanmaurer deleted the ws_5 branch August 3, 2026 17:38
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.

2 participants