Skip to content

Conversation

@pull
Copy link

@pull pull bot commented Jan 22, 2026

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.4)

Can you help keep this open source service alive? 💖 Please sponsor : )

## 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:

1. `get_headers()` to return an empty headers dict
2. `content_length` to be 0
3. The main loop to continue immediately back to `get_headers()`
4. An infinite loop consuming 100% CPU

## 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()`:**
```python
raw = STDIN.buffer.readline()
# Detect EOF: readline() returns empty bytes when input stream is closed
if raw == b"":
    raise EOFError("EOF reached while reading headers")
```

**In all callers (main loop, `handle_response()`, `custom_input()`):**
- Catch `EOFError` and exit gracefully with `sys.exit(0)`

## Key Insight

```python
# EOF: readline() returns empty bytes
io.BytesIO(b"").readline()  # Returns b''

# Empty line: readline() returns newline bytes
io.BytesIO(b"\r\n").readline()  # Returns b'\r\n'
```

## Testing

Added comprehensive unit tests in
`python_files/tests/test_python_server.py`:
- `test_get_headers_normal` - verifies normal header parsing still works
- `test_get_headers_eof_raises_error` - verifies EOF detection
- `test_get_headers_eof_mid_headers_raises_error` - EOF during header
reading
- `test_get_headers_empty_line_terminates` - empty line still terminates
headers
- `test_custom_input_exits_on_eof` - graceful exit from `custom_input()`
- `test_handle_response_exits_on_eof` - graceful exit from
`handle_response()`
- `test_main_loop_exits_on_eof` - simulates the main loop behavior
- `test_readline_eof_vs_empty_line` - documents the EOF vs empty line
distinction

All 8 tests pass.

## How to Verify

1. Open a Python file in VS Code
2. Use Shift+Enter to start a Native REPL session and run some commands
3. Close VS Code
4. Check for leftover processes: `ps aux | grep python_server`
5. With this fix, no leftover processes should remain
@pull pull bot locked and limited conversation to collaborators Jan 22, 2026
@pull pull bot added the ⤵️ pull label Jan 22, 2026
@pull pull bot merged commit cbcf5e1 into ConnectionMaster:main Jan 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant