Skip to content

test(node): de-flake the DHT integration suite and unpin two teardown budgets (#1830) - #1832

Merged
thiras merged 3 commits into
mainfrom
fix/1830-deflake-dht-and-teardown
Aug 26, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/1830-deflake-dht-and-teardown

Conversation

@yigitdot

@yigitdot yigitdot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1830.

Three of #1830's seven claims survive verification against main. The rest describe defects that #691, #1594/#1595 and #1779 already closed — the issue read historical rationale left in code comments as live defects, so those comments go too. The full per-claim triage is on the issue.

Negative-cache expiry stops being a latency race

find_providers_negative_cache_expires_after_ttl raced its own fixture: the injected 750ms TTL had to outlive a live loopback QUIC lookup before the suppression assertion read it, or the entry expired early and the provider was never suppressed. tokio::time::pause is unavailable there — the test is multi_thread over real QUIC, and the cache is anchored on std::time::Instant either way, so swapping the clock source would not have helped.

Suppression now runs under the production TTL, which nothing in the test can outlive, and expiry is forced on the one key under test by re-recording it with a Duration::ZERO TTL: entries hold an absolute expiry, shift_insert updates an existing key in place, and contains_active reads expiry <= now as elapsed. Duration::ZERO-as-already-expired is the module's own documented idiom (probe_cache.rs), not a new trick. A len() == 1 assertion pins that the re-record lands on the same key, so a future refactor cannot quietly turn this into "an unrelated entry doesn't suppress".

The 1100ms sleep goes with it — 1.40s → 0.35s. Wall-clock TTL passage stays covered by the negative_cache unit tests and node_origin_pull's refusal suite; what this test uniquely owns (that find_providers consults the cache on every pass, and the same instance recovers, across a real QUIC round trip) is unchanged.

One round_timeout was serving two opposite purposes

3s was short so the unreachable-peer scenario fails fast — but it was simultaneously the budget a reachable loopback round had to answer in, for two convergence tests, one of which needs two sequential rounds. Split into a convergence budget sitting just above DHT_CLIENT_TIMEOUT (so the per-RPC cap stays the binding constraint and the round drain never pre-empts a peer the RPC layer still wants to wait for), and the short budget named explicitly at the one call site where a timeout is the point.

The unreachable scenario's hang guard was Duration::from_secs(12) — exactly MAX_LOOKUP_ROUNDS (4) × 3s, the implementation's own worst case. That is a guard sitting on the ceiling, not clearing it, so it is raised to 30s.

The scenario itself spends one round. Measured by varying the constant: at 3s the test costs 6.4s, at 1s it costs 4.3s — a 2s cut buys 2.1s, so exactly one round is in the budget and the remaining ~3.4s is fixture setup and teardown. The routing table holds two peers and alpha is 3, so round one queries both, and the next pass breaks on an empty batch before arming a second round. The guard is sized against the MAX_LOOKUP_ROUNDS ceiling regardless, which is what a hang guard should clear.

Teardown is bounded and the two budgets no longer collide

The file tore endpoints down by hand — one unbounded close().await and a fire-and-forget spawn that let the test return mid-close — which is precisely the hazard support::shutdown documents. It now routes through that helper, so a stalled close is bounded and names its stage instead of parking until the 180s nextest backstop. Side benefit: support::shutdown returns a panicking server task's JoinError, so a panic in the accept loop itself now fails the test that spawned it rather than being discarded by .abort(). That does not extend to DhtHandler: this file's loop detaches a task per connection and swallows its result, so a handler panic is still not carried by accept_task — support::shutdown's own docs call that shape out.

The one call site where teardown is not teardown is hardened separately. dead.shutdown() in find_providers_tolerates_unreachable_peer establishes the scenario's precondition, and support::shutdown warns rather than fails on a breach (it must — the stranded-driver half is #1675). On a breach the dead peer would stay up and the test would pass having exercised nothing, so TestServer::shutdown now checks Endpoint::is_closed after the bounded close.

Separately, SHUTDOWN_TIMEOUT (10s) equalled ABANDON_DRAIN_CAP (10s). One ceilinged abandon drain could consume the entire test teardown budget before the first ep.close() was attempted, and the breach line would then report 0/M endpoints closed for a run in which nothing was wedged — a false stranded-driver reading on the one channel that keeps #1675 visible. Raised to clear the drain cap.

Measured, not asserted

Three runs — plain, under 12 spinning background loops, and under cargo llvm-cov nextest — on a 14-core darwin box:

Test plain +CPU load +llvm-cov Budget
bucket_overflow_… 0.009s 0.016s 0.018s none (pure)
…converges_after_peer_departs 0.276s 0.314s 0.393s 3s round
…converges_via_closer_nodes_second_round 0.312s 0.317s 0.379s 3s round, ×2
…negative_cache_expires_after_ttl 1.395s 1.411s 1.461s 750ms TTL
…tolerates_unreachable_peer 6.315s 6.337s 6.425s 12s guard

(Those figures are the pre-fix baseline. After the change …negative_cache_expires_after_ttl costs 0.42s and …tolerates_unreachable_peer is unchanged at ~6.39s.)

Worth being precise: the hang-guard mis-sizing is demonstrated; the negative-cache race is latent, not reproduced. Coverage amplification on this box is only ~1.2–1.4×, leaving roughly a 5× margin on the quantity at risk, and I could not make it fail locally. It is still worth taking because CI is a 4-core runner executing the whole workspace under llvm-cov — a far harsher amplifier — #1673 was attributed to exactly that starvation, and this fix makes the suite faster, so it costs nothing.

Deliberately not in this PR

Review follow-ups

Six findings from the review are closed in test(node,client-pull): close the #1832 review findings. Two are behavioural — the precondition hardening above, and moving CONVERGENCE_ROUND_TIMEOUT > DHT_CLIENT_TIMEOUT out of prose into a const _: () = assert! (the same shape node_origin uses to pin REFUSAL_SUPPRESSION_TTL's ceiling). The other four correct comments that asserted things the code does not do: the two-round derivation above, with_capacity_and_ttl's doc prescribing against three of its own module's unit tests, SHUTDOWN_TIMEOUT's claim to track a private constant in another crate, and the driver.rs claim to pin single-writer with a work future that holds no &store and cannot flush.

Four out-of-scope items are filed as #1840, including a pre-existing load-flake in client-pull's scheduler suite that reproduces on clean main.

Verification

  • cargo clippy --workspace --all-targets -- -D warnings — clean
  • cargo fmt -- --check — clean
  • cargo doc -p decdn-node --no-deps — clean
  • cargo nextest run -p decdn-node --no-fail-fast — 1038/1038 passing, no SLOW lines, no teardown exceeded lines
  • dht_integration green 20/20 in a repeat loop
  • cargo llvm-cov nextest -p decdn-node --test dht_integration — green

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.01% (was 82.00%, ▲ +0.01%) — 68155/83101 lines

File Coverage Lines Δ
crates/client-pull/src/driver.rs 91.35% 855/936 ▼ 0.01%
crates/node/src/dht/negative_cache.rs 95.92% 141/147 ● 0.00%
History
Commit Total Δ vs base
da85893 (HEAD) 82.01% ▲ +0.01%

yigitdot and others added 2 commits August 26, 2026 12:27
… budgets (#1830)

Three of #1830's seven claims survive verification against main. The rest
describe defects that #691, #1594/#1595 and #1779 already closed; the issue
read historical rationale in comments as live defects, so those comments go
too.

`find_providers_negative_cache_expires_after_ttl` raced its own fixture: the
injected 750ms TTL had to outlive a live loopback QUIC lookup before the
suppression assertion read it, or the entry expired early and the provider
was never suppressed. `tokio::time::pause` is unavailable there — the test is
multi_thread over real QUIC, and the cache is anchored on `std::time::Instant`
either way. Suppression now runs under the production TTL, which nothing in
the test can outlive, and expiry is forced on the one key under test by
re-recording it with a `Duration::ZERO` TTL: entries hold an absolute expiry,
`shift_insert` updates an existing key in place, and `contains_active` reads
`expiry <= now` as elapsed. That drops the 1100ms sleep with it — the test
goes from 1.40s to 0.35s. Wall-clock TTL passage stays covered by the
`negative_cache` unit tests and by `node_origin_pull`'s refusal suite.

One 3s `round_timeout` served both the scenario where a round must time out
and the two where a reachable peer must answer within it, making convergence
an assertion about loopback latency under llvm-cov. Split into a convergence
budget sitting just above `DHT_CLIENT_TIMEOUT` — so the per-RPC cap stays the
binding constraint — and the short budget named only where a timeout is the
point. The unreachable scenario's hang guard was 12s, exactly
`MAX_LOOKUP_ROUNDS × 3s`: a guard on the implementation's own worst case, not
headroom over it. It measures 6.4s (two rounds, not the one its comment
claimed) and now has a guard that clears the ceiling.

The file also tore endpoints down by hand — one unbounded `close().await` and
a fire-and-forget spawn that let the test return mid-close — which is the
hazard `support::shutdown` documents. It now routes through that helper, so a
stalled close is bounded and named instead of parking until the nextest
backstop, and a handler panic in the accept loop reaches the test that spawned
it rather than being discarded by the abort.

Last, `SHUTDOWN_TIMEOUT` equalled `ABANDON_DRAIN_CAP`, so one ceilinged
abandon drain could consume the whole teardown budget before the first close
was attempted and report `0/M endpoints closed` for a run in which nothing was
wedged — a false stranded-driver reading on the channel that keeps #1675
visible. It now clears the drain cap.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`interval_flush_persists_progress_before_completion` asserted a flush count
that only held if the runner cooperated. The work future sleeps 130ms on the
real clock against a 20ms flush interval, and `drive_with_interval_flush`
selects `biased` with completion first — so a runtime descheduled past the
work's deadline finds it already elapsed and returns, having flushed as few as
once. CI hit exactly that: `got 1`.

`start_paused` removes the wall clock. The interval's deadlines become the
only thing that can advance time, so the count is arithmetic: the immediate
first tick is consumed before the loop, leaving 20..=120ms inside the work's
130ms. Six, every time — which lets the assertion tighten from `> 1` to an
exact count, and that also pins the single-writer property the test claims to
check, since a work future that flushed too would push it over six.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@yigitdot
yigitdot force-pushed the fix/1830-deflake-dht-and-teardown branch from da85893 to f7487e8 Compare August 26, 2026 09:32
Six findings from the review of #1832. Two are behavioral; four correct
prose that asserted things the code does not do.

`TestServer::shutdown` now verifies its postcondition. `support::shutdown`
warns rather than fails on a deadline breach, because the stranded-driver
half of that breach is the known-open #1675. For teardown that is right.
At the one call site in `find_providers_tolerates_unreachable_peer` it is
not teardown but the scenario's PRECONDITION: on a breach the dead peer
stays up and the test passes having exercised nothing. `Endpoint::is_closed`
keeps that failure loud.

`CONVERGENCE_ROUND_TIMEOUT > DHT_CLIENT_TIMEOUT` moves from prose into a
`const _: () = assert!`. The ordering is the constant's whole purpose — a
`DHT_CLIENT_TIMEOUT` raised past it silently restores the round drain
pre-empting a peer the RPC layer still wants, with both convergence
scenarios back to measuring loopback latency and still green. Same shape
as `node_origin`'s `REFUSAL_SUPPRESSION_TTL` ceiling assertion.

The unreachable scenario spends ONE round, not two. Measured by varying
the constant: at 3s the test costs 6.4s, at 1s it costs 4.3s — a 2s cut
buys 2.1s, so one round is in the budget and the remaining ~3.4s is
fixture. The routing table holds two peers and `alpha` is 3, so round one
queries both and the next pass breaks on an empty batch BEFORE arming a
second round. `UNREACHABLE_HANG_GUARD` is unchanged: it is sized against
the `MAX_LOOKUP_ROUNDS` ceiling, which is what a hang guard should clear.

`with_capacity_and_ttl`'s doc no longer prescribes against three of its
own module's unit tests. TTL semantics and cache-wide TTL quantity are
both legitimate subjects for the seam; only an entry expired as a
PRECONDITION wants the `Duration::ZERO` idiom instead.

`SHUTDOWN_TIMEOUT` drops the claim to track `runtime::SHUTDOWN_DEADLINE`
— the values coincide but nothing couples them, and that constant is
private to another crate. `ABANDON_DRAIN_CAP` is the sole derivation.
The mechanism is corrected too: the drain runs on the pull leg's own
runtime thread, so the first close IS attempted and blocks inside it.

The `driver.rs` test does not pin the single-writer property: its work
future is a bare `sleep` holding no `&store`, so it cannot flush and the
count cannot detect one that does. The virtual clock also advances to the
work's own sleep deadline, not only the interval's.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@thiras
thiras merged commit 5cc801e into main Aug 26, 2026
@thiras
thiras deleted the fix/1830-deflake-dht-and-teardown branch August 26, 2026 16:28
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.

Flaky CI tests investigation

2 participants