mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Migrate the non-gRPC HTTP/2 path to Http2MultiplexHandler (stream-per-child-channel)

Open jamesdbloom opened this issue 2 months ago • 4 comments

Summary

The non-gRPC HTTP/2 path multiplexes every stream over a single shared connection channel, with stream identity carried in an x-http2-stream-id header rather than by the channel itself. Netty's Http2MultiplexHandler model — where each stream is a child channel — is the architecturally correct end state and would make a whole class of stream-routing bugs unrepresentable rather than merely fixed.

This is tracked from the TODO(jamesdbloom) at mockserver/mockserver-netty/src/main/java/org/mockserver/netty/unification/PortUnificationHandler.java:447. Filing it so the deferred work is visible: every issue that motivated it (#2641, #2667) is now closed, so the TODO was the only remaining record.

Not a correctness prerequisite

To be clear about priority: the bug that made this urgent is fixed. #2667 (a chunk for one stream being written to a different, already-closed stream) is resolved on the shared-connection architecture by StreamRoutingHttpToHttp2ConnectionHandler + StreamAddressedHttpContent, which address each data write to its originating stream instead of relying on the codec's single mutable currentStreamId. There is no known open correctness defect requiring this migration.

Why it is still worth doing

The current design keeps working by convention — every outbound write must remember to carry its stream id. Http2StreamIdAuditHandler exists precisely because a response head that forgets is silently mis-routed onto a new server-initiated stream, producing a hang the client sees and the server never reports. That is a warning about a design that is easy to get wrong, and it only covers heads, not content frames.

Under the multiplex model the child channel carries stream identity structurally, so:

  • a write cannot be mis-addressed, because there is nothing to address;
  • the audit handler and the x-http2-stream-id header contract become unnecessary;
  • fully-interleaved concurrent streaming — still a limitation today — falls out for free.

Known blast radius

This was scoped while fixing #2667 and is genuinely large; the whole non-gRPC pipeline assumes one channel per connection:

  • HttpRequestHandler and everything sharing the connection channel
  • the x-http2-stream-id header contract and Http2StreamIds
  • Http2StreamIdAuditHandler (removable)
  • StreamRoutingHttpToHttp2ConnectionHandler / StreamAddressedHttpContent (removable — superseded)
  • the keep-alive / ctx.close() handling in HttpSseResponseActionHandler.finishStream from #2641, which must never close the shared parent
  • WebSocket, dashboard and MCP handlers on the shared channel
  • the h2c upgrade path and the ALPN/TLS switch in PortUnificationHandler

The gRPC path (switchToHttp2Multiplex) already uses Http2FrameCodec + Http2MultiplexHandler, so it is the working reference for the target design.

Suggested approach

Not a single change. Likely: migrate behind a flag with both paths coexisting, port the handlers that assume a connection-scoped channel, then remove the header contract and the audit handler once the multiplex path is the only one. Concurrent interleaved streaming is the acceptance test — it is the case that does not work today.

jamesdbloom avatar Sep 09 '26 18:09 jamesdbloom

I will take this issue. Please assign it to me.

The current implementation uses a shared connection channel with stream identity in headers. The goal is to migrate to Http2MultiplexHandler, where each stream is a child channel. I will start by examining the PortUnificationHandler.java at line 447, as noted in the TODO. The first step is to replace the existing stream handling logic with Http2MultiplexHandler. I will ensure that the stream identity is correctly managed by the child channels, removing the need for x-http2-stream-id headers and related handlers. This should simplify the architecture and prevent mis-routing issues.

modelpath-dev avatar Sep 10 '26 21:09 modelpath-dev

Done. The Http2FrameCodec + Http2MultiplexHandler pipeline is now the only HTTP/2 server pipeline, for both h2 and h2c, with no configuration flag — every stream gets its own child channel.

What actually took the time

The multiplexed pipeline already existed here, used only when grpcBidiStreamingEnabled was set. That made it a quiet corner of the codebase, and quiet corners hide a specific kind of bug: connection-level machinery that does nothing useful on a per-stream channel and says nothing about it. Six of those were found and fixed before the switch, each one on its own, each one already reachable today by anyone running gRPC bidi streaming:

f815de0bf the websocket handshaker was stored per-handler, not per-channel
0c7c9481e stream channels did not inherit the connection's TLS, proxy and protocol state
382060b21 GOAWAY was written to the stream's pipeline, where no connection handler exists, and silently did nothing
b9e51edac streaming responses never sent end-of-stream, so SSE and LLM clients hung until they timed out
4d89afdb8 request bodies were never decompressed, and malformed request headers were refused outright
037d2634a the mid-response reset fault reset one stream instead of aborting the socket

Every one of them failed silently. Nothing threw, nothing was logged, and the server believed it had acted each time. The switch itself is 822de0b40; ae251704b removes the old machinery.

Two more surfaced during the switch, both caught by review rather than by the test suite:

  • the gRPC request handler lost its Configuration when it moved, so a maxGrpcMessageSize set on a Configuration instance stopped being enforced over HTTP/2 while HTTP/1.1 carried on honouring it
  • decompressing a request body removes the content-encoding header, so an expectation matching on that header stopped matching — HTTP/1.1 has long kept a copy of the headers Netty removes, and the stream pipeline now does the same

The second of those was only visible once the test command was corrected: mvn test excludes *IntegrationTest, so it runs about 1218 tests; the real gate runs 2257. The regression sat inside that gap.

What changes for users

One thing, and it is called out in the changelog: over HTTP/2, closeSocket / closeChannel now ends only the requesting stream instead of tearing down the connection and every concurrent request on it. That is the correct semantic when a connection carries many requests at once, and it is what the same expectation already did over HTTP/1.1. If you want the connection genuinely aborted, the resetMidResponse chaos fault still does that — and now does it properly rather than degrading into a stream reset.

Everything else is a fix for something that was silently broken. Requests match the same way, responses come back the same way, expectations are written the same way, and HTTP/1.1 and HTTP/3 are untouched.

Performance

Honestly: not yet measured under concurrency. A channel per stream allocates more than a shared pipeline did, and that question deserves a real answer rather than a guess. Two attempts to measure it locally failed — the first produced a physically impossible result (a million requests a second, a 95 nanosecond p95) because it counted requests it had assumed rather than completed, and recommended shipping on the strength of it. A self-validating benchmark now runs on the perf pipeline (dbdbec5de): it records throughput and percentiles across 1, 10 and 100 concurrent streams, never gates a build on the numbers, and fails loudly if the measurement itself cannot be trusted. A threshold will follow once there is enough history to set one against something real.

jamesdbloom avatar Sep 11 '26 06:09 jamesdbloom

I will take this issue. Please assign it to me.

The current implementation uses a shared connection channel with stream identity in headers. The goal is to migrate to Http2MultiplexHandler, where each stream is a child channel. I will start by examining the PortUnificationHandler.java at line 447, as noted in the TODO. The first step is to replace the existing stream handling logic with Http2MultiplexHandler. I will ensure that the stream identity is correctly managed by the child channels, removing the need for x-http2-stream-id headers and related handlers. This should simplify the architecture and prevent mis-routing issues.

Sorry @modelpath-dev, I didn't see your comment until after I had already started working on a solution, so this is now done. Happy to leave the next similar issue to you.

jamesdbloom avatar Sep 12 '26 06:09 jamesdbloom

@jamesdbloom no worries at all, and thanks for writing up the six fixes, that was a good read. Happy to take the next one like this.

modelpath-dev avatar Oct 24 '26 09:10 modelpath-dev