Skip to content

fix: propagate caller cancellation from the batching dispatcher - #5804

Open
prawalpokharel wants to merge 2 commits into
bentoml:mainfrom
prawalpokharel:fix/batching-caller-cancellation
Open

prawalpokharel wants to merge 2 commits into
bentoml:mainfrom
prawalpokharel:fix/batching-caller-cancellation

Conversation

@prawalpokharel

Copy link
Copy Markdown

What does this PR address?

Fixes #5803

CorkDispatcher turns every asyncio.CancelledError into its fallback, which the service app raises as ServiceUnavailable("process is overloaded"). That is meant for jobs the dispatcher drops itself (load shedding, shutdown()). But TimeoutMiddleware stops a request at traffic.timeout by cancelling the task that runs the endpoint, and that cancellation is caught by the same except. 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. There fallback() returns a ServiceUnavailable, 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 through asyncio.shield, so cancelling the caller no longer cancels that future. If the future was cancelled, the dispatcher dropped the job: inbound_call raises a private _JobDropped, and _func turns that into the fallback as before. Otherwise the caller itself was cancelled, so the job is cancelled too and the CancelledError propagates. 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:

  • a cancelled caller should get CancelledError (main returns the fallback)
  • asyncio.wait_for should raise TimeoutError (main returns the fallback)
  • a request that hits TimeoutMiddleware should 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 the bento_new_sdk and bento_server_http e2e suites and monitoring e2e, apart from failures that also happen on main in my sandbox: the Docker-based container mode can't run there, and the two test_custom_command.py tests 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 -a passes, with the buf hooks skipped as on pre-commit.ci.

Not covered here:

Before submitting:

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
prawalpokharel requested a review from a team as a code owner November 17, 2026 18:58
@prawalpokharel
prawalpokharel requested review from jianshen92 and removed request for a team November 17, 2026 18:58

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: batchable API logs 'process is overloaded' for every request that hits traffic.timeout

1 participant