Prevent socket write buffer from being cleared when prematurely#822
Prevent socket write buffer from being cleared when prematurely#822cbbm142 wants to merge 5 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #822 +/- ##
==========================================
+ Coverage 78.32% 78.38% +0.06%
==========================================
Files 41 41
Lines 4788 4839 +51
Branches 547 552 +5
==========================================
+ Hits 3750 3793 +43
- Misses 900 907 +7
- Partials 138 139 +1 |
5cd058a to
ebf303f
Compare
Add a check in cheroot.makefile.BufferedWriter._flush_unlocked to prevent clearing of the write buffer when raw.write() returns None due to a blocked stream. Also limits each write call to SOCK_WRITE_BLOCKSIZE bytes, which was defined but not previously used in this method.
Updated test to allow Flake8/pre-commit checks to pass successfully.
Updated allowlist to include buf, io, and RawIOBase
Added changelog entry for change cherrypy#822
avinashkamat48
left a comment
There was a problem hiding this comment.
This fixes the data-loss case when
aw.write() returns None once, but it can hang if the raw stream keeps returning None for the same non-blocking condition. In _flush_unlocked(),
stays None, no bytes are deleted, _write_buf remains non-empty, and the while self._write_buf: loop immediately retries forever. It would be safer to either raise/propagate a BlockingIOError or otherwise return control to the caller when no progress is made, rather than spinning inside flush.
When raw.write() returns None on a blocked non-blocking socket, use select() to wait up to SOCK_WRITE_TIMEOUT seconds for the socket to become writable before retrying. This avoids both the original silent buffer truncation bug and the infinite spin loop that the previous fix could produce. If the socket stays blocked past the timeout, raise BlockingIOError with the write buffer preserved. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@avinashkamat48 I have added a check to see if the socket becomes writable within 10 seconds. If it doesn't it raises the blocking error, otherwise it continues to try to send whatever is in the buffer. |
Adds a check in cheroot.makefile.BufferedWriter._flush_unlocked to prevent clearing of the write buffer when raw.write() returns None due to a blocked stream. Also limits each write call to SOCK_WRITE_BLOCKSIZE bytes, which was defined but not previously used in this method.
❓ What kind of change does this PR introduce?
Prevent socket write buffer from being cleared when it is blocked
📋 What is the related issue number (starting with
#)Resolves #821
❓ What is the current behavior? (You can also link to an open issue here)
#821
❓ What is the new behavior (if this is a feature change)?
N/A
📋 Other information:
This bug is hard to reproduce and spot in production since there are no tracebacks or log messages associated with it. It happens because n can be None, and then
del self._write_buf[:n]becomesdel self._write_buf[:]which will clear everything left in the buffer.📋 Contribution checklist:
(If you're a first-timer, check out
this guide on making great pull requests)
the changes have been approved
and description in grammatically correct, complete sentences