Prevent WebSocket._thread from speed-reading closed sockets#3
Open
rdash-fenix wants to merge 1 commit intoch-iv:mainfrom
Open
Prevent WebSocket._thread from speed-reading closed sockets#3rdash-fenix wants to merge 1 commit intoch-iv:mainfrom
rdash-fenix wants to merge 1 commit intoch-iv:mainfrom
Conversation
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.
Noticed that a program I was writing in flask had a very high CPU usage,
py-spyand some tinkering led me to discover it was an endless loop in WebSocket._thread. I could repeatably cause the fault condition by opening the endpoint incurland then killing it so it could not send a close message.Per python's socket documentation, calls to
recv()on a closed socket do not yield anOSErrorlike one would expect, but instead simply return an emptybytesinstance. Between this and wsproto's parser just returningNonewhen it receives an emptybytesinstance, you have a loop that pegs the CPU.My patch simply adds a check for an empty return, and injects the OSError the code is otherwise expecting. The other case where
recv()could return no bytes is if a zero-length datagram was received (which would not be possible/valid given websocket's use of TCP), so this simple check should be safe.Passed against the test suite, and functions correctly against both of the provided examples (as well as the codebase where I discovered it).