Repository navigation
test(node): de-flake probe rate-limit close-code test (#691) - #696
Conversation
The two-connection drain-then-reject form raced under nextest: the second connection's establishment competed with the first's teardown on the shared client endpoint, and the accept loop assumed exactly two Incomings. Drain the per-source bucket out-of-band via acquire_for_test and assert on a single unconditionally-rejected connection — same wire coverage (serve -> acquire -> reject -> close 0x10), no timing races. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors the probe_rate_limit_returns_rate_limited_close_code test to make it deterministic and prevent flakiness under nextest. Instead of relying on two racing connections, it pre-drains the rate limiter's bucket out-of-band and verifies that a single incoming connection is rejected. The review feedback suggests improving error handling within the background accept_task by returning an anyhow::Result and properly propagating any errors or panics when joining the task at the end of the test.
| let accept_task = tokio::spawn(async move { | ||
| let Some(incoming) = server_ep_bg.accept().await else { | ||
| return; | ||
| }; | ||
| let Ok(connecting) = incoming.accept() else { | ||
| return; | ||
| }; | ||
| let Ok(conn) = connecting.await else { | ||
| return; | ||
| }; | ||
| let _ = handler_bg.accept(conn).await; | ||
| }); |
There was a problem hiding this comment.
The background accept_task silently discards any errors or panics that occur during connection acceptance or handling. Returning an anyhow::Result<()> (similar to the pattern used in probe_roundtrip on line 204) ensures that any server-side failures are captured and can be explicitly propagated to fail the test with a clear error message.
| let accept_task = tokio::spawn(async move { | |
| let Some(incoming) = server_ep_bg.accept().await else { | |
| return; | |
| }; | |
| let Ok(connecting) = incoming.accept() else { | |
| return; | |
| }; | |
| let Ok(conn) = connecting.await else { | |
| return; | |
| }; | |
| let _ = handler_bg.accept(conn).await; | |
| }); | |
| let accept_task = tokio::spawn(async move { | |
| let incoming = server_ep_bg | |
| .accept() | |
| .await | |
| .ok_or_else(|| anyhow::anyhow!("no incoming connection"))?; | |
| let connecting = incoming | |
| .accept() | |
| .map_err(|e| anyhow::anyhow!("accept: {e}"))?; | |
| let conn = connecting | |
| .await | |
| .map_err(|e| anyhow::anyhow!("handshake: {e}"))?; | |
| handler_bg | |
| .accept(conn) | |
| .await | |
| .map_err(|e| anyhow::anyhow!("{e}"))?; | |
| Ok::<_, anyhow::Error>(()) | |
| }); |
|
|
||
| client_ep.close().await; | ||
| let _ = accept_loop.await; | ||
| let _ = accept_task.await; |
Coverage reportTotal: 89.21% (was 89.21%, ● 0.00%) — 21537/24142 lines No coverage data for changed Rust files. History
|
Address review on #696: return anyhow::Result from the background accept task and propagate via ?? at join, matching probe_roundtrip / spin_up_probe_harness, so a server-side accept/handshake/handler failure surfaces with a precise message instead of being silently swallowed. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR de-flakes the probe_rate_limit_returns_rate_limited_close_code integration test by removing a timing-sensitive two-connection setup that could race under cargo nextest run, while preserving the same wire-level assertion that the server closes with APP_ERR_RATE_LIMITED (0x10).
Changes:
- Refactors the rate-limit close-code test to pre-drain the per-source bucket via
ConnectionLimiter::acquire_for_testand then drive a single deterministically-rejected connection. - Simplifies the server-side accept logic from a “loop expecting two incoming connections” to a single-connection accept task with explicit error propagation.
- Updates test documentation/comments to describe the new deterministic structure and the prior flake root cause.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address PR #696 review (silent-failure-hunter, pr-test-analyzer): the close-code-only assertion proved "closed with 0x10" but not that the per-source layer fired, and the live connection's key binding to the pre-drained 127.0.0.1 bucket was an unasserted assumption. - Match the close reason bytes against RejectReason::PerSource so a regression that rejected via the (also-0x10) global cap is caught. - Assert per_source_tracked() == 1 to confirm the live connection charged the same pre-drained key (a different key is admitted, not rejected). - Reword the refill-period comment to express the rate relationship instead of a hard-coded 1000s (comment-analyzer). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request refactors the probe_rate_limit_returns_rate_limited_close_code test in crates/node/tests/probe_loopback.rs to eliminate flakiness under nextest. Instead of relying on two racing back-to-back connections to trigger rate limiting, the test now pre-drains the per-source bucket for the loopback address out-of-band using a test hook and verifies that a single subsequent connection is rejected with the expected rate-limit error code and reason. There are no review comments, and I have no feedback to provide.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Summary
Fixes #691 —
probe_rate_limit_returns_rate_limited_close_codeflaked undercargo nextest runbut passed undercargo test.Root cause (the issue's clock/static-state guesses were red herrings — the governor refill period is 1000s, so no token refills mid-test): the flake was structural in the test setup. The old test drained the per-source bucket with a throwaway first connection, then opened a second connection on the same client endpoint expecting the
0x10rejection. Two timing hazards lived there:Incomings.Fix (test-only): drain the per-source bucket deterministically through the existing
acquire_for_testhook for the loopback source key, then drive a single connection that is unconditionally rejected. Same wire-level coverage (serve → acquire → reject → close(0x10)), no second connection, no timing race.Investigation evidence
peer_ipreliably charges the bucket on loopback — 200/200 (the per-source layer always fires; not the flake).0x10reliably — 300/300 under 2× CPU oversubscription (the rejection observation is solid; the two-connection setup was the problem).Review-driven hardening
Addressed comments from the automated reviewers (gemini-code-assist +
/review-pragents):accept_tasknow returnsanyhow::Resultand propagates via??at join (matchingprobe_roundtrip/spin_up_probe_harness), so a server-side accept/handshake/handler failure surfaces with a precise message instead of being silently swallowed.RejectReason::PerSource, not just the0x10code — so a regression that rejected via the (also-0x10) global cap is caught.per_source_tracked() == 1to confirm the live connection charged the same pre-drained127.0.0.1key (a different key would be admitted, not rejected) — closes the formerly-unasserted key-binding assumption.1 / per_source_rate_per_secinstead of a hard-coded1000s.Test plan
probe_rate_limit_returns_rate_limited_close_code— 80×cargo nextest run+ 40×cargo test, plus 60× post-hardening, all under 2× CPU load, 0 failurescargo nextest run -p decdn-node— 256/256 passcargo fmt --check+cargo clippy -p decdn-node --tests— clean🤖 Generated with Claude Code