Skip to main content

trillium_http/h2/
acceptor.rs

1//! HTTP/2 driver loop ([`H2Driver`]) — owns the per-connection TCP transport and runs the
2//! poll-based state machine that demuxes frames, dispatches stream-opens to handler tasks, and
3//! pumps responses back out.
4//!
5//! Created by [`H2Connection::run`]. The runtime adapter calls [`H2Driver::next`] in a
6//! loop (or drives via the [`Stream`] impl, which has the same semantics); each yield either
7//! returns the next opened request stream (a [`Conn`] for the runtime to spawn a handler
8//! task against) or `None` when the connection is closed.
9//!
10//! The driver is a poll-based state machine, not an async fn. A single `drive` call is the
11//! unit of forward progress: it picks up conn-task signals, advances any in-flight response
12//! sends, drains pending outbound bytes, and advances the read cursor — parking with
13//! cancel-safe partial state when no further progress can be made.
14//!
15//! # Module layout
16//!
17//! Driver impl is split across this file and child modules to keep each focused:
18//!
19//! - **`acceptor.rs`** (this file): struct definition, the [`Self::drive`] orchestration loop, I/O
20//!   read primitives (`poll_fill_to`, `poll_drain_peer`), and the supporting enums
21//!   ([`DriverState`], [`ReadPhase`], [`CloseOutcome`], [`Action`], [`StreamEntry`]).
22//! - **`acceptor::closed_streams`**: bounded ledger of recently-closed streams + reasons, consulted
23//!   to pick the right §5.1 error category for stale peer frames.
24//! - **`acceptor::handler_signals`**: conn-task → driver work-pickup boundary. Owns the
25//!   `needs_servicing` mailbox protocol — `service_handler_signals`, `pick_up_new_client_streams`,
26//!   `has_pending_handler_signals`.
27//! - **`acceptor::outbound`**: outbound write/flush plumbing and `queue_*` frame helpers.
28//! - **`acceptor::recv`**: receive side — frame reader, dispatch, HEADERS+CONTINUATION
29//!   accumulation, malformed-request `RST_STREAM`, DATA routing into per-stream recv rings.
30//! - **`acceptor::send`**: send pump — picks up [`SendCursor`][send::SendCursor]s from the
31//!   conn-task signal pickup, frames HEADERS / DATA / trailing-HEADERS, signals completion.
32//!
33//! [`H2Connection::run`]: super::H2Connection::run
34//! [`Stream`]: futures_lite::stream::Stream
35
36mod closed_streams;
37mod constants;
38mod handler_signals;
39mod outbound;
40mod recv;
41mod send;
42#[cfg(test)]
43mod tests;
44mod types;
45
46use super::{
47    H2Error, H2ErrorCode, connection::H2Connection, frame::FRAME_HEADER_LEN, role::Role,
48    stream_state::StreamEvent, transport::H2Transport,
49};
50use crate::{
51    Conn,
52    headers::hpack::{HpackDecoder, HpackEncoder},
53};
54use closed_streams::{ClosedReason, ClosedStreams};
55use constants::{
56    INITIAL_CONNECTION_RECV_WINDOW, MAX_BUFFER_SIZE, MAX_DATA_CHUNK_SIZE, MAX_FLOW_CONTROL_WINDOW,
57};
58use futures_lite::io::{AsyncRead, AsyncWrite};
59use recv::PendingHeaders;
60use std::{
61    collections::BTreeMap,
62    future::Future,
63    io,
64    pin::Pin,
65    sync::Arc,
66    task::{Context, Poll, ready},
67};
68use swansong::ShuttingDown;
69use types::{
70    AcceptorConfig, Action, CloseOutcome, DriverState, Next, ReadPhase, StreamEntry, frame_slice,
71};
72
73/// Owns the per-connection TCP transport and drives the HTTP/2 demux loop.
74///
75/// See the module docs for the high-level driver shape and how its impl is split across the
76/// `recv` and `send` child modules.
77#[derive(Debug)]
78pub struct H2Driver<T> {
79    connection: Arc<H2Connection>,
80    transport: T,
81
82    /// Role this driver runs in — see [`Role`]. Consulted at role-asymmetric branch points
83    /// (preface direction, HEADERS-on-unknown-id, HEADERS-on-known-id).
84    role: Role,
85
86    /// Overall lifecycle position of the driver.
87    state: DriverState,
88
89    /// Future that resolves when the shared `Swansong` begins shutdown. Polled each
90    /// `drive` tick while the driver is running; on resolution the driver queues a
91    /// GOAWAY and transitions to `Closing`, after which the top-of-loop guard returns
92    /// early and we never poll this again on the same acceptor.
93    shutting_down: ShuttingDown,
94
95    /// Inbound byte cursor. Accumulates bytes from the transport across `drive` calls so
96    /// a partial frame read can survive a return to `Poll::Pending`. Always contains
97    /// exactly the bytes of the current frame being accumulated (header, then payload);
98    /// reset after each complete frame is dispatched.
99    read_buf: Vec<u8>,
100    read_filled: usize,
101    read_phase: ReadPhase,
102
103    /// Outbound byte cursor. The driver encodes control frames into `write_buf` and drains
104    /// to the transport via `poll_flush_outbound`. `write_cursor` is the offset of the
105    /// first byte not yet accepted by `poll_write`. After the buffer fully drains, both
106    /// fields are reset and a flush is issued.
107    write_buf: Vec<u8>,
108    write_cursor: usize,
109    write_flush_pending: bool,
110
111    /// HPACK decoder state, shared across all header blocks on this connection.
112    hpack: HpackDecoder,
113
114    /// HPACK encoder state. The driver is the sole owner — handlers / conn tasks
115    /// no longer touch it, so this is a plain field with no synchronization.
116    hpack_encoder: HpackEncoder,
117
118    /// Per-stream state, keyed by stream id. Driver-only — handler tasks hold their own
119    /// `Arc<StreamState>` via [`H2Transport`] and don't consult this table. The entry
120    /// bundles the shared state with driver-private bookkeeping (e.g. "have we already
121    /// advertised the recv window after seeing `is_reading`?").
122    ///
123    /// A `BTreeMap` (not a hash map) so the send pump iterates streams in ascending
124    /// stream-id order. For the client role this is load-bearing: a client MUST send
125    /// opening HEADERS in monotonically increasing stream-id order (RFC 9113 §5.1.1),
126    /// and concurrent `open_stream` calls would otherwise let the pump frame a higher
127    /// id before a lower one, drawing a `GOAWAY(PROTOCOL_ERROR)` from the peer. (See
128    /// also the allocate-under-`streams_lock` ordering in `open_stream`.)
129    streams: BTreeMap<u32, StreamEntry>,
130
131    /// Highest peer-initiated stream id seen so far. Peer-initiated (client) stream ids
132    /// must be odd and strictly increasing.
133    last_peer_stream_id: u32,
134
135    /// Accumulator for an in-progress HEADERS block that is waiting on further CONTINUATION
136    /// frames. `None` outside a HEADERS block. The spec forbids any frame on any stream
137    /// from interleaving while this is `Some`.
138    pending_headers: Option<PendingHeaders>,
139
140    /// Set once the driver decides to close: graceful (peer GOAWAY / server swansong / peer
141    /// EOF) or erroring (protocol violation → GOAWAY with code, or I/O failure → no
142    /// GOAWAY). `drive` completes (returns `None` or a final `Some(Err(...))`) once
143    /// outbound drains to empty.
144    close_outcome: Option<CloseOutcome>,
145
146    /// Set after `drive` yields its terminal result. Subsequent calls return `None` without
147    /// touching the transport.
148    finished: bool,
149
150    /// Reusable scratch the send pump reads body chunks into before framing as DATA.
151    /// Sized at [`MAX_DATA_CHUNK_SIZE`] — even if the peer permits larger frames we cap our
152    /// DATA emissions here to bound per-connection memory.
153    body_scratch: Vec<u8>,
154
155    /// Connection-level send flow-control window. Tracked as [`i64`] for symmetry with the
156    /// per-stream windows, which a mid-connection `INITIAL_WINDOW_SIZE` reduction can drive
157    /// temporarily negative; the connection window itself is *not* affected by
158    /// `SETTINGS_INITIAL_WINDOW_SIZE`. Decremented as we emit DATA; incremented by peer
159    /// `WINDOW_UPDATE(stream_id=0, inc)`. Overflow past [`MAX_FLOW_CONTROL_WINDOW`] is a
160    /// connection-level `FLOW_CONTROL_ERROR`.
161    connection_send_window: i64,
162
163    /// Connection-level recv flow-control window. Starts at the spec's baseline of 65535
164    /// octets and is raised to the configured `h2_initial_connection_window_size` via an
165    /// initial `WINDOW_UPDATE(0)` right after SETTINGS — the spec forbids SETTINGS from altering
166    /// it, so WU is the only path. Decremented as peer DATA frames arrive (across all
167    /// streams); incremented as the handler-task-side consumption signal is picked up and
168    /// we emit `WINDOW_UPDATE(0, consumed)`. A negative value means the peer overran the
169    /// window — connection-level `FLOW_CONTROL_ERROR`.
170    connection_recv_window: i64,
171
172    /// Bounded ledger of recently-closed streams and why they closed. Consulted by
173    /// [`recv::H2Driver::finalize_headers`] when a HEADERS frame arrives on an id ≤
174    /// `last_peer_stream_id` that's not in the active map, to distinguish `RST_STREAM`-
175    /// closed (stream-level `STREAM_CLOSED`) from `END_STREAM`-closed or never-opened
176    /// (connection-level). See [`ClosedStreams`] for the eviction policy.
177    closed_streams: ClosedStreams,
178
179    /// Snapshot of the h2-relevant fields of [`HttpConfig`][crate::HttpConfig] taken at
180    /// acceptor construction. Copied in because `HttpConfig` is per-server but an acceptor
181    /// is per-connection — the config is effectively immutable over a connection's
182    /// lifetime, and a local copy avoids reaching through [`H2Connection::context`] on
183    /// every policy check.
184    ///
185    /// [`H2Connection::context`]: super::H2Connection::context
186    pub(super) config: AcceptorConfig,
187}
188
189impl<T> H2Driver<T>
190where
191    T: AsyncRead + AsyncWrite + Unpin + Send,
192{
193    pub(super) fn new(connection: Arc<H2Connection>, transport: T, role: Role) -> Self {
194        let shutting_down = connection.swansong().shutting_down();
195        let context = connection.context();
196        let config = AcceptorConfig::from_http_config(context.config());
197        let hpack_encoder = HpackEncoder::new(
198            context.observer.clone(),
199            context.config.dynamic_table_capacity(),
200            context.config.recent_pairs_size(),
201        );
202        Self {
203            connection,
204            transport,
205            role,
206            state: DriverState::AwaitingPreface,
207            shutting_down,
208            read_buf: vec![0u8; FRAME_HEADER_LEN],
209            read_filled: 0,
210            read_phase: ReadPhase::NeedHeader,
211            write_buf: Vec::new(),
212            write_cursor: 0,
213            write_flush_pending: false,
214            hpack: HpackDecoder::new(config.hpack_table_capacity()),
215            hpack_encoder,
216            streams: BTreeMap::new(),
217            last_peer_stream_id: 0,
218            pending_headers: None,
219            close_outcome: None,
220            finished: false,
221            body_scratch: vec![0u8; MAX_DATA_CHUNK_SIZE as usize],
222            connection_send_window: INITIAL_CONNECTION_RECV_WINDOW,
223            connection_recv_window: INITIAL_CONNECTION_RECV_WINDOW,
224            closed_streams: ClosedStreams::default(),
225            config,
226        }
227    }
228
229    /// The shared [`H2Connection`] this acceptor was created from.
230    pub fn connection(&self) -> &Arc<H2Connection> {
231        &self.connection
232    }
233
234    /// Drive the connection until the next request stream opens, the connection ends, or a
235    /// fatal protocol or I/O error occurs.
236    ///
237    /// Returns `Ok(Some(conn))` for each new request stream — the runtime adapter is
238    /// expected to spawn a handler task that consumes the [`Conn`]. Malformed requests are
239    /// handled internally with a stream-level `RST_STREAM` and never surfaced. Returns
240    /// `Ok(None)` when the connection has been shut down cleanly (peer GOAWAY, our own
241    /// swansong shutdown, peer EOF at a frame boundary).
242    ///
243    /// # Errors
244    ///
245    /// The returned future resolves to an [`H2Error`] for any *connection-level* protocol
246    /// violation detected while decoding peer frames or for an unrecoverable transport I/O
247    /// error. A final GOAWAY is sent before a protocol error is returned (best-effort; I/O
248    /// errors skip it).
249    // Mirrors `StreamExt::next` (a `&mut self -> impl Future<Output = Option<T>>` adapter),
250    // not `Iterator::next`. The driver is also `Stream`, so callers can use either.
251    #[allow(clippy::should_implement_trait)]
252    pub fn next(&mut self) -> Next<'_, T> {
253        Next { driver: self }
254    }
255
256    /// Poll-based driver core. Shared by [`Next`]'s `Future` impl, the [`Stream`] impl on
257    /// [`H2Driver`], and [`H2Initiator`][super::H2Initiator]'s client-side Future impl.
258    ///
259    /// [`Stream`]: futures_lite::stream::Stream
260    #[allow(
261        clippy::too_many_lines,
262        reason = "state-machine orchestration; splitting muddies the read-as-a-recipe shape"
263    )]
264    pub(super) fn drive(
265        &mut self,
266        cx: &mut Context<'_>,
267    ) -> Poll<Option<Result<Conn<H2Transport>, H2Error>>> {
268        if self.finished {
269            return Poll::Ready(None);
270        }
271
272        for loop_number in 0..self.config.copy_loops_per_yield() {
273            log::trace!("h2 drive loop number: {loop_number}");
274            // 1. Conn-task signals. Picks up window-update intent (`is_reading`) and new
275            //    `submit_send` submissions, moving them into driver-private state.
276            self.service_handler_signals();
277
278            // 2. Send pump. Turns picked-up SendCursors into HEADERS / DATA / trailing- HEADERS
279            //    frame bytes in `write_buf`. Body reads that return Pending leave the cursor in
280            //    place — the body's source will wake the driver task.
281            self.advance_outbound_sends(cx);
282
283            // 3. Flush any pending outbound — never re-poll reads when we still owe bytes to the
284            //    peer, and never signal closure to the caller before the wire is clean.
285            match self.poll_flush_outbound(cx) {
286                Poll::Ready(Ok(())) => {}
287                Poll::Ready(Err(e)) => {
288                    // Flush failure while closing: just take whatever outcome we had and
289                    // shelve the fresh I/O error. While running, record and finish.
290                    if self.close_outcome.is_none() {
291                        self.close_outcome = Some(CloseOutcome::Io(e));
292                    }
293                    return Poll::Ready(self.finish_with_current_outcome());
294                }
295                Poll::Pending => return Poll::Pending,
296            }
297
298            // 4. If we were closing, outbound is now drained. For graceful (or protocol-error)
299            //    shutdowns, transition to `Drained` and wait for the peer to close its write half —
300            //    otherwise the peer sees our drop as a reset rather than a clean close. For
301            //    I/O-error shutdowns the transport is already untrustworthy, so skip the drain.
302            //    Defer the transition while in-flight streams still have outbound (an active
303            //    SendCursor or queued parts), an open send half (a handler that hasn't submitted
304            //    its response yet — half-closed-remote is *not* drained), OR inbound (recv half not
305            //    yet closed) work. Without this, a handler that submits trailers *after* the
306            //    cancellation race resolves gets stranded with bytes parked in mailboxes; a handler
307            //    that hasn't responded yet when shutdown begins has its response `SubmitSend`
308            //    orphaned by a driver that finished out from under it; and a client receiving
309            //    GOAWAY mid-stream stops decoding incoming frames before the server's trailing
310            //    HEADERS arrive. Falls through to step 6 so the recv pump (also gated on
311            //    Running|Closing now) keeps running and parks on the transport read waker rather
312            //    than the outbound-only `park` here.
313            if self.state == DriverState::Closing {
314                if matches!(self.close_outcome, Some(CloseOutcome::Io(_))) {
315                    return Poll::Ready(self.finish_with_current_outcome());
316                }
317                if self.has_active_send_cursors()
318                    || self.has_open_send_half()
319                    || self.has_pending_recv()
320                {
321                    self.log_closing_blockers();
322                } else {
323                    self.set_state(
324                        DriverState::Drained,
325                        "outbound drained, no in-flight streams",
326                    );
327                }
328            }
329
330            // 5. Server-initiated shutdown check. Only relevant while we're running — once we're
331            //    past the Closing/Drained transition we've already committed to a close and
332            //    re-observing the swansong here would re-enter begin_close in a loop. Post-shutdown
333            //    re-polls of `ShuttingDown` are harmless themselves (event_listener-backed, not
334            //    single-shot) but the re-entry isn't.
335            if self.state == DriverState::Running
336                && Pin::new(&mut self.shutting_down).poll(cx).is_ready()
337            {
338                self.begin_close(CloseOutcome::Graceful);
339                continue;
340            }
341
342            // 6. State-specific step.
343            match self.state {
344                DriverState::AwaitingPreface => {
345                    // Role-asymmetric: server reads the 24-byte preface off the wire; client
346                    // writes it to `write_buf` (the next drain tick flushes it, then our
347                    // SETTINGS, then the peer's SETTINGS arrives as the first frame in Running).
348                    let poll = match self.role {
349                        Role::Server => self.poll_read_preface(cx),
350                        Role::Client => {
351                            self.queue_client_preface();
352                            Poll::Ready(Ok(()))
353                        }
354                    };
355                    match poll {
356                        Poll::Ready(Ok(())) => {
357                            self.set_state(DriverState::NeedsServerSettings, "preface complete");
358                        }
359                        Poll::Ready(Err(e)) => {
360                            self.close_outcome = Some(e);
361                            return Poll::Ready(self.finish_with_current_outcome());
362                        }
363                        Poll::Pending => {
364                            if self.park(cx) {
365                                return Poll::Pending;
366                            }
367                        }
368                    }
369                }
370
371                DriverState::NeedsServerSettings => {
372                    self.queue_settings();
373                    // The spec forbids SETTINGS from altering the connection-level
374                    // flow-control window — it stays at the 65535 baseline unless we raise
375                    // it via `WINDOW_UPDATE(0)`. Do that immediately after SETTINGS so peer
376                    // bulk uploads aren't capped at ~5 Mbit/s × RTT.
377                    let raise = i64::from(self.config.initial_connection_window_size())
378                        - INITIAL_CONNECTION_RECV_WINDOW;
379                    if raise > 0 {
380                        let raise = u32::try_from(raise).unwrap_or(u32::MAX);
381                        self.queue_window_update(0, raise);
382                        self.connection_recv_window += i64::from(raise);
383                    }
384                    self.set_state(DriverState::Running, "initial SETTINGS queued");
385                }
386
387                // Read pump runs in both Running and Closing so a Closing-side driver
388                // (we sent or received GOAWAY) keeps decoding inbound frames for streams
389                // that haven't reached recv-closed yet — e.g. trailing HEADERS for an
390                // in-flight server-stream the peer is about to send. New `Action::Emit`
391                // streams are ignored in Closing: post-GOAWAY the peer shouldn't be
392                // opening new ones (and we wouldn't want to dispatch handlers for them
393                // even if it did).
394                DriverState::Running | DriverState::Closing => match self.poll_advance_read(cx) {
395                    Poll::Ready(Ok(Action::Continue)) => {}
396                    Poll::Ready(Ok(Action::Emit(conn))) => {
397                        if self.state == DriverState::Running {
398                            return Poll::Ready(Some(Ok(*conn)));
399                        }
400                        // Closing — drop the conn; outer loop continues processing
401                        // remaining in-flight streams until drained.
402                    }
403                    Poll::Ready(Ok(Action::Close(outcome))) => {
404                        self.begin_close(outcome);
405                    }
406                    // Protocol errors need a GOAWAY on the wire before we terminate;
407                    // `begin_close` queues that and transitions us to Closing so the next
408                    // outer-loop iteration drains the frame. Io errors short-circuit:
409                    // if we're already Closing, the transport is gone, so finish without
410                    // looping forever waiting for in-flight streams (`has_pending_recv`
411                    // can't decide on its own that the peer is never sending again).
412                    Poll::Ready(Err(e)) => {
413                        if self.state == DriverState::Closing {
414                            self.close_outcome.get_or_insert(e);
415                            return Poll::Ready(self.finish_with_current_outcome());
416                        }
417                        self.begin_close(e);
418                    }
419                    Poll::Pending => {
420                        if self.park(cx) {
421                            return Poll::Pending;
422                        }
423                    }
424                },
425
426                DriverState::Drained => match self.poll_drain_peer(cx) {
427                    Poll::Ready(()) => {
428                        return Poll::Ready(self.finish_with_current_outcome());
429                    }
430                    Poll::Pending => return Poll::Pending,
431                },
432            }
433        }
434
435        // Cooperative yield: we made `copy_loops_per_yield` rounds of progress without
436        // hitting an internal Pending. Re-arm immediately and let the runtime pick up
437        // anything else it has waiting before we resume.
438        cx.waker().wake_by_ref();
439        Poll::Pending
440    }
441
442    /// Register the driver's waker with the shared `outbound_waker` (so handler tasks can
443    /// wake the driver) and tell the caller whether it's safe to park. Returns `true` if
444    /// the driver should return `Poll::Pending`, or `false` if a handler produced work
445    /// between our last check and the registration — in which case the caller should loop
446    /// around to pick it up.
447    fn park(&mut self, cx: &mut Context<'_>) -> bool {
448        self.connection.outbound_waker().register(cx.waker());
449        !self.has_pending_handler_signals() && !self.has_pending_outbound_progress()
450    }
451
452    /// Convert the current `close_outcome` into the terminal return of [`Self::drive`]. Must
453    /// only be called after outbound bytes have been flushed. Graceful closes return `None`;
454    /// errors surface as a final `Some(Err(...))` before subsequent polls return `None`.
455    fn finish_with_current_outcome(&mut self) -> Option<Result<Conn<H2Transport>, H2Error>> {
456        self.finished = true;
457        // Complete every outstanding `H2Connection::send_ping` future with an error so
458        // awaiting callers don't block forever. Safe to call regardless of outcome —
459        // a no-op if no pings are in flight.
460        self.connection.fail_pending_pings(
461            io::ErrorKind::ConnectionAborted,
462            "h2 connection closed before PING ACK",
463        );
464        // Wake any `PeerSettings` waiters so a peer that disconnects without ever sending
465        // SETTINGS doesn't strand them. Their `poll` rechecks swansong state and returns
466        // Ready; the caller's follow-up operation surfaces the connection-closed error.
467        self.connection.wake_peer_settings_waiters();
468        // Resolve every still-live stream's recv-side waiters. A connection that dies with
469        // an in-flight stream (server GOAWAY + close, peer FIN, I/O error) leaves any task
470        // parked on the response — `response_headers`, a body `poll_read`, an upgrade
471        // `poll_write` — with no other wake source. Without this a client request hangs
472        // forever on a graceful server shutdown. Mirror the per-stream RST teardown:
473        // terminal `Reset` (recv reports eof → `ResponseHeaders` yields `ConnectionAborted`,
474        // reads return EOF, writes `BrokenPipe`) + the same waker fan-out.
475        let reset_code = match &self.close_outcome {
476            Some(CloseOutcome::Protocol(code)) => *code,
477            _ => H2ErrorCode::NoError,
478        };
479        for entry in self.streams.values() {
480            // Move each still-live stream to `Closed{Reset}` (a no-op on streams already closed, so
481            // an existing reason isn't clobbered), then fan out every recv/send waker so parked
482            // tasks observe the close instead of hanging.
483            let _ = entry.shared.apply_event(StreamEvent::RecvReset(reset_code));
484            entry.shared.recv.waker.wake();
485            entry.shared.recv.response_headers_waker.wake();
486            entry.shared.send.outbound_write_waker.wake();
487            // A handler already parked in `SubmitSend` (response staged, awaiting the driver to
488            // frame it) needs this wake to re-poll and observe the now-reset stream — the recv
489            // fan-out above doesn't reach the send-completion waiter.
490            entry.shared.send.completion_waker.wake();
491        }
492        match self.close_outcome.take() {
493            None | Some(CloseOutcome::Graceful) => None,
494            Some(CloseOutcome::Protocol(code)) => Some(Err(H2Error::Protocol(code))),
495            Some(CloseOutcome::Io(e)) => Some(Err(H2Error::Io(e))),
496        }
497    }
498
499    /// Enter the closing state: record the outcome and queue a GOAWAY (only for outcomes
500    /// that warrant one). The main loop will drain `write_buf` and then finish.
501    fn begin_close(&mut self, outcome: CloseOutcome) {
502        // Idempotent: with the recv pump now running in Closing (so we keep
503        // decoding inbound frames for in-flight streams across GOAWAY), a peer
504        // GOAWAY arriving after we've already begun closing would otherwise
505        // re-queue our own GOAWAY and re-enter Closing, ping-ponging forever
506        // with a peer that mirrors the behavior.
507        if self.state == DriverState::Closing || self.state == DriverState::Drained {
508            log::trace!(
509                "h2 driver: begin_close({outcome:?}) — already in {:?}, ignoring",
510                self.state,
511            );
512            return;
513        }
514        // Don't overwrite a prior outcome (e.g. if an error fires in the middle of a
515        // graceful shutdown, keep the error).
516        let code = match &outcome {
517            CloseOutcome::Graceful => Some(H2ErrorCode::NoError),
518            CloseOutcome::Protocol(code) => Some(*code),
519            CloseOutcome::Io(_) => None,
520        };
521        let reason = match &outcome {
522            CloseOutcome::Graceful => "graceful close",
523            CloseOutcome::Protocol(_) => "protocol error",
524            CloseOutcome::Io(_) => "i/o error",
525        };
526        if self.close_outcome.is_none() {
527            self.close_outcome = Some(outcome);
528        }
529        if let Some(code) = code {
530            self.queue_goaway(self.last_peer_stream_id, code);
531        }
532        self.set_state(DriverState::Closing, reason);
533    }
534
535    /// The sole mutator of `self.state`. Logs every transition so a trace log reads as
536    /// a sequence of named lifecycle events.
537    fn set_state(&mut self, new: DriverState, reason: &'static str) {
538        if self.state == new {
539            return;
540        }
541        log::trace!(
542            "h2 driver: state {old:?} → {new:?} ({reason})",
543            old = self.state,
544        );
545        self.state = new;
546    }
547
548    /// Log which in-flight streams are blocking the `Closing → Drained` transition.
549    /// Called from the closing-state check when at least one predicate (`has_active_send_cursors`
550    /// or `has_pending_recv`) is still true, so a trace log shows exactly which streams the
551    /// driver is waiting on.
552    fn log_closing_blockers(&self) {
553        if !log::log_enabled!(log::Level::Trace) {
554            return;
555        }
556        for (id, entry) in &self.streams {
557            let lifecycle = *entry.shared.lifecycle_lock();
558            let queued = !entry
559                .shared
560                .send
561                .queue
562                .lock()
563                .expect("send queue mutex poisoned")
564                .is_empty();
565            if entry.send.is_some() || queued || !lifecycle.recv_closed() {
566                log::trace!(
567                    "h2 driver: Closing — stream {id} blocking drain (lifecycle={lifecycle:?}, \
568                     cursor_present={}, queued={queued})",
569                    entry.send.is_some(),
570                );
571            }
572        }
573    }
574
575    /// Read bytes from the transport into `read_buf[read_filled..target]` until
576    /// `read_filled >= target`. Cancel-safe: if the caller drops the Future, any bytes
577    /// already placed are preserved in the buffer.
578    ///
579    /// A 0-byte read is surfaced as `UnexpectedEof`. The caller maps this to a terminal
580    /// I/O error; we don't emit a GOAWAY on peer-initiated close.
581    fn poll_fill_to(&mut self, target: usize, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
582        if self.read_buf.len() < target {
583            self.read_buf.resize(target, 0);
584        }
585        while self.read_filled < target {
586            let n = ready!(
587                Pin::new(&mut self.transport)
588                    .poll_read(cx, &mut self.read_buf[self.read_filled..target])
589            )?;
590            if n == 0 {
591                return Poll::Ready(Err(io::Error::from(io::ErrorKind::UnexpectedEof)));
592            }
593            self.read_filled += n;
594        }
595        Poll::Ready(Ok(()))
596    }
597
598    /// Post-GOAWAY, drain whatever inbound bytes are *immediately* available from the
599    /// peer so our Drop sends a clean FIN (no unread data → no TCP RST) while the peer
600    /// sees the GOAWAY we just emitted. Read loops internally: consume each Ready chunk,
601    /// discard it, ask for more. Exits as soon as the transport returns `Pending` (no
602    /// bytes available right now) OR `Ready(0)` (peer FIN already arrived) OR any error.
603    ///
604    /// Does **not** register the waker on `Pending` — we're actively closing, not
605    /// observing the peer. A peer that happens to send more bytes after our exit will
606    /// have those bytes dropped when the transport is closed; that's a race the peer
607    /// chose to lose by sending after receiving our GOAWAY.
608    ///
609    /// Returning `Ready(())` unconditionally (no `Pending` case) lets the caller finalize
610    /// immediately. The `Poll` wrapper is kept for symmetry with the rest of the driver's
611    /// poll-style methods.
612    fn poll_drain_peer(&mut self, cx: &mut Context<'_>) -> Poll<()> {
613        // A peer flooding us with bytes could keep this loop going a long time. Cap it
614        // so a pathological client can't pin our close-out forever.
615        const MAX_DISCARD_ITERATIONS: usize = 256;
616        // Lightweight scratch — we're throwing it away. 512 balances "drain in few
617        // iterations" against "don't hold a large buffer for a rare path."
618        let mut scratch = [0u8; 512];
619        for _ in 0..MAX_DISCARD_ITERATIONS {
620            // We pass `cx` through for the benefit of the transport's `poll_read` contract,
621            // but we *interpret* `Pending` as "done draining" rather than parking on it —
622            // we're actively closing, not observing. A peer that sends more bytes after
623            // our exit loses the race.
624            match Pin::new(&mut self.transport).poll_read(cx, &mut scratch) {
625                Poll::Ready(Ok(0) | Err(_)) | Poll::Pending => {
626                    return Poll::Ready(());
627                }
628                Poll::Ready(Ok(_)) => {}
629            }
630        }
631        Poll::Ready(())
632    }
633
634    /// Look up why a stream is closed. `None` means either never-opened or evicted from the
635    /// bounded ledger — both fall through to the connection-level default.
636    pub(super) fn closed_reason(&self, stream_id: u32) -> Option<ClosedReason> {
637        self.closed_streams.reason(stream_id)
638    }
639}