Skip to content

Prevent socket write buffer from being cleared when prematurely#822

Open
cbbm142 wants to merge 4 commits into
cherrypy:mainfrom
cbbm142:main
Open

Prevent socket write buffer from being cleared when prematurely#822
cbbm142 wants to merge 4 commits into
cherrypy:mainfrom
cbbm142:main

Conversation

@cbbm142
Copy link
Copy Markdown

@cbbm142 cbbm142 commented May 18, 2026

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

  • 🐞 bug fix
  • 🐣 feature
  • 📋 docs update
  • 📋 tests/coverage improvement
  • 📋 refactoring
  • 💥 other

📋 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] becomes del 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)

  • I wrote descriptive pull request text above
  • I think the code is well written
  • I wrote good commit messages
  • I have squashed related commits together after
    the changes have been approved
  • Unit tests for the changes exist
  • Integration tests for the changes exist (if applicable)
  • I used the same coding conventions as the rest of the project
  • The new code doesn't generate linter offenses
  • Documentation reflects the changes
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 18, 2026

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.24%. Comparing base (2ffb0ba) to head (d3e8258).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #822      +/-   ##
==========================================
- Coverage   78.32%   78.24%   -0.08%     
==========================================
  Files          41       41              
  Lines        4788     4809      +21     
  Branches      547      549       +2     
==========================================
+ Hits         3750     3763      +13     
- Misses        900      906       +6     
- Partials      138      140       +2     

@cbbm142 cbbm142 force-pushed the main branch 2 times, most recently from 5cd058a to ebf303f Compare May 19, 2026 15:48
cbbm142 added 2 commits May 19, 2026 11:56
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
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.

Socket write buffer can be cleared prematurely causing truncated files

1 participant