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 73.10924% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.16%. Comparing base (094088f) to head (2e35bcb).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #779      +/-   ##
==========================================
- Coverage   79.24%   79.16%   -0.08%     
==========================================
  Files          29       29              
  Lines        4201     4315     +114     
  Branches      538      550      +12     
==========================================
+ Hits         3329     3416      +87     
- Misses        728      755      +27     
  Partials      144      144              

@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
@julianz- julianz- force-pushed the fix-socket-teardown branch 2 times, most recently from 048d898 to f0471ca Compare October 7, 2025 03:27

class MockSocket:
"""A mock socket."""
"""A mock socket for emulating buffered I/O."""
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract this into a separate PR.

Copy link
Contributor Author

@julianz- julianz- Oct 15, 2025

Choose a reason for hiding this comment

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

Not sure I understand. The only thing modified here was the docstring. This was to distinguish MockSocket which was already there from MockRawSocket which I am adding. Originally, I added the latter in the same file but had to move to test_errors.py because the linter said I had too many modules in test_makefile.py.

@julianz- julianz- force-pushed the fix-socket-teardown branch 9 times, most recently from 9a6bb9f to 2e35bcb Compare October 16, 2025 04:02
@julianz- julianz- force-pushed the fix-socket-teardown branch 3 times, most recently from 90d7cb6 to 03ecb25 Compare October 16, 2025 06:03
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 03ecb25 to 9e1e189 Compare October 16, 2025 21:49
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