Skip to content

H1 request rejected after linking leaves the backend stream attached: backend holds a cut request, index desyncs #1716

Closed

Description

@FlorentinDUBOIS

Summary

When the H1 frontend rejects a request after its stream is linked to a backend, the backend stream is never ended. The trailer bound of #1707 is the clearest trigger. The backend connection keeps a request cut before its trailer section. It is never closed while the client connection lives, and the backend reverse index still lists the stream.

Debug builds, which include every e2e run, panic the worker on the backend_streams invariant. Release builds leave the backend connection open, still attached to the stream slot that the next request on the same client connection reuses.

Observed on origin/main at a810211.

Cause

  • lib/src/protocol/mux/h1.rs:487-494: HttpContext::filter_request_trailers marks the front kawa in error when the trailer section goes over h2_max_header_fields. When the request arrives in several reads, the head and body were already forwarded, so the stream is Linked(token) and the backend holds everything up to 0\r\n.
  • lib/src/protocol/mux/h1.rs:534-538: the Position::Server arm answers 400 with set_default_answer. The Position::Server if stream.back.consumed arm at h1.rs:509-532 cuts the response with forcefully_terminate_answer. Neither arm ends the backend stream. The Position::Client arm just above (h1.rs:496-507) does call endpoint.end_stream(token, ...).
  • lib/src/protocol/mux/answers.rs:326 and :342: both helpers set stream.state = StreamState::Unlinked without Context::unlink_stream. The backend ConnectionH1 keeps stream = Some(id) and BackendStatus::Connected, and context.backend_streams keeps token -> [id].
  • The timeout paths in lib/src/protocol/mux/mod.rs (for example :3437 and :3544) call unlink_stream before the same helpers. The request-error arms do not.

Impact:

  • Debug and test builds: the end-of-pass check in Mux::ready_inner (lib/src/protocol/mux/mod.rs:3322) panics the worker:
    backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}.
  • Release builds:
    • The backend connection holds a half-written request and is closed only when the client session closes. The backend waits for a trailer section that never comes.
    • Its readable still parses whatever the backend sends into context.streams[id].back. That is the slot the keep-alive reset (h1.rs writable) hands to the next request on the same client connection.

This is not specific to trailers. Any frontend parse error after the stream is linked takes the same arms, for example a malformed chunk-size line in a later segment.

#1707's test_h1_trailer_field_limit_exceeded_rejected_split does not see it for two reasons:

  • Its request carries Connection: close, and the builtin 400 carries Connection: close too.
  • The session therefore returns CloseSession in the same ready_inner pass. It never reaches the invariant check, and Mux::close tears the backend down as a side effect.

A listener 400 answer without Connection: close exposes the bug. set_default_answer explicitly supports such an answer as the operator's way to keep the client connection alive.

Reproduction

This e2e test is added in the fix PR, in e2e/src/tests/h1_security_tests.rs:

  1. Configure an HTTP listener whose 400 answer is HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n.
  2. Send, keep-alive, the first segment POST /api HTTP/1.1, Host, Transfer-Encoding: chunked, 5\r\nHello\r\n0\r\n. Wait for the backend to receive it.
  3. Send a second segment with 129 trailer fields (h2_max_header_fields + 1) and the final CRLF.
  4. The client gets HTTP/1.1 400 Bad Request. The backend connection is never closed, and the worker panics:
TRAILER-LIMIT-SPLIT-BACKEND-CLOSE: backend accepted=true received 295 bytes, client got Some("HTTP/1.1 400 Bad Request"), backend_closed=false
thread '<unnamed>' panicked at lib/src/protocol/mux/mod.rs:3322:13:
assertion `left == right` failed: backend_streams index key count mismatch: expected={}, actual={Token(5): [0]}

Expected

Once a request is rejected while its stream is linked, sozu should end the backend stream, as the Position::Client arm and the timeout paths already do:

  • The backend connection is unlinked and closed. It is never returned to the keep-alive pool, even when its response was already complete, because it holds a partial request.
  • The client is answered exactly as today: 400 when no response byte has left, a cut response and a close otherwise. The client connection's keep-alive stays governed by the 400 answer's Connection header.

Activity

  1. added 3 commits that reference this issue on Sep 30, 2026
    46a19c5
    784d453
    75bbccb
  2. added a commit that references this issue on Oct 2, 2026
    cd65824
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions