Skip to content

Send the CancelRequest before closing the socket - #2

Closed
Sanguinor88 wants to merge 1 commit into
hyphatech:mainfrom
OSU-Dev:cancel-before-close
Closed

Sanguinor88 wants to merge 1 commit into
hyphatech:mainfrom
OSU-Dev:cancel-before-close

Conversation

@Sanguinor88

Copy link
Copy Markdown
Contributor

When a statement was given up on (a timeout, a cancellation, or a raise from the caller's callback), stop closed the connection first and only then sent the CancelRequest for it. The server saw the session end before the cancel arrived, and a pooling proxy can drop a cancel for a client session that has already disconnected, so the statement went on running for nobody.

stop now sends the cancel first and closes the connection in a finally, so the connection is closed whatever the cancel does. The cancel opens a connection of its own and reads nothing from this one's stream, which no exchange reads once it is given up on; it keeps its shield and its bound of 5 s or the timeout, so a caller waits no longer than before, and a failed cancel is still ignored. The new case, 54.2.8 a cancel is sent before the connection closes, has a fake server note whether the session is still open when the CancelRequest arrives. The docs now give the order: cancelled, then closed.

When a statement was given up on (a timeout, a cancellation, or a raise
from the caller's callback), stop closed the connection first and only
then sent the CancelRequest for it. The server saw the session end
before the cancel arrived, and a pooling proxy can drop a cancel for a
client session that has already disconnected, so the statement went on
running for nobody.

stop now sends the cancel first and closes the connection in a finally,
so the connection is closed whatever the cancel does. The cancel opens a
connection of its own and reads nothing from this one's stream, which no
exchange reads once it is given up on; it keeps its shield and its bound
of 5 s or the timeout, so a caller waits no longer than before, and a
failed cancel is still ignored. The new case, 54.2.8 a cancel is sent
before the connection closes, has a fake server note whether the session
is still open when the CancelRequest arrives. The docs now give the
order: cancelled, then closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zaiev

zaiev commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thanks, the bug is real and the fix is the right direction. We couldn't push to your branch (edits by maintainers are off), so it continues in #6 with your commit as the author. One change on top: the connection now refuses new statements before the cancel goes out, otherwise a statement sent in that window could run, be cancelled instead of the given-up one, or be reported as lost. There's a test for it.

@zaiev zaiev closed this Dec 3, 2026
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.

2 participants