Skip to content

Net: a seek that cancels an HTTP/2 transfer dials a fresh connection - #1289

Merged
GLinnik21 merged 4 commits into
plxnative:mainfrom
fraygox:fix/h2-seek-fresh-connect
Nov 10, 2026
Merged

GLinnik21 merged 4 commits into
plxnative:mainfrom
fraygox:fix/h2-seek-fresh-connect

Conversation

@fraygox

@fraygox fraygox commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The media transport never sets CURLOPT_HTTP_VERSION, so a firmware libcurl of 7.62 or later negotiates
HTTP/2 with a server that offers it (the webOS 10.3 set's 7.82 does; the bundled curl in #1270 does too).
A seek removes the unfinished transfer and attaches a fresh easy handle to the same multi.

  • On HTTP/1.1 libcurl closes a connection whose transfer was removed unfinished, so the next request dials.
  • On HTTP/2 libcurl keeps the connection (from source reading: a premature multi_done does not close a
    multiplexed connection in 7.82 or 8.x
    ) and hands it to the next request. That request's bytes then wait
    behind everything the server had already queued for the cancelled stream.

Mechanism, measured in a reproduction (not on the television): Traefik v3 in front of an nginx origin, a client about 12 ms away, a link paced to about 94 Mbit/s, with ss -tin sampled in Traefik's network namespace and a packet capture. Each row covers the five seeks that reused the connection:

Server send buffer Not yet sent at the seek New data on the wire after Predicted (not sent ÷ 94 Mbit/s)
default (tcp_wmem max 32 MiB) 2670-3378 KB 236-299 ms 227-287 ms
tcp_wmem max 1 MiB 782-867 KB 70-77 ms 67-74 ms
tcp_notsent_lowat=16384 56-65 KB 2-9 ms 5-6 ms

The same reproduction with nginx serving HTTP/2 directly (no Traefik) gave seek to resume of 291 ms on HTTP/2 against 86 ms on HTTP/1.1, and 71 ms on HTTP/2 with tcp_notsent_lowat set.

Fix

CurlSource::stop() judges the handle it detaches, only if it entered the multi (an attached
flag set after curl_multi_add_handle succeeds) and did not fail before it had a connection (a
CURLcode of 5, 6, 7, 35, 51, 60 or 90: a refused connect, a failed TLS handshake, the strict
attempt that retries in key mode). It reads CURLINFO_HTTP_VERSION, or, for a handle
detached before its own response line, the last version that handle's origin answered on
(ConnLedger, kept per origin: a chain from an HTTP/1.1 server to an HTTP/2 CDN lends neither its
version). If the transfer was on HTTP/2 and did not finish cleanly (unfinished, or failed: done && !failed), it marks that origin busy. Every request this source makes to a busy origin for the next
BUSY_FOR = 120 s sets CURLOPT_FRESH_CONNECT; the first one logs
curlio: fresh connection …, the rest are silent. A new mark restarts the window; a rebuilt multi
clears the marks.

  • Why a time window, not "the next request" or a dial count. libcurl hands a request the oldest
    matching idle connection, so after one fresh dial the busy connection would be handed to the next
    request that did not dial fresh. It stops being offered once it has been idle longer than
    CURLOPT_MAXAGE_CONN: 118 s by default in 7.82 (set->maxage_conn = 118) and 8.23
    (conn_max_idle_ms = 118 * 1000), and the busy connection is idle from its cancel because nothing
    takes it again. A dial count does not hold across versions: the cache evicts its oldest idle
    connection past 4 per attached handle in 7.82, past 2 (and only if idle ≥ 1 s) in 8.23. (From
    reading both versions' source; not measured.)
    For HLS, one cancel costs a handshake per request
    to that origin for two minutes, then reuse resumes.
  • Why a failed transfer counts. In 7.82 the error paths call streamclose, which for an h2
    (PROTOPT_STREAM) handler resets the stream and keeps the connection, as a premature
    multi_done does. (From source reading.)
  • Bounded: MAX_LEDGER_ORIGINS = 8 (registered in ci/list-caps.ini as work), least
    recently used first, so the server every chain replay starts from outlives rotating CDN edges.
  • Origins compare as libcurl dials them: https://cdn/a and https://cdn:443/b are one origin,
    https://h/a and https://h:32400/b are two. A hop's Origin is carried on Hop (from the
    redirect's Target, or parsed once per chain for its first URL), not re-parsed per attempt and
    again in stop().
  • Not affected: HTTP/1.1 (already dials), HTTP/3 (QUIC cancels per stream), transfers that
    finished cleanly (nothing queued; reuse stays right for HLS segments), key mode (keypin::apply
    already sets FRESH_CONNECT and FORBID_REUSE, so the new setopt is a no-op there).
  • CURLOPT_FORBID_REUSE is not added. It would close the busy connection at removal, but it would have
    to be set on a transfer still in progress, which curl_easy_setopt's documentation calls undefined
    (from the commit message; not re-checked), and nothing measured it: the benchmarked fix set
    FRESH_CONNECT alone. Adding it is conditional on a measurement of the residual below.
  • A refused setopt is logged and the request reuses the connection (today's behaviour); a refused getinfo
    reads as an unknown version, which never dials fresh. CURLOPT_FRESH_CONNECT is net.rs's constant,
    now pub(crate).

Evidence (measured)

A wired webOS 5.6.2 set, firmware curl 7.61.0 asked for HTTP/2 (sysh2), through Traefik v3.7.17 about
10 ms away, reverse-proxying to nginx; netem for the loss row. Seek-to-resume, per-run median, paired
by round against the same set's HTTP/1.1 baseline (sys); ratio is arm / sys, 8 rounds, median with 95%
bootstrap CI. sysh2fc is the same client with CURLOPT_FRESH_CONNECT set on the seek requests. That is
the benchmark client's version of the change, not this PR's code running in the app.

Path Mbit/s sys (HTTP/1.1) sysh2 sysh2fc
no added delay 8 149 ms 3.14x [2.59, 3.53] 1.00x [0.79, 1.02]
no added delay 20 238 ms 2.22x [1.93, 2.42] 1.38x [1.01, 1.89]
+50 ms, 1% loss 8 869 ms 2.04x [0.89, 2.56] 1.67x [0.66, 1.88]

(source: analysis-play3-fc.json, 72 runs, none excluded.) Reading it:

  • Without loss the HTTP/2 penalty is clear and the fix removes it at 8 Mbit/s. At 20 Mbit/s a residual
    1.38x remains (CI lower bound 1.01); I have not determined whether it is the old connection's backlog
    still arriving. That is the open question behind FORBID_REUSE.
  • With 1% loss at +50 ms the CIs include 1.0 for both arms: no conclusion either way.
  • Client CPU per MB: sysh2fc used 0.83x (8 Mbit/s) and 0.88x (20 Mbit/s) of sys; sysh2 0.98x / 0.98x.
  • A second, earlier sample of sysh2 (analysis-play3.json, 266 runs) gave 2.98x [2.91, 3.52] (8 Mbit/s),
    2.24x [2.08, 2.41] (20 Mbit/s) and 3.22x [2.10, 9.96] (+50 ms, 1% loss, 8 Mbit/s). In that file the
    fixed arm exists only at +50 ms, 1% loss, 20 Mbit/s: 1.16x [0.90, 1.66].
  • Server-side alternative: with tcp_notsent_lowat=16384 on the server (analysis-play3-srv-lowat.json,
    5 rounds, no added delay, 8 Mbit/s), sysh2 seeks were 0.70x [0.62, 0.71] of the HTTP/1.1 baseline. That
    needs a server setting and does not help servers the user does not control; this PR is the client side.

On the television (this PR's code)

A debug build of the head below, on a webOS 10.3.0 set whose firmware libcurl 7.82.0
negotiates HTTP/2 by default, direct-playing a film from Plex through Traefik over HTTPS: a resume
and five manual seeks, back and forth. Every one logged a fresh connection and its reopen came back
ok=true in 214-531 ms; the film played on after each (event log, range offsets only):

ff: seek begin 2677s
curlio: fresh connection for range=1759380781- (a cached HTTP/2 connection to this origin carries a cancelled stream; later requests to it dial fresh for 120s, unlogged)
ff: seek_cb reopen off=1759380781 ok=true ms=346
ff: seek 2677s rv=0 ms=346
…
ff: seek begin 2557s
curlio: fresh connection for range=1713679963- (…)
ff: seek_cb reopen off=1713679963 ok=true ms=294
ff: seek 2557s rv=0 ms=294

So on 7.82 stop() recognises a detached, unfinished transfer as HTTP/2 and the next request dials
fresh; each seek cancels a mid-body transfer, so each starts a new mark and logs once. The log does not say whether that came from the handle's own CURLINFO_HTTP_VERSION or from
its origin's version recorded at the response headers. This shows the rule firing, not a timing
comparison: no HTTP/1.1 or unfixed arm was run on this set.

Tests

plx_net: 333 passed, 0 failed, 1 ignored. Regression tests, one or more per rule:

  • only_an_http2_transfer_that_did_not_finish_cleanly_leaves_its_connection_busy,
    stop_judges_a_failed_transfer_unfinished_and_a_failed_connect_not_at_all (the pure decision:
    version, clean finish vs failure, connect-phase failure, attachment, origin fallback, key mode).
  • the_ledger_keeps_the_last_version_per_origin, the_ledger_forgets_the_least_recently_used_origin,
    a_busy_mark_lasts_busy_for_and_logs_its_first_dial_only,
    a_fresh_connection_is_dialled_only_to_the_busy_origin_as_libcurl_reads_it.
  • Through real libcurl on the loopback server (HTTP/1.1, so a busy HTTP/2 connection is stood in for by
    a mark or a recorded version): requests_to_a_busy_http2_origin_dial_fresh_for_the_window_and_reuse_after_it
    (accept counts 2, 3, then 3 again past the window; one log line),
    a_detached_unfinished_http2_request_marks_its_origin_busy_across_a_failed_chain,
    a_request_stopped_before_its_response_line_takes_only_its_own_origins_version,
    a_handle_that_never_entered_the_multi_leaves_no_busy_connection (a bounded range ending before it
    starts), a_request_that_never_connected_leaves_no_busy_connection (a refused connect, rc 7),
    an_unfinished_http11_transfer_leaves_no_busy_connection.

Nine mutations, each run against range_io::tests and each failing at least one test:

Mutation Fails
A stop() does not record the origin a_detached_unfinished_http2_request_marks_its_origin_busy…
B the mark is consumed by the first fresh dial a_busy_mark_lasts…, a_fresh_connection_is_dialled…, requests_to_a_busy_http2_origin…
C version fallback is the source's last, any origin a_request_stopped_before_its_response_line_takes_only_its_own_origins_version
D a failed transfer counts as finished stop_judges_a_failed_transfer_unfinished_and_a_failed_connect…
E a never-attached handle is judged a_handle_that_never_entered_the_multi…, stop_judges…
I a failed connect is judged a_request_that_never_connected…, stop_judges…
F FIFO eviction instead of LRU the_ledger_forgets_the_least_recently_used_origin
G the window never expires a_busy_mark_lasts…, requests_to_a_busy_http2_origin…
H every fresh dial is logged a_busy_mark_lasts…, requests_to_a_busy_http2_origin…

Gates

Head 56e5b45e, with main 612fc8cf merged in, tree clean, Apple M1.

  • make check: ==== check: 127 steps — all ok ====.
  • make check-nodef: finished, exit 0.
  • ARM make ipk (debug): PASS actual IPK: 8 ELF files, all packaging assertions passed.
    make ARM_PROFILE=release ipk, then check-build-budgets.py --binary on the staged binary:
    17,697,584 of 20,000,000 bytes, ok.
  • tools/fwcompat.py --min-release 4.4.2: exit 0 on the main binary.

The two failing curlio::keymode_tests in the first CI run were the ubuntu-26.04 / libcurl 8.18 TLS
session resumption that #1297 fixed (the run tested a merge onto 8e486d1a); this branch now has main
merged in, which includes it. The host's libcurl (8.7.1) cannot reproduce that failure, so CI on this head is what confirms it.

Firmware compatibility: no new symbol. CURLOPT_FRESH_CONNECT is in curl.h since 7.7 (the control
plane already sets it), CURLINFO_HTTP_VERSION since 7.50.0, below the 7.53.1 floor; both go through the
already-bound curl_easy_setopt/curl_easy_getinfo wrappers. On 7.53 and 7.61 the default is HTTP/1.1, so
the rule does not fire there.

Not verified

  • No timing measurement of this PR's code. The benchmark numbers above come from a client that sets
    the option on its seek requests; the television run above shows the rule firing, not its effect.
  • No end-to-end HTTP/2 run in the host tests. The loopback server is HTTP/1.1. The loopback tests
    ran on the Mac's libcurl 8.7.1 only (CI's run is pending).
  • The failed-transfer rule (an h2 error keeps the connection) rests on reading 7.82's source and on
    the pure decision test; no failed h2 transfer was observed. A connect timeout (rc 28) cannot be told
    from a mid-body low-speed timeout by its code, so it still marks an h2 origin busy (120 s of fresh
    dials, never a reuse of a busy connection).
  • A request to an h2 origin stopped while still in DNS or connect takes the origin's version and marks
    it busy, though it queued nothing; the cost is the same 120 s of fresh dials.
  • libcurl 7.62 to 7.64 default to HTTP/2 and have no CURLOPT_MAXAGE_CONN, so there the 120 s
    window does not guarantee the busy connection has been dropped. The firmware inventory records
    sonames, not curl versions, so whether a supported firmware ships one is not known.
  • The 120 s window is from libcurl source reading (7.82, 8.23), not measured; nor the HLS cost of a
    handshake per request inside it.
  • The residual 1.38x at 20 Mbit/s, and any effect of CURLOPT_FORBID_REUSE.
  • Behaviour under loss (CIs include 1.0), and servers other than Traefik/nginx.

Signed-off-by: fraygox [email protected]

🤖 Generated with Claude Code

fraygox and others added 2 commits November 8, 2026 23:58
The media transport never sets CURLOPT_HTTP_VERSION, so a firmware
libcurl of 7.62 or later negotiates HTTP/2 with a server that offers
it (webOS 10.3's 7.82 does). A seek removes the unfinished transfer and
attaches a fresh easy handle to the same multi. On HTTP/1.1 libcurl
closes a connection whose transfer was removed unfinished, so the next
request dials; on HTTP/2 it keeps the connection (a premature
multi_done does not close a multiplexed connection, 7.82 and 8.x
alike) and hands it to the next request, whose bytes then wait behind
everything the server had already queued for the cancelled stream in
its kernel send buffer: 2.6-3.6 MB measured with ss and a pcap,
time-to-new-data about that over the link rate, Traefik and nginx alike.
On a television, paired medians against HTTP/1.1 over 8 rounds: the
firmware curl's seek-to-resume was 3.14x slower at 8 Mbit/s and 2.22x
at 20 Mbit/s; with CURLOPT_FRESH_CONNECT on the seek's requests, 1.00x
[0.79, 1.02] and 1.38x [1.01, 1.89], and 12-17% less client CPU per MB.

stop() now reads CURLINFO_HTTP_VERSION of the handle it detaches
(falling back to the version the source's last response came over, for
a handle detached before its own response line) and, when the transfer
was unfinished and on HTTP/2, records that origin as busy. The next
request chain consumes the fact and sets CURLOPT_FRESH_CONNECT on every
attempt to that origin (a redirect's return to it, a TLS ladder rung);
a chain that fails puts the fact back, since the busy connection is
still cached for the seek after. The decision is two pure functions,
left_backlog and dial_fresh.

Only HTTP/2: HTTP/1.1 already dials (libcurl closed the connection), so
the option would change nothing; HTTP/3 cancels per stream, with no
shared byte stream to queue behind; a finished transfer queued nothing,
and reusing its connection stays the right call (HLS segments).

CURLOPT_FORBID_REUSE on the abandoned handle is NOT added. It would
close the busy connection at removal, which libcurl's source shows
works the same way in 7.82 and 8.23 (multi_done reads reuse_forbid
off the removed handle), and it would stop the old backlog competing
for the link and keep it out of libcurl's cache. But it has to be set
on a transfer still in progress, which curl_easy_setopt's contract
calls undefined, and nothing measured it: the measured fix set
FRESH_CONNECT alone and reached parity at 8 Mbit/s. The busy connection
is left to libcurl's cache, which bounds it (7.82 closes the oldest
cached connection once the cache holds more than four per easy
handle). One residual is known: a later request that does not replace
an unfinished HTTP/2 transfer (a seek after a transfer finished, the
next HLS segment) may still be handed the old connection, since 7.82
picks the oldest idle match. The residual 1.38x at 20 Mbit/s
may be that backlog still arriving on the old connection; measuring it
is the condition for adding the close.

Compatibility: no new symbol. CURLOPT_FRESH_CONNECT (74) is in curl.h
since 7.7 and still in 8.23 (net::keypin already sets it on the
control plane); CURLINFO_HTTP_VERSION (LONG + 46) since 7.50.0, below
the 7.53.1 floor. Both go through the already-bound setopt/getinfo
wrappers. A refused setopt is logged and the request reuses the
connection, as today; a refused getinfo reads as an unknown version,
which never dials fresh. On 7.53 and 7.61 the default is HTTP/1.1, so
the rule does not fire there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: fraygox <[email protected]>
Review follow-ups to the previous commit.

The busy fact is no longer consumed by the request that dialled past
it. libcurl hands a request the OLDEST matching idle connection (7.82
appends each returned connection to its host bundle and takes the first
match, url.c ConnectionExists; 8.23 the same), so after one fresh dial
the next request that did not replace an unfinished HTTP/2 transfer was
handed the busy connection again: the HLS segment after the one that
followed a cancel, or the MKV open's seek back after a Cues read to the
end. Now every origin a detached unfinished HTTP/2 transfer left busy
stays in CurlSource::busy, and every later request to it sets
CURLOPT_FRESH_CONNECT, until the multi is rebuilt (an empty cache).

Why this over a counted window ("until N requests have dialled fresh"):
the busy connection stays choosable until libcurl evicts it, and when
that happens depends on the cache limit (7.82: more than four cached
connections per easy handle) and on libcurl's version, so any N is a
guess at libcurl internals. The cost of staying fresh is one TCP+TLS
handshake per request to that origin: for progressive playback, where
requests are seeks, that is exactly HTTP/1.1's per-seek dial, the
measured baseline; for HLS it is one handshake per segment after the
first cancel, against seconds of a 2.6-3.6 MB backlog at link rate each
time the busy connection is handed out. Closing the busy connection
would end both costs, but needs CURLOPT_FORBID_REUSE on a transfer in
progress (undefined per curl_easy_setopt) and is still unmeasured.

- Origins are compared as libcurl dials them (target_of, the redirect
  code's reading), not origin::split's tolerant one, which reads a URL
  with no port as :32400: https://cdn/a and https://cdn:443/b are one
  connection, https://h/a and https://h:32400/b are two.
- A key-mode handle records nothing: keypin::apply sets FRESH_CONNECT
  and FORBID_REUSE on this plane's key-mode handles too, so its
  connection closes with it and the new setopt is a no-op there. The
  constant's doc said only that the control plane sets it.
- The multi-rebuild path empties the list, so the fresh-connection log
  line never claims a busy connection in an empty cache; the line now
  prints the actual range (bounded probes included).
- finish_redirect_body's doc said a connection removed mid-body is
  closed; on HTTP/2 it stays cached, and stop() records it.
- The record lives in a bounded list (MAX_BUSY_ORIGINS, 8, registered
  in ci/list-caps.ini as work): past it the oldest origin is forgotten,
  costing one possible reuse of a busy connection.
- The chain plumbing of the previous commit (run_chain, the backlog
  parameter, restore-on-failure) is gone: start_attempt reads the list.

Tests: dial_fresh over conn_origin (443 vs 32400, case, scheme);
note_busy dedup and bound; every later seek to a busy origin dials
(accept count 3 on the third request); and the stop() wiring through
real libcurl: a reopen stopped before its response line falls back to
the source's last version, records the origin, the next reopen logs a
fresh connection, and the failed chain keeps the record.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: fraygox <[email protected]>

@GLinnik21 GLinnik21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eight inline findings, most severe first: three about when an origin is recorded busy (the per-source version fallback, a failed transfer counted as finished, a never-attached handle), two about how long it stays recorded (for the multi's life; FIFO eviction), one on test coverage, and two cleanups.

These come from reading the diff against the surrounding curlio.rs; nothing was run, and the claims about libcurl's connection handling for a failed HTTP/2 transfer are from memory of its source rather than checked against the 7.82 firmware build.


Generated by Claude Code

Comment thread rust-modules/net/src/curlio.rs Outdated
// What this transfer leaves on its connection, read while the handle can still answer.
// A transfer detached before its response line has no version of its own yet; it rode
// the connection the source's last response came over, or a new one to the same host.
let version = match http_version(self.easy) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version fallback is per source, not per origin. A handle detached before its own response line takes self.http_version, which is the last response from any origin, and it is used even when the handle never got a connection.

Chain PMS (HTTP/1.1) → 302 → CDN (HTTP/2): the CDN response leaves http_version = 3. The next seek replays the chain and is stopped (or deadlined) before the PMS hop answers, possibly still in DNS/connect. The PMS origin, which has no HTTP/2 connection at all, is recorded busy and every later request to it dials fresh.

The inverse also happens: once the PMS hop has answered (http_version = 2), a CDN hop cancelled before its response line on the reused h2 connection is not recorded, so the backlog this PR targets is reused.

Keeping the last version per origin (beside busy) would fix both directions.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: the fallback is now the last version of that handle's origin (ConnLedger), so neither direction of the PMS → CDN case misfires. Details in point 1 of #1289 (comment).

Comment thread rust-modules/net/src/curlio.rs Outdated
// stream's queued bytes (`left_backlog`, `busy`): dial past it. Not `require_setopt!`:
// a libcurl that refuses reuses the connection, which is slower and nothing worse. A
// key-mode handle already dials fresh (`keypin::apply`), so this changes nothing there.
if dial_fresh(&self.busy, conn_origin(self.hop_url.to_str().unwrap_or_default()).as_ref()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A busy origin is never forgotten until the multi is rebuilt. After one cancelled HTTP/2 transfer, every later request to that origin does a full TCP+TLS dial, long after libcurl has evicted the busy connection: the cache holds four, and the busy one is the oldest, so it goes first after about four fresh dials.

For HLS over HTTP/2 (reopen_until shares one multi precisely to keep connections) one mid-segment seek means every segment and playlist refresh for the rest of playback pays a handshake on the TV's CPU and writes one curlio: fresh connection event-log line. An unfinished 3xx body on HTTP/2 likewise pins the redirecting origin fresh for every later seek.

The commit message argues against a counted window because N guesses at libcurl internals; the cost of the alternative chosen here is unbounded for HLS, so it may be worth bounding by the cache size the same message already cites, or at least logging the line once per origin.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: a busy mark now lasts BUSY_FOR = 120 s (libcurl's 118 s CURLOPT_MAXAGE_CONN default in both 7.82 and 8.23) instead of the multi's life, and the log line is once per mark. Point 2 of #1289 (comment) has why it's a time window and not a dial count.

Comment thread rust-modules/net/src/curlio.rs Outdated
};
// A key-mode handle was set `CURLOPT_FORBID_REUSE` (`keypin::apply`): its connection
// closes with it, whatever it left queued.
if left_backlog(version, self.done, self.key_pin.is_some()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A failed transfer is treated as finished. self.done is also true when the transfer ended in failure (reap sets done with failed). A failed HTTP/2 transfer only resets its stream and libcurl keeps the multiplexed connection, so it can leave a backlog that is not recorded here.

Scenario: an h2 body transfer dies mid-body (low-speed timeout rc=28, stream error rc=92); libavformat heals with a seek, stop() sees finished = true and records nothing, and the reopen is handed the same connection with the dead stream's queued bytes ahead of it. That is the slow seek this PR fixes, on the heal path.

Passing self.done && !self.failed would cover it. I have not confirmed the libcurl side on a 7.82 build.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: done && !failed, checked against 7.82's streamclose path. Point 3 of #1289 (comment).

Comment thread rust-modules/net/src/curlio.rs Outdated
0 => self.http_version,
v => v,
};
// A key-mode handle was set `CURLOPT_FORBID_REUSE` (`keypin::apply`): its connection

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop() can judge a handle that never entered the multi, with the previous attempt's key_pin. start_attempt assigns self.easy before self.key_pin, and the Some(_) => return Err(OpenErr::Local) arm of the range match (line 1239) returns in between.

A bounded-range start with range_end < at therefore leaves a never-attached handle; the caller's stop() sees done = false, version 0 falling back to the source's last (HTTP/2) version, and the previous attempt's key_pin. After a strict previous attempt it records the hop origin busy for a request that was never sent.

Narrow, but cheap to close: compute the range before self.easy = easy, or clear key_pin in retire_attempt.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45 with an attached flag set after curl_multi_add_handle, which covers every early return, not just this one. It also skips handles that failed before connecting. Point 4 of #1289 (comment).

Comment thread rust-modules/net/src/curlio.rs Outdated
return;
}
if busy.len() >= MAX_BUSY_ORIGINS {
busy.remove(0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIFO eviction re-enables reuse of a busy connection. The oldest origin is dropped at MAX_BUSY_ORIGINS whether or not its busy connection is still cached, and an origin noted again is not refreshed.

A server that redirects each seek to a different CDN edge hostname (presigned URLs) can pass eight busy origins over a scrubbing session. The first ones go first, including the PMS origin that every chain replay hits, and its next request is handed the busy HTTP/2 connection again.

Moving a re-noted origin to the back (LRU) would keep the one that matters.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: eviction is now LRU, and the test checks before the server is re-noted, so it fails on FIFO. Point 5 of #1289 (comment).

Comment thread rust-modules/net/src/curlio.rs Outdated
/// The loopback server speaks HTTP/1.1, so the busy HTTP/2 connection is stood in for by
/// recording its origin; each transfer is read to its end so libcurl keeps its connection
/// cached, which is what a reusing seek would take. Every later seek must dial: libcurl hands
/// a request the oldest matching idle connection, which would be the busy one.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test drives the rule through a real HTTP/2 response. This test plants the origin in src.busy by hand, and the stop() test below overwrites src.http_version. The CURLINFO_HTTP_VERSION == 3 reading, and the assumption that a detached mid-body h2 handle reports its version, are unverified on host.

If a firmware libcurl answers differently for a mid-body handle, stop() never records an origin and the fix is inert on the TV while every host test stays green. A device log showing the curlio: fresh connection line after a seek against an h2 server would close this.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device run on a webOS 10.3 set (firmware libcurl 7.82, HTTP/2 default) through Traefik: the curlio: fresh connection line on a resume and on five seeks, all reopens ok. Log in point 6 of #1289 (comment). There's still no end-to-end HTTP/2 host test.

Comment thread rust-modules/net/src/curlio.rs Outdated
/// origin is still busy with a cancelled HTTP/2 stream; a libcurl
/// that refuses it reuses the connection, which is today's behaviour, so the refusal is logged, never
/// fatal.
const CURLOPT_FRESH_CONNECT: c_int = 74;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate constant. CURLOPT_FRESH_CONNECT (74) is already declared at net.rs:135 beside CURLOPT_FORBID_REUSE for keypin::apply. Making that one pub(crate) and using it here, as this file already does for CURLOPT_CAINFO, avoids two copies with separately maintained doc comments.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: it uses net.rs's constant, now pub(crate), with its doc updated. Point 7 of #1289 (comment).

Comment thread rust-modules/net/src/curlio.rs Outdated
// A key-mode handle was set `CURLOPT_FORBID_REUSE` (`keypin::apply`): its connection
// closes with it, whatever it left queued.
if left_backlog(version, self.done, self.key_pin.is_some()) {
if let Some(o) = conn_origin(self.hop_url.to_str().unwrap_or_default()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hop origin is re-parsed from the URL. conn_origin(self.hop_url.to_str().unwrap_or_default()) appears here and in start_attempt, going CString → str → target_of → resolve_location each time, with a silent empty-string fallback. A redirect Hop is built from a Target that already holds the parsed Origin; storing it beside hop_url removes the parse, the helper and the fallback.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 56e5b45: Hop carries the Origin from its Target (parsed once for the first hop); conn_origin is gone. Point 8 of #1289 (comment).

fraygox and others added 2 commits November 9, 2026 19:23
…handles

Review of plxnative#1289:

- The version a handle detached before its response line falls back to is
  its ORIGIN's last, not the source's: a chain from an HTTP/1.1 server to an
  HTTP/2 CDN no longer marks the server busy, nor misses the CDN.
- A failed transfer counts as unfinished (done && !failed). In 7.82 the
  error paths call streamclose, which for an h2 (PROTOPT_STREAM) handler
  closes the stream and keeps the connection.
- stop() judges only a handle that entered the multi (an `attached` flag set
  after curl_multi_add_handle), closing every early return in start_attempt,
  not only the bounded-range one; and not one that failed before it had a
  connection (rc 5, 6, 7, 35, 51, 60, 90), such as a refused connect or the
  strict attempt that retries in key mode. Read from the CURLcode, not
  CURLINFO_REQUEST_SIZE, which reads 0 even for a finished transfer on 8.7.1.
- A busy mark lasts BUSY_FOR (120 s), not the multi's life: libcurl refuses
  an idle connection older than CURLOPT_MAXAGE_CONN (118 s default in 7.82
  and 8.23), and the busy one is idle from its cancel. A dial count was not
  used: the cache limit is 4 per attached handle in 7.82 and 2 (idle >= 1 s)
  in 8.23. The fresh-dial line is logged once per mark.
- Origins are kept least recently used first (MAX_LEDGER_ORIGINS = 8), so
  the server every chain replay starts from outlives rotating CDN edges.
- CURLOPT_FRESH_CONNECT is net.rs's, now pub(crate); the hop's Origin is
  carried on Hop from its Target instead of re-parsed from the URL.

Signed-off-by: fraygox <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fraygox

fraygox commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All eight are addressed in 56e5b45e, one commit after a merge of main (612fc8cf) into the branch. The PR body is updated to match.

CI first. The two curlio::keymode_tests failures weren't this change. The run tested a merge onto 8e486d1a on ubuntu-26.04 (libcurl 8.18), which is the TLS session resumption that #1297 fixed. Other branches on pre-#1297 bases failed the same two tests. The branch now has main merged in, so it includes that fix. The host here has libcurl 8.7.1, which can't reproduce that failure, so the new CI run is the real confirmation.

  1. Version fallback per source → per origin. stop() now falls back to the last version that handle's origin answered on (ConnLedger). The PMS (HTTP/1.1) → CDN (HTTP/2) chain marks neither wrongly, in either direction. Tests: a_request_stopped_before_its_response_line_takes_only_its_own_origins_version (real libcurl) and the_ledger_keeps_the_last_version_per_origin.
  2. Never forgotten → a 120 s window. A busy mark now lasts BUSY_FOR = 120 s, and a new cancel restarts it. I bounded it by time instead of by the cache size because the cache size differs across versions. 7.82 evicts its oldest idle connection past 4 × attached handles. 8.23 evicts past 2 × attached, and only connections idle ≥ 1 s. So "about four dials" holds on the firmware's 7.82 but not on the bundled 8.23. What both versions share is CURLOPT_MAXAGE_CONN (118 s default in each). A connection idle that long is closed at the next lookup instead of reused, and the busy one is idle from its cancel. For HLS, one cancel costs a handshake per request for two minutes, then reuse resumes. The log line is now once per mark, not once per request. These limits come from reading both versions' source (the age check sits on the reuse lookup in both), not from a measurement. One gap: 7.62–7.64 default to HTTP/2 but predate CURLOPT_MAXAGE_CONN, so there the window doesn't guarantee the busy connection is gone. The inventory records sonames, not curl versions, so I can't say whether any supported firmware ships one.
  3. Failed transfer counted as finished. It now uses done && !failed. I checked this against 7.82's source: the error paths call streamclose, which for a PROTOPT_STREAM (h2) handler resets the stream and keeps the connection.
  4. Never-attached handle. Rather than reorder the range computation, stop() now judges a handle only if curl_multi_add_handle succeeded (an attached flag). That covers this return and the others between self.easy = easy and the add, such as the CString::new(pin)?. It also skips a handle that failed before it had a connection (CURLcode 5, 6, 7, 35, 51, 60, 90). Otherwise a refused connect or the strict rc=60 attempt that retries in key mode would mark an h2 origin busy for nothing. I tried CURLINFO_REQUEST_SIZE for this first, but it reads 0 even for a finished transfer on libcurl 8.7.1, which would have made the rule silently inert, so the check reads the code instead. A connect timeout (28) shares its code with a mid-body low-speed timeout, so it stays judged. key_pin isn't cleared, because the abandon path in stop() may still leave it referenced. Test: a_handle_that_never_entered_the_multi_leaves_no_busy_connection, which uses your range_end < at case.
  5. FIFO → LRU. A re-noted origin moves to the back, so the server every chain replay starts from outlives rotating edges. The test checks after each new edge and before the server is re-noted. An earlier version of the test re-noted first, which hid exactly this bug (the FIFO mutation passed it).
  6. No real HTTP/2 path. I ran a debug build of 56e5b45e on a webOS 10.3.0 set (firmware libcurl 7.82.0, HTTP/2 by default), direct-playing a film through Traefik: a resume and five manual seeks, back and forth. Every one logged the line, reopened ok=true in 214–531 ms, and played on:
    ff: seek begin 2677s
    curlio: fresh connection for range=1759380781- (a cached HTTP/2 connection to this origin carries a cancelled stream; later requests to it dial fresh for 120s, unlogged)
    ff: seek_cb reopen off=1759380781 ok=true ms=346
    ff: seek 2677s rv=0 ms=346
    …
    ff: seek begin 2557s
    curlio: fresh connection for range=1713679963- (…)
    ff: seek_cb reopen off=1713679963 ok=true ms=294
    ff: seek 2557s rv=0 ms=294
    
    So on 7.82 the rule isn't inert: stop() recognises the detached, unfinished transfer as HTTP/2 and the next request dials fresh. The log can't tell whether that came from the handle's own CURLINFO_HTTP_VERSION or from its origin's version recorded at the headers. This shows the rule firing, not a timing comparison, and there's still no end-to-end HTTP/2 test on the host.
  7. Duplicate constant. It now uses net.rs's CURLOPT_FRESH_CONNECT, made pub(crate), with its doc comment updated.
  8. Re-parsed hop origin. Hop now carries origin: Option<Origin>. A redirect takes it from its Target, and the chain's first URL is parsed once. conn_origin and the re-parse per attempt and again in stop() are gone. The first hop's single parse keeps the to_str().unwrap_or_default() that Hop::first already used for its key.

The decision in stop() is now a pure function (detach_leaves_backlog), and I ran nine mutations, one per rule, against the tests. Each one fails at least one test; the table is in the body. Gates on 56e5b45e: make check 127/127, check-nodef, ARM ipk (release binary 17,697,584 of 20,000,000 bytes) and fwcompat 4.4.2 all pass.

@fraygox
fraygox requested a review from GLinnik21 November 10, 2026 00:34
@GLinnik21
GLinnik21 merged commit b1bbe75 into plxnative:main Nov 10, 2026
13 checks passed
@GLinnik21

Copy link
Copy Markdown
Collaborator

Thanks!

fraygox added a commit to fraygox/plx-native that referenced this pull request Nov 10, 2026
…busy mark

Review of the main merge (Fable 5.2):

- A source scan pins every libcurl dynlib! table outside net.rs (curlio,
  connect_only) and requires each to bind from net::bundled_curl() with
  load_from, so a revert of the merge's connect_only fix, or a new table
  bound from the system candidates, fails a host test. Nothing else can
  catch it: the host tests never bind the bundle.
- plxnative#1289's busy mark met this branch's Alt-Svc: after a cancelled h2
  transfer, an origin that moves to HTTP/3 kept dialling fresh QUIC
  connections for the whole 120 s window. ConnLedger::saw_version now ends
  the mark on an HTTP/3 answer (Alt-Svc routes the origin to QUIC, which
  never matches the busy TCP connection); an HTTP/1.1 answer still keeps
  it, since it can come from a fresh connection while the busy one is
  cached.
- Docs that named only curlio's multi table as reading bundled_curl now
  name connect_only's too.

Signed-off-by: fraygox <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
fraygox added a commit to fraygox/plx-native that referenced this pull request Nov 13, 2026
…tion

With the bundled curl, a seek still took 1.7x as long as over HTTP/1.1
under plxnative#1289's rule (254 against 148 ms to resume, 8 Mbit/s, ~10 ms from
Traefik, webOS 5.6.2 set; that set's firmware 7.61 asked for HTTP/2
under the same rule: 152 ms). Every request after a seek already dials
fresh, but up to four abandoned connections stayed open beside the new
one.

A bundled media transfer to an origin last heard over HTTP/2, or not yet
heard from while Alt-Svc cannot route it to QUIC, is now set
CURLOPT_FORBID_REUSE before it enters the multi (setting it on a transfer
in progress is undefined per curl_easy_setopt), so its connection closes
with it: 136 ms in the same runs, 0.92x HTTP/1.1. stop() then records no
busy connection for it. Firmware libcurls, HTTP/1.1, HTTP/3, an
unknown origin with Alt-Svc on, and a source reopened for HLS (its
playlist and segment requests finish cleanly, and plxnative#1289's mark still
covers its cancels) are unchanged. A redirect hop the rule chose closes
its connection too, so its next hop dials its own.

Signed-off-by: fraygox <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
nphil added a commit to nphil/Marquee that referenced this pull request Nov 18, 2026
A seek removes the unfinished transfer and adds a new easy handle to the same multi. libcurl closes that connection on HTTP/1.1 but keeps an HTTP/2 one, so the new request queued behind the bytes the server had already written for the cancelled stream: on an 8 Mbit/s link the seek waited about 3.5 s for its first byte instead of about 0.1 s. A source now remembers, per origin, the last HTTP version and whether a detached unfinished or failed HTTP/2 transfer left its connection busy, and sets CURLOPT_FRESH_CONNECT on requests to that origin for the next two minutes (libcurl itself stops offering an idle connection after 118 s). HTTP/1.1, HTTP/3, finished transfers, handles that never connected and key-mode handles are left alone.

Ported from upstream b1bbe75 (Net: a seek that cancels an HTTP/2 transfer dials a fresh connection (plxnative#1289)).
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.

2 participants