Skip to content

test(node): de-flake probe rate-limit close-code test (#691) - #696

Merged
thiras merged 4 commits into
mainfrom
fix/691-probe-rate-limit-flake
May 29, 2026
Merged

thiras merged 4 commits into
mainfrom
fix/691-probe-rate-limit-flake

Conversation

@yigitdot

@yigitdot yigitdot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #691 — probe_rate_limit_returns_rate_limited_close_code flaked under cargo nextest run but passed under cargo 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 0x10 rejection. Two timing hazards lived there:

  • the second connection's establishment racing the first connection's teardown on the shared client endpoint, and
  • the accept loop assuming it receives exactly two Incomings.

Fix (test-only): drain the per-source bucket deterministically through the existing acquire_for_test hook 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_ip reliably charges the bucket on loopback — 200/200 (the per-source layer always fires; not the flake).
  • Pre-drain + single rejected connection observes 0x10 reliably — 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-pr agents):

  • Background accept_task now returns anyhow::Result and propagates 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.
  • The close assertion now matches the close reason bytes against RejectReason::PerSource, not just the 0x10 code — so a regression that rejected via the (also-0x10) global cap is caught.
  • Asserts per_source_tracked() == 1 to confirm the live connection charged the same pre-drained 127.0.0.1 key (a different key would be admitted, not rejected) — closes the formerly-unasserted key-binding assumption.
  • Reworded the refill-period comment to express 1 / per_source_rate_per_sec instead of a hard-coded 1000s.

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 failures
  • cargo nextest run -p decdn-node — 256/256 pass
  • cargo fmt --check + cargo clippy -p decdn-node --tests — clean

🤖 Generated with Claude Code

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]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment on lines +602 to 613
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;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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>(())
});

Comment thread crates/node/tests/probe_loopback.rs Outdated

client_ep.close().await;
let _ = accept_loop.await;
let _ = accept_task.await;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Propagate any errors or panics from the background accept_task to ensure they are not silently ignored at the end of the test.

Suggested change
let _ = accept_task.await;
accept_task
.await
.map_err(|e| anyhow::anyhow!("accept task join: {e}"))??;

@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 89.21% (was 89.21%, ● 0.00%) — 21537/24142 lines

No coverage data for changed Rust files.

History
Commit Total Δ vs base
95ca45d 88.84% ● 0.00%
53dd774 88.84% ● 0.00%
e0cc1e7 88.84% ● 0.00%
c14de40 (HEAD) 89.21% ● 0.00%

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]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_test and 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]>
@yigitdot

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@yigitdot
yigitdot requested a review from thiras May 29, 2026 17:02
@thiras
thiras merged commit c03bf63 into main May 29, 2026
19 checks passed
@thiras
thiras deleted the fix/691-probe-rate-limit-flake branch May 29, 2026 18:04
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.

test(node): probe_rate_limit_returns_rate_limited_close_code flakes under nextest

3 participants