-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add safe_close helper to standardize socket cleanup in tests #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
denini08
wants to merge
1
commit into
python-trio:main
Choose a base branch
from
denini08:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
add type fix type fix type
Member
|
I don't think it's true that shutdown first is better than simply closing?
There are patterns where it could make a difference like doing a shutdown
and then waiting for the other side to respond with a close before
finishing the close, if you have unpredictable duplex traffic + a protocol
with no GOAWAY message type. I can't think of when that scenario would come
up but it probably has for somebody at some point. But afaict a shutdown
immediately followed by a close doesn't solve anything or even behave
differently in any way than a simple close.
…On Sun, Jan 25, 2026, 06:56 Denini Gabriel ***@***.***> wrote:
Hi! First of all, thanks for the great work on this project.
While looking at the socket-based tests, I noticed that sockets are being
closed directly with close(). In most cases this works fine, but the
recommended pattern when dealing with sockets is to first call shutdown()
and then close(). This allows the connection to be terminated more
cleanly, ensuring that both read and write channels are properly closed
before releasing the resource.
According to the Python documentation:
shutdown() should be called before close() to signal that no more data
will be sent or received, and to allow the other side to handle the
connection termination properly.
https://docs.python.org/3/library/socket.html#socket.socket.shutdown
In tests, failures caused by not doing this are rare, but when they happen
they are usually hard to diagnose (for example, resource warnings, hanging
sockets, or platform-dependent behavior). Using a small helper like
safe_close makes the cleanup more explicit and consistent across the test
code.
This PR introduces a safe_close function that:
- Tries to call shutdown(SHUT_RDWR) first
- Falls back gracefully if the socket is already closed or in an
invalid state
- Always attempts to call close()
The main goal here is not to fix a concrete bug, but to improve the
cleanup pattern and make it more robust and standardized for current and
future tests. It also makes the intent clearer: sockets are always closed
in a safe and predictable way. This was identified during an ongoing
research project.
------------------------------
You can view, comment on, or merge this pull request online at:
#698
Commit Summary
- 36da767
<36da767>
Add safe_close function for socket cleanup in tests
File Changes
(1 file <https://github.com/python-trio/trustme/pull/698/files>)
- *M* tests/test_trustme.py
<https://github.com/python-trio/trustme/pull/698/files#diff-7f31a89310e2fd6438037acd7f0e5e1eadd05c8155193f69fd1d4aad05b1b375>
(35)
Patch Links:
- https://github.com/python-trio/trustme/pull/698.patch
- https://github.com/python-trio/trustme/pull/698.diff
—
Reply to this email directly, view it on GitHub
<#698>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEU42CXTP2EHDSN3VAOXE34ITKQFAVCNFSM6AAAAACS2MLZE6VHI2DSMVQWIX3LMV43ASLTON2WKOZTHA2TGMZWGYZDCOI>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Member
|
Oic the Python docs are talking about the case where you've dup'ed the fd
so there are multiple references to the underlying socket. In that case
close only decrements the refcount and doesn't affect any of the other
references, and shutdown can be used to immediately force the network state
transition that normally doesn't happen until the last reference is closed.
This is a pretty exotic situation or thing to want (arguably if you ever
get into this state then you should rethink your design) and I can't see
how it would every be relevant to our tests?
…On Sun, Jan 25, 2026, 06:56 Denini Gabriel ***@***.***> wrote:
Hi! First of all, thanks for the great work on this project.
While looking at the socket-based tests, I noticed that sockets are being
closed directly with close(). In most cases this works fine, but the
recommended pattern when dealing with sockets is to first call shutdown()
and then close(). This allows the connection to be terminated more
cleanly, ensuring that both read and write channels are properly closed
before releasing the resource.
According to the Python documentation:
shutdown() should be called before close() to signal that no more data
will be sent or received, and to allow the other side to handle the
connection termination properly.
https://docs.python.org/3/library/socket.html#socket.socket.shutdown
In tests, failures caused by not doing this are rare, but when they happen
they are usually hard to diagnose (for example, resource warnings, hanging
sockets, or platform-dependent behavior). Using a small helper like
safe_close makes the cleanup more explicit and consistent across the test
code.
This PR introduces a safe_close function that:
- Tries to call shutdown(SHUT_RDWR) first
- Falls back gracefully if the socket is already closed or in an
invalid state
- Always attempts to call close()
The main goal here is not to fix a concrete bug, but to improve the
cleanup pattern and make it more robust and standardized for current and
future tests. It also makes the intent clearer: sockets are always closed
in a safe and predictable way. This was identified during an ongoing
research project.
------------------------------
You can view, comment on, or merge this pull request online at:
#698
Commit Summary
- 36da767
<36da767>
Add safe_close function for socket cleanup in tests
File Changes
(1 file <https://github.com/python-trio/trustme/pull/698/files>)
- *M* tests/test_trustme.py
<https://github.com/python-trio/trustme/pull/698/files#diff-7f31a89310e2fd6438037acd7f0e5e1eadd05c8155193f69fd1d4aad05b1b375>
(35)
Patch Links:
- https://github.com/python-trio/trustme/pull/698.patch
- https://github.com/python-trio/trustme/pull/698.diff
—
Reply to this email directly, view it on GitHub
<#698>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEU42CXTP2EHDSN3VAOXE34ITKQFAVCNFSM6AAAAACS2MLZE6VHI2DSMVQWIX3LMV43ASLTON2WKOZTHA2TGMZWGYZDCOI>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
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.
Hi! First of all, thanks for the great work on this project.
While looking at the socket-based tests, I noticed that sockets are being closed directly with
close(). In most cases this works fine, but the recommended pattern when dealing with sockets is to first callshutdown()and thenclose(). This allows the connection to be terminated more cleanly, ensuring that both read and write channels are properly closed before releasing the resource.According to the Python documentation:
In tests, failures caused by not doing this are rare, but when they happen they are usually hard to diagnose (for example, resource warnings, hanging sockets, or platform-dependent behavior). Using a small helper like
safe_closemakes the cleanup more explicit and consistent across the test code.This PR introduces a
safe_closefunction that:shutdown()firstclose()The main goal here is not to fix a concrete bug, but to improve the cleanup pattern and make it more robust and standardized for current and future tests. It also makes the intent clearer: sockets are always closed in a safe and predictable way. This was identified during an ongoing research project.