Fix python_server.py infinite loop on EOF (fixes #25620) #25746
Merged
+180
−7
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