Repository navigation
Let async terminal handlers finish during graceful disconnect - #307
Open
codebanditssss wants to merge 1 commit into
Open
codebanditssss wants to merge 1 commit into
codebanditssss wants to merge 1 commit into
Conversation
This was referenced Nov 19, 2026
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.
Summary
Let async terminal handlers finish within the remaining graceful-disconnect timeout. Preserve the stop signal and bounded cancellation. Avoid waiting on the write task when disconnect is called by that task. Include nine regression/boundary cases and the required patch-version bump.
The reader sets
_stop_eventbefore awaiting Termination or terminal Error handlers.disconnect(terminate=True)wakes on that signal and cancels the read task while a handler is suspended. Normalasync withexit is affected.Reproduction
Register an async Termination handler that waits 100 ms before recording completion. Connect and call
disconnect(terminate=True)against a local server that replies with Termination. Baseline cancels the callback in about 2 ms; fixed completes it in about 103 ms. An abnormal 1011 close reproduces the same problem for an async Error handler.Tests cover suspended handlers, context exit, handler exception isolation/order, nested disconnect, write-side self-wait, slow-handler cancellation and one shared read-side timeout budget.
Validation
Handlers exceeding the timeout can still be cancelled. Cancellation-suppressing handlers can prolong shutdown. This is not a strict whole-disconnect wall-clock bound.
PR #303 concerns unreadable server messages, not cancellation of valid terminal callbacks during shutdown.