Skip to content

fix(extension-core): report a refused retry at the deadline as a transport failure - #6201

Open
matthewgrossman wants to merge 8 commits into
mainfrom
fix/6196-macos-unreachable-service-flake/matthewgrossman
Open

matthewgrossman wants to merge 8 commits into
mainfrom
fix/6196-macos-unreachable-service-flake/matthewgrossman

Conversation

@matthewgrossman

@matthewgrossman matthewgrossman commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

A fail-closed middleware or gateway interceptor calls its service and sends the call again while the service refuses connections. When the service stays down, the caller should see "unavailable" with the transport failure. Instead it was sometimes reported as "timed out", depending on how long the last refused attempt took to come back. This is what makes the two tests in #6196 flaky on macOS runners.

Example, with a 356 ms deadline and a service that refuses every connection:

  1. Retries run at 0, 5, 15, 35, 75, 155, 255 and 355 ms.
  2. The last retry starts 1 ms before the deadline, and its refusal takes a few ms to arrive.
  3. The caller's timeout fires at 356 ms first, so the call reports "timed out" instead of the transport failure.

Behavior

A retry now starts only if it has room to answer before the deadline. Every retry that starts runs to completion and reports what happened.

Situation Before Now
Service refuses until the deadline Transport failure, or "timed out" if the last retry started too close to the deadline Always the transport failure
First attempt is slow and never answers Timed out Timed out (unchanged)
A retry connects and then hangs Timed out Timed out (unchanged)
Call without a deadline Retries start for 5 s, and a started retry runs to completion Unchanged

A slow service is a timeout, and a service that keeps refusing is a transport failure. The last retry no longer starts so late that its refusal can lose the race against the caller's timeout.

Related Issue

Closes #6196

Changes

  • call_with_retries starts a retry only if at least the pause it just waited remains before the deadline (now + 2 * pause <= give_up). With the 356 ms deadline above, the 355 ms retry is not started, and the 255 ms retry keeps about 100 ms to answer.
  • This is a margin, not a proof: a refusal must arrive within that headroom, which is 5 to 100 ms against the few ms the flake needed.
  • Retries are not wrapped in any timeout, so there are no nested timers to order.
  • Adds paused-clock regression tests for the 356 ms schedule, a retry that hangs after a refusal, and a no-deadline retry that answers after the window. The two stop-time assertions allow two max pauses instead of one.

Testing

  • cargo nextest for openshell-extension-core, openshell-gateway-interceptors, and openshell-supervisor-middleware pass; fmt and clippy clean
  • Unit tests added/updated
  • Stress run of both flaky tests on macOS at background QoS (taskpolicy -b, -j 4): 4/300 failures before (same errors as CI), 0/600 after (300 iterations of both tests on the final code)

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

@matthewgrossman matthewgrossman added area:runtime Sandbox execution, isolation, supervision, policy enforcement, networking, and provider lifecycle. topic:extensions Extension APIs and integration points, consistent design, and external use cases. topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts. labels Dec 3, 2026
…sport failure

Bound each retry by the deadline, or the fixed window when there is none, in a single loop, and abandon it with the earlier transport failure. The first attempt stays the caller's to cut short.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
@matthewgrossman
matthewgrossman force-pushed the fix/6196-macos-unreachable-service-flake/matthewgrossman branch from cb6855b to 9ca5747 Compare December 3, 2026 20:35
The backoff wakes in time, but the refusal arrives after the deadline, so the caller's timeout would otherwise win the race.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
…e deadline

The doc comment promises no attempt starts after the deadline, but a backoff that woke at or after it still started one.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Replace call_with_retries with call_before, which bounds every attempt by the deadline itself and returns Elapsed only for a first attempt that runs out of time. Callers no longer wrap it in a second timeout_at at the same instant, so the result no longer depends on which timer is polled first. The fail-open choice moves in as Unprocessed::Fail, the deadline is required, and the fixed retry window is gone.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Out of scope for the flake fix in #6196. The one-file fix before it covers both races; the wider restructure can be proposed separately.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Run each retry under timeout_at(give_up) in a single loop, with a strict pre-sleep bound, so a retry that overruns the deadline reports the failure before it instead of racing the caller's timeout. Drop the overlapping latency sweep test.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>

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

Tested on macOS arm64 at b15c1be. cargo test -p openshell-extension-core --lib transport passes. "Without the fix" below is this tree with the retry_until call replaced by call().await.

Two cases behave differently from the description.

1. A retry the service accepted is reported as refused (transport.rs:229)

retry_until cannot tell a retry that is still connecting from one the service is already handling. Paused-clock probe: 500 ms deadline, first attempt refused, second attempt sleeps 10 s then answers.

  • This PR: attempts=2 result=Ok(Err(Status { code: Unknown, message: "connect failure", source: Some(Custom { kind: ConnectionRefused, .. }) }))
  • Without the fix: attempts=2 result=Err(Elapsed(()))

So a service that restarts and then hangs is reported as unreachable while a request is in flight. The description says a slow service is still reported as a timeout; that holds only for the first attempt.

2. Calls without a deadline are cut at the 5 s window (transport.rs:222-223)

Probe: call_with_retries(None, ..), first attempt refused, retry answers after 6 s.

  • This PR: elapsed=5s result=Err(.. "connect failure" ..)
  • Without the fix: elapsed=6.005s result=Ok("answer")

The description says calls without a deadline are unchanged. Should the retry be bounded only when deadline is Some?

shiju-nv
shiju-nv previously approved these changes Dec 3, 2026
status = match tokio::time::timeout_at(give_up, call()).await {
Ok(Ok(response)) => return Ok(response),
Ok(Err(status)) => status,
Err(_) => break,

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.

🤖 review:

Minor: This also changes the outcome for a retry that did connect. Example with a 500 ms fail-closed stage while the service restarts: the attempt at 0 ms is refused, the retry at 5 ms reaches the new instance, and its handler takes 600 ms. At 500 ms the retry is dropped here and the 0 ms refusal is returned. call_before_deadline then yields Ok(Err(status)), so the chain reports the unavailable reason instead of StageTimeout/ChainTimeout, and the gateway reports the connect failure instead of "evaluation timed out".

The request is still denied either way, but this is the "slow service" case the description says should stay a timeout, and the returned status says the call was not processed when the service did receive it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch, and you're right: dropping a retry that had connected and was slow reported it as the earlier refusal. I've changed the approach. The retry is no longer wrapped in a timeout. Instead a retry starts only if at least the pause it just waited remains before the deadline, so every retry that starts runs to completion and reports what happened. A retry that connects and hangs is a timeout again, like a first attempt. a_retry_that_hangs_is_left_to_the_caller covers it, and it fails on the previous version.

Drop the per-retry timeout_at. A retry now starts only when at least the pause it waited remains before the deadline, so every retry runs to completion and reports what happened: a refusal is a transport failure, and a retry that connected and hangs is a timeout, as for a first attempt. Calls without a deadline are no longer cut at the fixed window.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
@matthewgrossman

Copy link
Copy Markdown
Member Author

@137137137 Both points are right and both are fixed by not starting a retry without headroom, instead of cutting retries off with a timeout.

  1. A retry that connected and is slow is no longer abandoned and mislabelled as the earlier refusal. It runs to completion, so it is reported as a timeout, as before the PR. Test: a_retry_that_hangs_is_left_to_the_caller.
  2. With no deadline, the 5 s window only limits when a retry starts, so a retry that answers at 6 s succeeds again. Test: a_retry_started_inside_the_window_may_answer_after_it.

The trade-off is that this is a margin, not a proof: a refusal has to arrive within the headroom, 5 to 100 ms against the few ms the flake needed. The PR description now says so.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
@137137137

Copy link
Copy Markdown

Re-ran both probes at f5111d0. The slow retry with a 500 ms deadline now gives Err(Elapsed(())), and the no-deadline retry that answers at 6 s gives elapsed=6.005s result=Ok("answer"), both the same as before the PR. cargo test -p openshell-extension-core --lib transport passes, 18 tests. Nothing further from me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:runtime Sandbox execution, isolation, supervision, policy enforcement, networking, and provider lifecycle. topic:extensions Extension APIs and integration points, consistent design, and external use cases. topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(test): middleware and interceptor unreachable-service tests flake on macOS and fail the merge queue

3 participants