Skip to content

fix: piggyback same-payload duplicate in-flight requests instead of 409 - #332

Merged
shigechika merged 2 commits into
mainfrom
fix/idempotent-duplicate-in-flight-id
Jul 24, 2026
Merged

shigechika merged 2 commits into
mainfrom
fix/idempotent-duplicate-in-flight-id

Conversation

@shigechika

Copy link
Copy Markdown
Owner

Summary

  • After a gateway restart or idle-reclaim, a client's reconnect burst can re-fire a request whose JSON-RPC id collides with itself while the first copy is still outstanding on the just-created session.
  • send_request now compares the incoming payload against the one already in flight under that id: an identical payload is piggybacked onto the existing pending slot (both callers observe the same backend reply, 200), a genuinely different payload still raises _DuplicateInFlightId (409) since it cannot be correlated.
  • This removes the operator-visible "tools fetch failed" churn on the first reconnect after every restart/idle-reclaim (option 1 from the issue's proposed fixes).

Test plan

  • pytest full suite: 1239 passed, 3 skipped
  • New unit test test_duplicate_in_flight_id_same_payload_piggybacks: two concurrent same-id/same-payload send_request calls against a slow (0.3s) backend return the identical reply and complete in ~0.3s total (proving only one dispatch reached the backend, not two serial ones)
  • New HTTP-level test test_duplicate_in_flight_id_same_payload_returns_200_over_http: same scenario through the actual gateway HTTP handler, asserts 200/200 with identical JSON bodies
  • Existing test_duplicate_in_flight_id_rejected updated to clarify it covers the DIFFERENT-payload case; still passes unchanged
  • ruff check clean on all changed files
  • Updated docs/guides/serve.md / serve.ja.md operator notes to describe the new same-payload-retry behavior

Closes #331

🤖 Generated with Claude Code

https://claude.ai/code/session_01YD76oeDYARdjhLQf93nkug

After a gateway restart or idle-reclaim, a client's reconnect burst can
re-fire a request whose JSON-RPC id collides with itself while the first
copy is still outstanding. Since the payload is identical, treat it as a
retry: attach it to the in-flight request's pending slot and return the
same backend reply, instead of rejecting it with 409 (which surfaced to
users as "tools fetch failed" on the first reconnect after every restart).
A genuinely different payload under the same id is still rejected, since
it cannot be correlated.

Closes #331
Address code-review findings on the piggyback change:

- Retire the shared pending slot from its LAST waiter (refcount) instead
  of the registering caller: an owner whose own timeout expires no longer
  orphans a still-waiting piggybacked retry (whose late reply leaked to
  SSE and which _fail_all could no longer wake, while has_pending read
  False and let the idle reaper reap the session mid-wait).
- Read slot["line"] under the lock on the way out, so a reply landing at
  a waiter's timeout boundary is still honored instead of diverging into
  a 504 for one caller and 200 for the other.
- Match retries semantically (parsed JSON equality, byte-equality fast
  path) so a client library that re-serializes with a different key
  order on retry still piggybacks instead of getting 409.
- Document the residual sequential window (a retry landing just after
  completion is indistinguishable from legitimate sequential id reuse
  and is dispatched anew) and the per-waiter deadline semantics in the
  docstring and en/ja operator guides.
- Tests: replace the flake-prone wall-clock assertion with a slow_echo
  dispatch counter (calls) proving exactly one copy reached the backend;
  poll for in-flight registration instead of a fixed sleep in the HTTP
  test; add owner-timeout-survival and re-serialized-retry tests; extract
  the _wait_in_flight helper shared by the spin-wait blocks.
@shigechika
shigechika force-pushed the fix/idempotent-duplicate-in-flight-id branch from 5049949 to d81658d Compare July 24, 2026 08:00
@shigechika
shigechika merged commit a8a7a09 into main Jul 24, 2026
12 checks passed
@shigechika
shigechika deleted the fix/idempotent-duplicate-in-flight-id branch July 24, 2026 08:05
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.

serve: first reconnect after restart/idle-reclaim often fails (409 duplicate-in-flight id during the re-init burst); a second reconnect succeeds

1 participant