Skip to content

Cancel and drain in-flight ctx.run tasks before raising 409 on cancellation - #242

Closed
sharosoo wants to merge 1 commit into
restatedev:mainfrom
sharosoo:fix/restate-sdk-python-cancel-drain
Closed

sharosoo wants to merge 1 commit into
restatedev:mainfrom
sharosoo:fix/restate-sdk-python-cancel-drain

Conversation

@sharosoo

Copy link
Copy Markdown

Fixes #238.

Changes

  • Add Tasks.cancel_and_drain() in python/restate/server_context.py to cancel in-flight ctx.run tasks and await their unwinding (shielded against outer cancellation so child finally cleanup completes before propagating cancellation).
  • Await self.tasks.cancel_and_drain() in ServerInvocationContext.create_poll_or_cancel_coroutine when DoProgressCancelSignalReceived is received before raising TerminalError('cancelled', 409).
  • Add unit tests in tests/disconnect_hotloop.py verifying that in-flight ctx.run closures receive CancelledError and complete their finally cleanup before the handler catches TerminalError(409), including when the draining caller itself is cancelled.

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@sharosoo

Copy link
Copy Markdown
Author

Closing this PR for now: draining in-flight ctx.run tasks unconditionally (and shielding the drain across repeated cancellations) conflicts with the intended cancellation semantics clarified in restatedev/restate#5591. Leaving #238 open to discuss whether an opt-in or bounded hook is appropriate.

@sharosoo sharosoo closed this Oct 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support stopping async run closures before cancellation compensation starts

1 participant