Skip to content

fix(core): give a queued write a deadline, and the deadline a teardown (#223, #224, #225) - #228

Merged
EdmondDantes merged 2 commits into
mainfrom
223-outbound-write-deadline
Aug 22, 2026
Merged

EdmondDantes merged 2 commits into
mainfrom
223-outbound-write-deadline

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Closes #223, #224, #225.

The deadline (#223)

http_write_timer_arm had three callers: the two awaited senders and
http1_stream.c:335. None of the batched senders armed anything, and every
plaintext HTTP/2 response goes out through them — as does every fire-and-forget
HTTP/1 one since #209 and #211. A peer that stopped reading without closing
wedged the writev: out_in_flight stayed set, http_connection_destroy
deferred on it, and the deadline sweep re-deferred on every tick, so the
connection, its io and its read buffer were never reclaimed.

The deadline is armed at each submit, including the chained ones, so it measures
a write that stopped making progress rather than the lifetime of a whole
response. The arm in http1_stream.c goes: the sender under it does it now.

Two defects came out of making the timer fire on the common path.

The timer closed the io itself. The reactor frees a handle only when the
close and the dispose come as a pair from one place, and that place is
http_connection_destroy. Arriving second, it found the io already closed, its
own ZEND_ASYNC_IO_CLOSE became a no-op, and the handle stayed in the reactor's
table for good: one 520-byte async_io_t per timed-out connection, five for
five with five connections. Measured with an external client so no PHP stream of
the test's own is involved. This is not new — on main a streamed HTTP/1
response to a non-reading peer leaks the same handle, because that path arms the
deadline through the awaited sender. A buffered one does not, which is why
arming the batched senders is what made it visible.

The timer now marks the connection, wakes whoever is parked on the outbound
path, and asks for the destroy. The destroy owns the close. For it to run at all
with the write still outstanding, the out_in_flight deferral is lifted when
the deadline has fired — the completion that would lift it is precisely what did
not arrive.

Nothing asked HTTP/2 for a teardown. The stream that timed out ends and the
connection stays, for multiplexing it can no longer do. The h2 stream dispose
also re-checks the deferred destroy after dropping its pin, which its three
sibling sites already did.

The writev completion read the connection before validating it, which the lifted
deferral turns into a use-after-free; it validates first now, and the destroy
fires the writev's user callback itself so the body references are released
while the strategy that owns them is still there.

The last frames (#225)

http2_session_emit skips while a writev is outstanding and leaves the re-drive
to the completion — which a path about to destroy the connection will not be
there for. http2_session_emit_now runs the same pump and queues behind that
write instead. The write completion now asks the session for what it still holds
before finalising a deferred destroy, with destroy_pending cleared first so a
synchronous submit failure completing from inside the emit cannot destroy under
the frame. A session whose feed answered with a teardown is latched
emit_stopped, so the initial SETTINGS a bad preface leaves queued still do not
follow the GOAWAY out.

One cursor per session (#224)

The TLS hybrid emit picks DRAIN (mem_send) or GATHER (session_send) per pass
from large_streams_pending, and DRAIN parks the tail of a slice nghttp2 already
counts as sent. GATHER then resumes from the next frame. A parked slice pins the
mode now.

Evidence

tests/phpt/server/h2/060-h2-write-deadline-reclaims.phpt: a peer that stops
reading with setReadTimeout(30) and setWriteTimeout(1); the connection gauge
drops within the write deadline, which the read one cannot explain. It guards
the handle leak too — run-tests fails the test on it.

#224 and #225 are carried without tests, and both say why: forcing the TLS mode
to flip with a slice parked needs a fault-injection hook, and #225's window needs
a peer that half-closes with a write in flight.

470 phpt, 446 passed, 0 failed, 0 warned, 24 skipped; ctest 16 of 16. The
bad-preface and protocol-error wires are byte-identical to main.

@EdmondDantes
EdmondDantes merged commit a81bc68 into main Aug 22, 2026
7 of 8 checks passed
@EdmondDantes
EdmondDantes deleted the 223-outbound-write-deadline branch August 22, 2026 14:13
@github-actions

github-actions Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 82.74% → 82.81% (+0.07 pp)

File Baseline Current Δ Touched
src/core/http_connection.c 78.01% 78.82% +0.80 pp ●
src/core/http_connection.h 81.82% 81.82% +0.00 pp ●
src/http1/http1_stream.c 79.13% 79.02% -0.10 pp ●
src/http2/http2_strategy.c 84.36% 84.85% +0.48 pp ●
src/http2/http2_stream.c 94.00% 98.00% +4.00 pp
src/http3/http3_callbacks.c 82.42% 83.39% +0.96 pp
src/http3/http3_listener.c 77.33% 76.88% -0.45 pp
src/http3/http3_packet.c 90.43% 85.22% -5.22 pp
src/http_server_config.c 92.48% 92.42% -0.06 pp
src/websocket/ws_session.c 89.48% 89.85% +0.37 pp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A queued plaintext write has no deadline, so a peer that stops reading holds the connection forever

1 participant