Skip to content

feat(network): prefer IPv6 with a bounded IPv4 fallback - #1320

Merged
GLinnik21 merged 9 commits into
plxnative:mainfrom
valentinBoch:feature/ipv6-preferred
Nov 14, 2026
Merged

GLinnik21 merged 9 commits into
plxnative:mainfrom
valentinBoch:feature/ipv6-preferred

Conversation

@valentinBoch

@valentinBoch valentinBoch commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Prefer IPv6 automatically, with IPv4 as a fast fallback, during Plex server discovery. There is no user-facing IP-family picker, no strict mode, and no change to ordinary app-managed network transport policy.

This re-scopes the PR in response to GLinnik21's review.

How it works

  • Rank IPv6 before IPv4 among advertised Plex candidates within the same location/security tier. Preserve location priority (LAN → remote → relay), HTTPS preference, and the numeric-vs-hostname tie-break within each family. IPv4 remains eligible.
  • Per-route eligibility deadlines: a route with a genuinely better score/tier retains the 1.5 s grace after the first verified answer. A same-location, same-scheme, same-score IPv6 route may replace a verified IPv4 winner only within 250 ms of that IPv4 route's own verified reply, regardless of whether a different route keeps the race open. Other same-family/tier ties cannot displace the winner or hold the race open. The coordinator and final winner selection enforce the same individual rules; per-probe deadlines remain unchanged when no winner exists.
  • Diagnostics: preserve the server, address:port, tier and scheme for timeouts and skipped probes while adding family/latency. Completed verified probes and the selected winner log their family and latency. Do not duplicate the transport-layer unreachable logs or log credentials.
  • No global policy state: the planner is again a pure function of the supplied Plex resource and credential policy. The picker, strict-family transport overrides, and legacy prefer_ipv6 / ipv6_only preferences are removed.

Regression coverage

  • Candidate ordering prefers IPv6 while retaining IPv4 fallback.
  • Working IPv4 with blackholed IPv6 settles after approximately 250 ms, not 1.5 s.
  • Fixed-clock tests prove: responsive IPv6 can win within 250 ms; a substantially slower IPv6 cannot replace fast IPv4 even while a local route hangs; a second successful IPv6 is not delayed by a blackholed same-family address; a newly winning local IPv4 starts its own 250 ms window; the 1.5 s better-tier grace is preserved; relay-only behavior follows the same rules; and the 250 ms boundary is enforced.
  • Replaced the fragile threaded exact-order test with a deterministic no-thread winner test. Red → green evidence: 3 new regressions failed on the previous PR head; after the fix, all 7 review-focused fixed-clock tests pass.
  • Updated sequential discovery tests to reflect IPv6-first ordering without relying on cross-test global state.

Verification

  • cargo test --lib -p plx_plex -p plx_session: 805 + 348 passed, 0 failed on the new patch; 7/7 fixed-clock review tests passed.
  • git diff --check, ci/check-deps.sh and make lint: passed.
  • cargo check --tests -p plxnative-modules -p plx_screens: passed (type-checks host application / screen test targets without linking native libraries).
  • Full make check against the updated upstream main: blocked at host linking because the Linux environment lacks -lSDL2, -lSDL2_ttf and -lGL; this is not reported as a passing suite. The Python branch was stopped after the Cargo branch failed.
  • Physical LG webOS testing outstanding: the current build environment has no TV configured. In particular, a real dual-stack and broken-IPv6 playback/network check has not yet been performed.

The latest corrective commit modifies only the discovery race, its unit tests and developer documentation. The PR as a whole retains its IPv6-first candidate planner.

@fraygox

fraygox commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

A family picker is useful, and the strict modes look well plumbed. My concern is with making IPv6 preferred the default. A lot of home networks have IPv6 on the LAN without a working public IPv6 path (CGNAT or IPv4-only fibre), so I read through how the fallback behaves at ce1cbb2. This is from reading the code, not from a TV run.

What already limits the damage

  • Discovery is a parallel race (probe_server_racing), not a sequential walk, so a dead IPv6 route never sits in front of IPv4 for its full 5 s / 10 s deadline.
  • Ipv6Preferred maps to IpFamily::Any, so CURLOPT_IPRESOLVE is unchanged for plex.tv and other dual-stack names. Only the Plex candidate order changes.

What the new default costs

  • Up to 1.5 s per server when an IPv6 candidate hangs. IPv6 now sorts ahead of its IPv4 twin at the same candidate_score, so could_outrank is true for it and race_decided holds the race for PROBE_DEADLINES.grace after IPv4 has already answered. On main the IPv4 twin has the lower index, so the race ends as soon as it answers. The production callers pass a no-op activate into the race, so this is a real wait, and servers are resolved one after another. It only bites when IPv6 packets are dropped silently (the TV has a global address but the path is broken); with no IPv6 route at all the connect should fail immediately.
  • IPv6 wins whenever it answers inside the grace window, even if IPv4 answered much sooner. better() breaks a score tie by index, so the race re-points to the IPv6 twin. The PR description says parallel probes "can still select a substantially faster IPv4 endpoint", but as far as I can tell that only happens when IPv6 takes longer than 1.5 s or fails. A working but slow IPv6 path (tunnel, poor peering) would be picked for media.

No mode reproduces today's behaviour

main is IPv4 first with IPv6 as a fallback. Here, IPv4 only filters IPv6 candidates out entirely (!prefer_ipv6 => !is_v6 in candidates_with_ipv6_policy), so anyone who wants the current ordering loses the IPv6 fallback — which is often the only direct remote route behind CGNAT.

Suggestion

Add a fourth mode, IPv4 preferred (IPv6 fallback), that matches main exactly, and keep that as the default for new and existing installs. IPv6 preferred and the two strict modes then become opt-in, and nobody's behaviour changes on upgrade. Given the description notes there has been no physical webOS testing yet, that seems the safer way to land it; the default could be revisited once there is device data on dual-stack and broken-IPv6 networks.

# Conflicts:
#	rust-modules/screens/src/preferences.rs
#	rust-modules/screens/src/preferences/picker_tests.rs

Copy link
Copy Markdown
Contributor Author

Thanks @fraygox — good catch, particularly the grace-window behavior and the fact that the original three choices couldn't reproduce main.

I've updated this PR to follow your suggestion:

  • Added IPv4 preferred (IPv6 fallback) as the default for both new and existing installations. Its candidate ordering is identical to main (location → scheme → resolvability → IPv4 first), and IPv6 remains eligible as fallback.
  • Kept IPv6 preferred (IPv4 fallback) and IPv4 only / IPv6 only as explicit opt-in choices.
  • Existing installs with no protocol setting stay IPv4-first on upgrade. Previously saved explicit test-build choices continue to be respected.
  • Updated all 12 translations and tests, and merged current upstream main into the branch.

Checks passed: 19 preference tests, 27 Plex probe tests, cargo test-target type checks, localization, dependency gates and the no-default-features build check. Physical LG/webOS playback/network testing is still outstanding (the host UI unit test executable cannot link here without SDL2/SDL2_ttf/GL dev libraries).

Appreciate the careful review!

@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.

Thanks for this, and thanks @fraygox for the careful read.

I'd rather not ship a picker for this. Most people won't know what IPv4 vs IPv6 means on a TV, and the strict modes can quietly break the app. "IPv6 only" applies to the live connection right away, so on an IPv4 server, browsing and playback stop until a restart.

I'm actually pro IPv6 and would like the app to prefer it by default, with no setting. Something like Happy Eyeballs (prefer IPv6, fall back to IPv4 after a short delay) in the route race:

  • rank IPv6 ahead of its IPv4 twin within the same location/scheme tier
  • when IPv4 has already answered and the only better candidate left is the IPv6 twin, wait ~250 ms instead of the full 1.5 s grace
  • log which family won and how long each took, so there's data behind the default

That way working IPv6 wins, broken IPv6 costs a quarter second, and nobody has to find a setting.

Would you be up for re-scoping the PR that way? Your probe changes are most of the way there. It should take the IP mode as an argument rather than reading the session global, and the legacy prefer_ipv6/ipv6_only keys can go, since no release ever wrote them. A TV run on a dual-stack network would also help a lot before it lands.

Comment thread rust-modules/plex/src/plex/probe.rs Outdated
/// field's own doc and `auth.rs`'s race semantics for what that answer is allowed to mean.
pub fn candidates(res: &Resource, policy: CredentialPolicy) -> Vec<Candidate> {
// Pure cached read, not a storage I/O request. It is already populated at boot.
let saved = super::session::peek();

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.

planner shouldn't read the session global. can the ip mode come in as an argument? candidates_with_ip_mode already takes one

}
}) {
let location = tier(c);
let ipv6 = c.ipv6 || c.address.contains(':');

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.

nit: v6 check is computed twice here and a few lines up in the filter. compute once so the filter and sort can't disagree

c.address, "192.168.0.10",
"IPv4 leads the plaintext fallbacks"
c.address, "2001:db8::1",
"IPv6 leads the plaintext fallbacks by 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.

these expect IPv6 first but the default mode is IPv4 first. passes only if another test left an IPv6 session behind?

fn ip_family_modes_constrain_plain_http_literal_resolution() {
use crate::net::{IpFamily, set_ip_family};
let _guard = plx_base::testlock::serial();
set_ip_family(IpFamily::V4);

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.

this flips the global IP family while other tests run in parallel, and a failing assert skips the reset. could the family be passed in instead?

Replace the opt-in family picker with automatic dual-stack discovery.
Rank IPv6 ahead of IPv4 within a location/security tier; keep IPv4
eligible and limit same-tier IPv6 grace to 250 ms after IPv4 answers.
Log each probe family/latency and the selected family, without global
IP family configuration or legacy preference keys.

Add regression tests for fast, slow and blackholed IPv6 paths.
@valentinBoch valentinBoch changed the title feat(network): add IPv4/IPv6 connection policy selector feat(network): prefer IPv6 with a bounded IPv4 fallback Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @GLinnik21 — I've re-scoped #1320 as requested and pushed the updated branch.

  • Removed the user-facing IP-family picker, strict modes, legacy preference keys, and global family policy changes. The candidate planner no longer reads session-global state.
  • IPv6 is now ranked before IPv4 within the same location/security tier; IPv4 always remains eligible as fallback.
  • If IPv4 verifies first and the only remaining candidates that can outrank it are same-location, same-scheme, equal-score IPv6 alternatives, we allow up to 250 ms rather than the generic 1.5 s grace. Better-ranked LAN/security routes still receive the original grace.
  • Added per-probe family, outcome and latency logging, including timeouts/abandoned probes, plus the chosen family and its probe latency.
  • Corrected discovery order expectations and added regressions for fast, blackholed and slower-than-IPv4 IPv6, and for the same-tier-only grace rule. Tests don't mutate any process-global family setting.

Verification after merging current main:

  • cargo test --lib -p plx_plex -p plx_session: 805 + 343 passed, 0 failed.
  • cargo check --tests -p plxnative-modules -p plx_screens: passed.
  • git diff --check: passed.
  • The complete make check could not finish: this Linux host lacks the SDL2 / SDL2_ttf / GL link libraries required for the UI test binaries. This is an environment linker failure, not a successful full-suite run.

Still outstanding: physical LG/webOS validation on a real dual-stack and broken-IPv6 network; this environment does not have a TV configured, so I haven't claimed hardware/playback testing.

I re-read the diff against current main: only the Plex candidate planner, discovery race, regression tests and related documentation remain. Thanks for the clear direction!

Copy link
Copy Markdown
Collaborator

Thanks for the re-scope, @valentinBoch. The picker, the strict modes, the global state and the legacy keys are all gone, IPv6 ranks first within a tier, and the logging is in. Reviewed at e021420a, by reading the code; nothing was run.

The 250 ms window works for one IPv4 route and its IPv6 twin. Two other cases still behave differently, and neither has a test.

1. A dead IPv6 address can still hold the race for 1.5 s (auth.rs:2227)

grace_for_pending returns the full grace whenever the current winner is IPv6 (if best.candidate.ipv6 { return default; }). Take a server that advertises, in one tier:

[v6a (dead), v6b (works), v4 (works)]

v4 answers, then v6b answers and takes over. v6a still sits higher in the list, so the race waits the full 1.5 s. On main v4 sorts first and the race ends as soon as it answers. The test fixture already advertises two local IPv6 addresses (auth_discovery_tests.rs:2046), so this setup is realistic.

2. A much slower IPv6 route can still replace IPv4 (auth.rs:2229-2243, better() at :2101)

The 250 ms cap applies only when every pending route that could outrank the winner is an IPv6 twin. Away from home, for example:

Local  https (hangs, wrong network)
Remote https v6  answers at 1.2 s
Remote https v4  answers at 30 ms

The hanging Local route keeps the 1.5 s window open. Remote v6 then answers, wins the tie-break on list position, and replaces a route that is 40× faster. The PR body says "a substantially slower IPv6 endpoint does not displace fast IPv4", but that is only tested with two routes (auth_discovery_tests.rs:404-418).

Suggested rule

Rather than adjusting the race-wide grace, decide for each pending route whether it can still win:

  • It ranks higher on location or scheme: it may win until grace (1.5 s) after the first answer. This is unchanged from main.
  • It is IPv6 at the same rank as an IPv4 winner: it may win until 250 ms after that IPv4 route answered, not after the first answer of any route (ports.rs:313-315 measures from first_win today).
  • Anything else (another address of the same rank and family): it cannot win, and the race does not wait for it.

The race ends when no pending route can still win.

Situation Expected
Local v4 answers, Local v6 hangs v4 accepted at +250 ms
Local v6 answers 100 ms after Local v4 v6 wins
[v6a dead, v6b ok, v4 ok] in one tier settles when v6b answers, with no wait for v6a
Away from home: Remote v4 answers, Local hangs Remote v4 accepted at 1.5 s (as on main)
…and Remote v6 answers 1.2 s later Remote v4 kept
Remote v4 answers, Local v4 at 800 ms Local v4 wins; Local v6 gets 250 ms from 800 ms
Relay only same rules inside the relay phase

On the third row: two same-family addresses of the same rank already cost 1.5 s on main for IPv4 (could_outrank: score == best.score && index < best.index). Covering that here is optional, but this PR makes it common for IPv6.

Tests

Per AGENTS.md, please add the regressions first and see them fail on the current head. The table rows map well onto the pure grace_for_pending / race_decided tests, as the one at :421 already does, with fixed timings and no threads. The threaded "IPv6 wins inside the quarter second" test (auth_discovery_tests.rs:388-401) has about 160 ms of slack and asserts an exact order, so it is likely to flake when two make check runs share a machine.

Minor

  • Logging: auth: '{name}' probe timed out at {address}:{port} became probe family=IPv6 result=timeout waited_ms=N. With up to eight direct routes per server, the log can no longer say which route timed out. Please keep the server, tier, scheme and address, and add family and latency to that line. The new line also logs result=unreachable, against the comment at auth.rs:2192 ("Not logged here").
  • Docs: rust-modules/plex/src/plex/CLAUDE.md:304-307 should describe the window as starting from the IPv4 twin's answer once that is fixed.
  • CI and device: CI has not run on e021420a yet. A TV run on a dual-stack network is still wanted before this lands, ideally including one with IPv6 silently dropped.

Generated by Claude Code

Evaluate each pending route's win window separately rather than a shared race grace. Higher-scored/better-tier routes retain 1.5s from first reply; equivalent IPv6 candidates get 250ms from the current IPv4 winner's reply. Never promote late IPv6 or wait for same-family ties.

Preserve per-address timeout diagnostics, remove duplicate unreachable logs, use deterministic no-thread regression tests, and update discovery documentation.

Copy link
Copy Markdown
Contributor Author

Thanks @GLinnik21 — I followed up on your review and pushed 00dbf91 to this PR.

I followed the requested red → green workflow on the previous PR head (e021420a): I added fixed-clock regressions first, observed 3 failing tests (dead IPv6 peer prolonging the race, slow IPv6 replacing fast IPv4 while Local hangs, and the IPv6 window being anchored to the first winner), then changed the implementation and reran them successfully.

The new decision logic is per candidate:

  • A genuinely better score/location/scheme keeps the existing 1.5 s window from the first verified answer.
  • Equivalent IPv6 can replace IPv4 only within 250 ms of that IPv4 winner's own verified answer, even if an unrelated higher-tier probe keeps the overall race open.
  • Another address of the same score/tier/family cannot replace the winner or hold the race open.
  • Both the pending-probe wait/settlement and late-arriving verified responses enforce these same rules, using each worker's recorded answer time rather than the coordinator's dequeue time.

Other requested changes:

  • Replaced the timing-sensitive threaded IPv6 activation-order test with a deterministic, fixed-clock replacement test.
  • Restored identifying context in timeout/abandoned-probe logs (server, address:port, location, scheme), while retaining family and elapsed time. Avoided duplicating transport-layer unreachable messages.
  • Updated the discovery documentation to say precisely that 250 ms starts at the winning IPv4 route's own reply.
  • The review table's cases, including two IPv6 addresses where one is dead, late IPv6 on a remote route, late local IPv4 promotion, relay behavior and the 250 ms boundary, are covered in fixed-clock tests.

Checks on the corrected tree: plx_plex 805/805, plx_session 348/348, review-specific cases 7/7, cargo check --tests -p plxnative-modules -p plx_screens, ci/check-deps.sh, make lint and git diff --check all passed.

The full host UI executable test suite still cannot link on this server without SDL2/SDL2_ttf/GL development libraries; I am not claiming a full make check pass. Real LG webOS dual-stack / silently-dropped-IPv6 verification is also still outstanding.

Thanks again for the detailed scenarios and specific feedback.

Copy link
Copy Markdown
Collaborator

Thanks @valentinBoch, the per-route rule in 00dbf91 matches every row of the table, and it held up on the TV. A code re-review found nothing blocking.

TV verification at 00dbf91

This used the ARM release build on the LG set. Each run was a fresh sign-in through the mock plex.tv, with no real account involved. The server was on the LAN: IPv4 went to the mock PMS, and the IPv6 twin(s) went to a ULA address behind a delay proxy, in the same tier and with the same scheme. Times are from the TV's clock.

Case Selected IPv4 answer → selected
C0: no IPv6 route IPv4 +0 ms
C1: IPv6 twin answers +100 ms IPv6 (latency_ms=115) chosen as soon as IPv6 answered
C2: IPv6 twin accepts, never answers (2 runs) IPv4 +246 / +247 ms (probe not waited for … family=IPv6 waited_ms≈260)
C3: IPv6 twin answers +400 ms IPv4 +246 ms; the late IPv6 reply was ignored
C4: SYN silently dropped (stealth firewall) IPv4 +246 ms
C4′: IPv6 refused at once (RST) IPv4 +0 ms
C5: dead IPv6 listed first, live IPv6 at +100 ms, plus IPv4 IPv6 settled on the live IPv6 answer, with no wait for the dead one

Playback in C2 worked over IPv4 (direct play). The held IPv6 proxy saw no connection after the race.

make check (0 failed) and make check-nodef both pass, and all 13 CI checks on 00dbf91 are green.

Not covered: a real ISP dual-stack path. These runs were LAN-only, with the mock standing in for the server.

Optional follow-ups (not blocking)

  • auth/ports.rs:333-384: the time left in the window is computed from two clock reads per loop. If the window closes between them, the race can fall through to the 5 s / 10 s probe deadline. Computing it once per iteration fixes this. main already has the same pattern.
  • auth_discovery_tests.rs:384, :413: these two threaded wall-clock tests are the only ones that exercise the real wake-up, and they have 350–500 ms of slack. Widen them, or switch to event-ordered channels.
  • auth.rs:2139-2143: 401, wrong-server and plaintext-only answers now log two lines each.
  • Stale comments: screens/src/login.rs:118-119 ("at most grace (1.5 s)") and the probe_server_racing doc comment.

Generated by Claude Code

@GLinnik21
GLinnik21 merged commit e6200fe into plxnative:main Nov 14, 2026
13 checks passed
@GLinnik21

Copy link
Copy Markdown
Collaborator

Thanks!

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.

3 participants