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.
Summary
Fixes #25620 - Leftover process python_server.py with 100% CPU after closing VS Code.
The Problem
When VS Code closes, the STDIN stream to python_server.py is closed. The readline() method returns empty bytes (b'') to signal EOF. However, the previous code incorrectly treated this as an empty line separator, causing:
The Fix
This PR properly detects EOF by checking for b'' (empty bytes) vs b'\r\n' or b'\n' (actual empty line with newline characters):
In get_headers():
In all callers (main loop, handle_response(), custom_input()):
Key Insight
Testing
Added comprehensive unit tests in python_files/tests/test_python_server.py:
All 8 tests pass.
How to Verify