Handle read() returning None on a non-blocking socket - #839
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
Under a non-blocking socket (cheroot supports this via timeout=0), io.BufferedReader.read() can legitimately return None instead of b'' when there's no data available yet - that's documented behavior in _pyio's raw IO contract, not an error case. StreamReader.read() didn't account for this and crashed with TypeError: object of type 'NoneType' has no len() the moment a non-blocking read came back empty, which in practice meant it could blow up mid-request whenever the client hadn't sent anything yet. Fixes cherrypy#278.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #839 +/- ##
==========================================
- Coverage 78.37% 78.21% -0.16%
==========================================
Files 41 41
Lines 4791 4802 +11
Branches 548 550 +2
==========================================
+ Hits 3755 3756 +1
- Misses 899 906 +7
- Partials 137 140 +3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #278.
When cheroot serves with a non-blocking socket (
timeout=0),io.BufferedReader.read()can legitimately returnNoneinstead ofb''when there's nothing to read yet - that's the documented contract forio.RawIOBasein non-blocking mode, not an error condition.StreamReader.read()overridesread()to trackbytes_readbut didn't account for this, so it crashed withTypeError: object of type 'NoneType' has no len()any time a non-blocking read came back with nothing available.I confirmed this is still live on current
main, not just a Python 2 quirk as the original report speculated - reproduced it with a real non-blocking socket pair (no data sent yet, read() blows up immediately).The fix just skips the byte count update when
valisNoneand returns it unchanged, so the rest of the io stack still sees the documented would-block signal it already knows how to handle.Added a regression test using the existing
MockSocketpattern (itsrecv_intonow raisesBlockingIOErrorto simulate the non-blocking-no-data case) - fails on unmodifiedmakefile.pywith the exact same TypeError from the issue, passes with the fix. Ran the fulltest_makefile.py,test_conn.py, andtest_wsgi.pysuites before and after, all green.