Skip to content

Conversation

@denini08
Copy link

@denini08 denini08 commented Jan 25, 2026

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() 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.

@njsmith
Copy link
Member

njsmith commented Jan 26, 2026 via email

@njsmith
Copy link
Member

njsmith commented Jan 26, 2026 via email

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