Repository navigation
fix: propagate caller cancellation from the batching dispatcher - #5804
Open
prawalpokharel wants to merge 2 commits into
Open
prawalpokharel wants to merge 2 commits into
prawalpokharel wants to merge 2 commits into
Conversation
A caller that is cancelled while it waits for its batch should see its own cancellation, and a request that hits traffic.timeout should be answered once, with the 504. Only a job the dispatcher drops itself (load shedding or shutdown) should get the fallback. Three of these tests fail without the dispatcher fix: the cancelled caller, the timed-out caller and the request behind TimeoutMiddleware. The shedding and shutdown tests pass either way and guard the fallback.
CorkDispatcher turned every CancelledError into the overload fallback,
including the one raised when the calling task itself is cancelled.
With traffic.timeout, a request to a batchable API that timed out got
its 504 from TimeoutMiddleware. Its endpoint then raised
ServiceUnavailable ("process is overloaded"), which was logged as an
error, and tried to answer again with a 503.
Await the job's future through asyncio.shield, so that cancelling the
caller does not cancel the future. If the future was cancelled, the
dispatcher dropped the job and the caller still gets the fallback.
Otherwise the caller was cancelled: cancel the job too and let the
cancellation propagate. This does not need Task.cancelling(), which
only exists from Python 3.11.
prawalpokharel
requested review from
jianshen92
and removed request for
a team
November 17, 2026 18:58
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR address?
Fixes #5803
CorkDispatcherturns everyasyncio.CancelledErrorinto its fallback, which the service app raises asServiceUnavailable("process is overloaded"). That is meant for jobs the dispatcher drops itself (load shedding,shutdown()). ButTimeoutMiddlewarestops a request attraffic.timeoutby cancelling the task that runs the endpoint, and that cancellation is caught by the sameexcept. So every request to a batchable API that times out is logged as an overload error with a traceback, and the endpoint then tries to answer again with a 503 after the 504 has gone out. The issue has the reproduction and the log.The legacy runner server (
runner_app.py) uses the same dispatcher behind the same middleware. Therefallback()returns aServiceUnavailable, and the handler turns it into a "Service Busy" 503, so a timed-out runner request also tries to answer again after the 504. With this change the cancellation propagates there too. The dispatcher tests cover that, but I haven't run a legacy runner end to end.The change is in
inbound_call: it awaits the job's future throughasyncio.shield, so cancelling the caller no longer cancels that future. If the future was cancelled, the dispatcher dropped the job:inbound_callraises a private_JobDropped, and_functurns that into the fallback as before. Otherwise the caller itself was cancelled, so the job is cancelled too and theCancelledErrorpropagates.Task.cancelling()would be shorter, but it only exists from Python 3.11. I checked this on 3.9, 3.11 and 3.12, the versions in the CI matrix.Tests are in
tests/unit/_internal/marshal/test_dispatcher_caller_cancel.py. Three fail on main:CancelledError(main returns the fallback)asyncio.wait_forshould raiseTimeoutError(main returns the fallback)TimeoutMiddlewareshould be answered once (on main the endpoint also tries to send a 503 after the 504)Two controls pass before and after: the real controller dropping an old job, and
shutdown(). I ran the new tests 100 times each on 3.9, 3.11 and 3.12, also pinned to one busy CPU. The existing unit tests pass on 3.9, 3.11 and 3.12 with and without the change. So do thebento_new_sdkandbento_server_httpe2e suites and monitoring e2e, apart from failures that also happen on main in my sandbox: the Docker-basedcontainermode can't run there, and the twotest_custom_command.pytests are flaky there (16 of 50 runs failed on main, 13 of 50 with this change). Their services have no batchable API, so they never reach the dispatcher.pre-commit run -apasses, with the buf hooks skipped as on pre-commit.ci.Not covered here:
TimeoutMiddlewarestill leaves the task it cancelled running without awaiting it, so any other handler that turns a cancellation into a response could still answer twice. fix(server): propagate errors through TimeoutMiddleware #5670, fix: cancel ASGI application when a waiting request is cancelled #5772 and fix: cancel TimeoutMiddleware timer when the request finishes #5773 already touch that file.Before submitting:
guide on how to create a pull request.
pre-commit run -ascript has passed (instructions)?those accordingly? Here are documentation guidelines and tips on writting docs.