Skip to content

Conversation

julianz-
Copy link
Contributor

@julianz- julianz- commented Oct 5, 2025

Fixed race conditions to make socket I/O more resilient during connection teardown.

  1. BufferedWriter's write(): Added error handling to ignore common socket errors (e.g., ECONNRESET, EPIPE, ENOTCONN, EBADF) that occur when the underlying connection has been unexpectedly closed by the client or OS. This prevents a crash when attempting to write to a defunct socket.
  2. BufferedWriters's close(): Made idempotent, allowing safe repeated calls without raising exceptions.
  3. Needed to add explicit handling of WINDOWS environments as these are seen to throw Windows specific WSAENOTSOCK errors.

Includes new unit tests to cover the idempotency and graceful handling of already closed underlying buffers.


This change is Reviewable

@julianz- julianz- closed this Oct 5, 2025
@julianz- julianz- reopened this Oct 5, 2025
@julianz- julianz- changed the title Fix race condition and improve robustness during socket I/O [DRAFT] Fix race condition and improve robustness during socket I/O Oct 5, 2025
@julianz- julianz- force-pushed the fix-socket-teardown branch 2 times, most recently from 887399d to 4f1662e Compare October 5, 2025 19:04
Copy link

codecov bot commented Oct 5, 2025

Codecov Report

❌ Patch coverage is 64.28571% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.99%. Comparing base (e9b4361) to head (1c7c0c0).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #779      +/-   ##
==========================================
- Coverage   79.24%   78.99%   -0.26%     
==========================================
  Files          29       29              
  Lines        4197     4261      +64     
  Branches      538      549      +11     
==========================================
+ Hits         3326     3366      +40     
- Misses        727      750      +23     
- Partials      144      145       +1     

@julianz- julianz- force-pushed the fix-socket-teardown branch from 4f1662e to 4833dac Compare October 5, 2025 19:09
@julianz- julianz- marked this pull request as draft October 5, 2025 19:09
@julianz- julianz- changed the title [DRAFT] Fix race condition and improve robustness during socket I/O Fix race condition and improve robustness during socket I/O Oct 5, 2025
@julianz- julianz- changed the title Fix race condition and improve robustness during socket I/O Fix race conditions and improve robustness during socket I/O Oct 5, 2025
@julianz- julianz- marked this pull request as ready for review October 5, 2025 19:26

try:
super().close()
except (OSError, SysCallError) as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in a var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can reuse acceptable_sock_shutdown_error_codes?

assert faux_get_map.conn_closed


class TestBufferedWriter:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the class. This should go into test_makefile.py.

@@ -0,0 +1,2 @@
Fixed race conditions to make socket I/O more resilient during connection teardown.
-- by :user:`julianz-`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- by :user:`julianz-`
-- by :user:`julianz-`

else:
self.conn.wfile.write(chunk)
except (SysCallError, ConnectionError, OSError) as e:
error_code = e.errno if isinstance(e, OSError) else e.args[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a number of places where this is repeated. It's worth having a helper function, I think.

@julianz- julianz- force-pushed the fix-socket-teardown branch 2 times, most recently from 048d898 to f0471ca Compare October 7, 2025 03:27
@julianz- julianz- force-pushed the fix-socket-teardown branch 10 times, most recently from d23845a to 1c7c0c0 Compare October 7, 2025 17:31
Fixes to make socket I/O more resilient during connection teardown.

1. BufferedWriter's write(): Added error handling to ignore common
   socket errors (e.g., ECONNRESET, EPIPE, ENOTCONN, EBADF) that occur
   when the underlying connection has been unexpectedly closed by the
   client or OS. This prevents a crash when attempting to write to a
   defunct socket.
2. BufferedWriters's close(): Made idempotent, allowing safe repeated
   calls without raising exceptions.
3. Needed to add explicit handling of WINDOWS environments as these are
   seen to throw Windows specific WSAENOTSOCK errors.

Includes new unit tests to cover the idempotency and graceful handling
of already closed underlying buffers.
@julianz- julianz- force-pushed the fix-socket-teardown branch from 1c7c0c0 to 210d2a9 Compare October 7, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants