Skip to content

A half-closed peer loses the rest of its response #249

Closed

Description

@EdmondDantes

What the code does

http_connection_read_callback_fn latches the connection as unwritable on any
read that ends the stream, a clean EOF included
(src/core/http_connection.c:1204):

const bool err = (exception != NULL) || (req->exception != NULL);
const ssize_t bytes_read = req->transferred;

if (UNEXPECTED(err || bytes_read <= 0)) {
    ...
    /* Where a queued write's failure becomes visible: libuv reports it at
     * completion, and a fire-and-forget completion carries no status. */
    conn->write_failed = true;

Four lines below, the deferral says what the intent is: "Without this, an EOF
from a peer that sent its last request and shut down the write half kills
mid-flight responses." The latch above defeats it — http_send_batched_finish
gates the HTTP/2 re-drive on the same flag (src/core/http_connection.c:1653),
and the HTTP/1 writer stops on it too.

A peer that half-closes has stopped sending; it has not stopped reading.

Measurement

Handler writes 4 MiB in 64 KiB chunks and calls end(). The client sends one
request, calls stream_socket_shutdown($fp, STREAM_SHUT_WR), sleeps 300 ms and
then reads to EOF. Same client without the shutdown is the control.

case half-closed control
HTTP/2, DATA bytes 65536–393216 of 4194304, no END_STREAM 4194304, END_STREAM
HTTP/1, wire bytes 110 (headers only), no terminator 4194995, terminator

The HTTP/2 figure moves with timing; the truncation does not.

The cause, isolated

One line, and the whole body arrives:

conn->write_failed = err;   /* instead of true */

With that, HTTP/2 delivers 4194304 of 4194304 with END_STREAM. It is a probe,
not the fix: the latch is what tells a handler parked on backpressure that a
peer which is fully gone will never drain, and a clean EOF cannot be told from
a full close at read time.

Two candidate fixes

  1. Latch only on a read error, and let a peer that is truly gone surface through
    the write path — the write deadline from A queued plaintext write has no deadline, so a peer that stops reading holds the connection forever #223 already tears such a
    connection down.
  2. Split the flag: a clean EOF sets "the peer sends no more" and still fires
    out_idle / out_drain so nobody parks, while write_failed keeps its
    present meaning for errors.

Which one depends on whether a fire-and-forget writev can report its failure at
all, which is a question about the reactor API rather than about this file.

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