Send Terminate message before closing TCP connection#86
Merged
SeanTAllen merged 1 commit intomainfrom Feb 11, 2026
Merged
Conversation
Previously, Session.close() hard-closed the TCP connection without
notifying the server. PostgreSQL clients should send a Terminate ('X')
message before disconnecting so the server can clean up resources
immediately rather than discovering the broken connection on next I/O.
The Terminate is sent from _ConnectedState.shutdown(), which covers
both _SessionConnected (authentication phase) and _SessionLoggedIn
(query phase). _SessionSSLNegotiating correctly skips Terminate since
the PostgreSQL protocol hasn't started during SSL negotiation.
The send is best-effort — if the connection is already broken, the
send silently fails and close proceeds normally.
Design: #72
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.
Session.close()now sends a Terminate (X) message to the PostgreSQL server before closing the TCP connection. Previously, the connection was hard-closed without notifying the server, leaving server-side resources to linger until the server detected the broken connection.The Terminate is sent from
_ConnectedState.shutdown(), covering both authentication and query phases._SessionSSLNegotiatingcorrectly skips it since the PostgreSQL protocol hasn't started during SSL negotiation. The send is best-effort — if the connection is already broken, it silently fails and close proceeds normally.Design: #72