Fix python_server.py infinite loop on EOF (fixes #25620)#25746
Merged
anthonykim1 merged 1 commit intomainfrom Jan 22, 2026
Merged
Fix python_server.py infinite loop on EOF (fixes #25620)#25746anthonykim1 merged 1 commit intomainfrom
anthonykim1 merged 1 commit intomainfrom
Conversation
When VS Code closes, the STDIN stream closes and readline() returns empty bytes (b''). Previously this was incorrectly treated as an empty line separator, causing an infinite loop with 100% CPU usage. This fix: - Detects EOF in get_headers() by checking for b'' and raising EOFError - Handles EOFError in all three places that call get_headers(): - The main loop - handle_response() - custom_input() - Exits gracefully with sys.exit(0) when EOF is detected The key insight is distinguishing between: - EOF: readline() returns b'' (empty bytes) - Empty line: readline() returns b'\r\n' or b'\n' (newline bytes) Also added comprehensive unit tests to verify the fix.
DonJayamanne
approved these changes
Jan 22, 2026
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.
Summary
Fixes #25620 - Leftover process
python_server.pywith 100% CPU after closing VS Code.The Problem
When VS Code closes, the STDIN stream to
python_server.pyis closed. Thereadline()method returns empty bytes (b'') to signal EOF. However, the previous code incorrectly treated this as an empty line separator, causing:get_headers()to return an empty headers dictcontent_lengthto be 0get_headers()The Fix
This PR properly detects EOF by checking for
b''(empty bytes) vsb'\r\n'orb'\n'(actual empty line with newline characters):In
get_headers():In all callers (main loop,
handle_response(),custom_input()):EOFErrorand exit gracefully withsys.exit(0)Key Insight
Testing
Added comprehensive unit tests in
python_files/tests/test_python_server.py:test_get_headers_normal- verifies normal header parsing still workstest_get_headers_eof_raises_error- verifies EOF detectiontest_get_headers_eof_mid_headers_raises_error- EOF during header readingtest_get_headers_empty_line_terminates- empty line still terminates headerstest_custom_input_exits_on_eof- graceful exit fromcustom_input()test_handle_response_exits_on_eof- graceful exit fromhandle_response()test_main_loop_exits_on_eof- simulates the main loop behaviortest_readline_eof_vs_empty_line- documents the EOF vs empty line distinctionAll 8 tests pass.
How to Verify
ps aux | grep python_server