Repository navigation
Conversation
BufferedWriter._flush_unlocked could raise OSError (EBADF) or ValueError when the underlying socket descriptor was already invalidated. This flush is frequently driven by the finalizer (IOBase.__del__ -> close -> flush) while tearing down abruptly closed TLS connections. The escaping exception taints the worker thread teardown path, gradually draining the thread pool and wedging the server while it otherwise appears to keep running. Swallow OSError/ValueError during flush (dropping the unsendable buffered bytes, since the connection is gone) so a dead connection cannot take a worker thread with it. Fixes cherrypy#710 Signed-off-by: Michael Stowe <mstowe@gmail.com>
1 of 3 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #843 +/- ##
==========================================
+ Coverage 78.16% 78.31% +0.14%
==========================================
Files 41 41
Lines 4791 4814 +23
Branches 548 549 +1
==========================================
+ Hits 3745 3770 +25
+ Misses 906 905 -1
+ Partials 140 139 -1 |
Documentation build overview
10 files changed ·
|
- Whitelist 'finalizer' and 'teardown' in docs/spelling_wordlist.txt (used in the changelog fragment and test docstrings). - Ignore DAR401 for cheroot/test/test_makefile.py, consistent with the existing per-file darglint suppressions for mock socket methods (darglint cannot resolve the dynamically raised 'self._exc'). Signed-off-by: Michael Stowe <mstowe@gmail.com>
This branch has not been deployed
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.
What kind of change does this PR introduce?
What do these changes do?
BufferedWriter._flush_unlockedincheroot/makefile.pyonly guardsio.BlockingIOError. When a connection's underlying socket descriptorhas already been closed/invalidated, flushing the remaining buffered
bytes calls
self.raw.write(...)on a dead fd and raisesOSError: [Errno 9] Bad file descriptor(orValueError: I/O operation on closed file).This flush is very commonly driven by the finalizer
(
IOBase.__del__→close→flush) while tearing down abruptlyclosed TLS connections (client abort, timeout). Letting the exception
escape a finalizer taints the worker thread's teardown path; over time
worker threads die and are not replaced, draining the thread pool until
the server stops accepting/serving requests while otherwise appearing
to run.
This PR catches
OSError/ValueErrorin_flush_unlocked, drops theunsendable buffered bytes (the connection is gone, there is nowhere to
send them), and returns cleanly.
Regression tests are added in
cheroot/test/test_makefile.py(
test_flush_on_dead_socket_is_swallowed,test_close_on_dead_socket_is_swallowed). They fail without the fixand pass with it.
Are there changes in behavior for the user?
Only in the failure path: a buffered write/flush to an
already-dead socket no longer propagates an exception. Successful
writes are unaffected. This prevents the gradual thread-pool
exhaustion / server "wedge" that manifests downstream (e.g. SABnzbd's
web UI going blank while the backend keeps working).
Is it a substantial burden for the maintainers to support this?
No. The change is small (a single additional
exceptclause), local to_flush_unlocked, and covered by focused unit tests using the existingMockSockettest style.Related issue number
Fixes #710 (also relevant to the family of reports around
OSError/SSLEOFErrorin cheroot's SSL socket teardown, e.g.sabnzbd/sabnzbd#1429).
Checklist
docs/changelog-fragments.d/