Skip to content

HTTP/2: a deferred destroy discards the response frames still inside the session #225

Closed

Description

@EdmondDantes

What the code does

http_send_batched_finish (src/core/http_connection.c:1601) finalises a
deferred destroy and returns; it reaches http2_conn_notify_emit only on the
other branch:

if (UNEXPECTED(conn->destroy_pending) && conn->handler_refcount == 0) {
    conn->destroy_pending = false;
    http_connection_destroy(conn);
    return;
}

out_signal_idle(conn);
out_signal_drain(conn);
http2_conn_notify_emit(conn);

Since #219 that re-drive is how h2c output reaches the wire after an emit that
skipped a write in flight, so the order decides whether a response finishes.

Failing scenario

destroy_pending is set — the peer half-closed, and the EOF branch at
http_connection.c:1196 called http_connection_destroy, which deferred on
handler_refcount. The handler then calls end(); h2_stream_mark_ended
resumes the provider and calls http2_session_emit, which skips because a
writev is in flight, leaving the final DATA(END_STREAM) inside nghttp2. Dispose
drops handler_refcount to 0. The write completes, finish sees
destroy_pending && refcount == 0 and destroys — the END_STREAM frame is
discarded and the client waits for a body end that never comes.

Why the obvious fix is not the fix

Moving the re-drive ahead of the destroy was tried in PR 220 and reverted. Two
things break:

  1. A bad connection preface then answers GOAWAY followed by the SETTINGS the
    session had queued before BAD_CLIENT_MAGIC fired — 75 bytes where main
    sends 17. tests/phpt/server/h2/059-h2-bad-preface-goaway.phpt pins that.
  2. The re-drive can free the connection under the caller: a WRITEV_EX submit
    that fails synchronously completes from inside it, and that completion
    re-enters finish, which destroys. The outer frame then reads a freed arena
    slot. Today the re-drive is the last statement, so a destroy inside it
    returns into nothing.

So the fix has to either make the destroy wait for the session rather than the
other way round, or give the teardown paths their own emit. #219 took the second
route for the two paths it could name — http2_session_emit_now queues behind
the write in flight — and this scenario is the third.

Found while reviewing #219 (PR 220). Reachability is unproven: it needs a peer
that half-closes with a write in flight and a dispose landing in that window.

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: http_send_batched_finish re-drives http2_conn_notify_emit before finalising a deferred destroy, and defers again while out_in_flight or out_pending_len says the emit queued more (src/core/http_connection.c:1653). The two regressions PR 220 hit are avoided: destroy_pending is cleared before the emit so a synchronous submit failure cannot destroy under the frame, and the bad-preface path sends its static GOAWAY without draining the session. Still owed a test; the CHANGELOG entry says so.

  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