Skip to content

HTTP/2 over TLS: the hybrid emit alternates two drain cursors over one session #224

Closed

Description

@EdmondDantes

What the code does

http2_session_emit picks the TLS emit mode per pass
(src/http2/http2_strategy.c:1525-1537): H2_EMIT_HYBRID takes the DRAIN path
when session->large_streams_pending == 0 and the GATHER path otherwise. The two
drive nghttp2 through different APIs on the same session:

  • DRAIN — h2_emit_flush_tls_drain → http2_session_drain →
    nghttp2_session_mem_send;
  • GATHER — h2_emit_flush_tls → nghttp2_session_send.

http2_session_drain parks the tail of a slice it could not fit in
session->send_pending / send_pending_offset
(src/http2/http2_session.c:1440-1442, :1460-1462), and resumes from there on
its next call. nghttp2_session_send knows nothing about that field.

nghttp2 states the rule in nghttp2.h: the pointer nghttp2_session_mem_send
assigns is valid until the next call of nghttp2_session_mem_send() or
nghttp2_session_send(), and the caller must send all of it before the next
chunk.

The DRAIN buffer is 16649 bytes, so a 16 KiB DATA frame plus its header does not
fit whole and a partial send_pending is the normal case, not the edge one.
large_streams_pending crossing zero between two passes is what switches modes.

What follows

The bytes parked in send_pending are never emitted: the GATHER pass resumes
from the next frame. The peer receives a truncated frame followed by a
well-formed frame header — the same shape #219 removed from the plaintext read
path.

Fix

One cursor per session. Either give the TLS DRAIN path a buffer it can always
finish from, or make the mode sticky for the life of a session, or route both
through nghttp2_session_send. Which one depends on why HYBRID exists, which is
a measurement question — the enum's own comment says GATHER is there for large
streams.

Found while reviewing #219 (PR 220). Not reproduced: it needs a TLS session
whose large_streams_pending crosses zero between two emit passes with a
partial slice parked.

Activity

  1. added this to the Next3 milestone on Aug 22, 2026
  2. added a commit that references this issue on Aug 22, 2026
  3. self-assigned this
    on Aug 22, 2026
  4. added
    bug
    on Aug 22, 2026
  5. EdmondDantes commented on Aug 23, 2026

    @EdmondDantes
    ContributorAuthor

    Fixed in 18f51b2: a parked slice pins the emit mode — http2_session_has_pending_send(session) is the first term of the DRAIN test (src/http2/http2_strategy.c:1558), so GATHER cannot step past bytes DRAIN left in send_pending. Still owed a test; the CHANGELOG entry says so, and the fault-injection hook it needs has a home in HTTP_SERVER_TEST_HOOKS.

  6. added a commit that references this issue on Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions