-
-
Notifications
You must be signed in to change notification settings - Fork 21
fix(render): stop at end of JSON payload instead of waiting for EOF #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """`cs render` must not outlive the payload it was handed. | ||
|
|
||
| `sys.stdin.buffer.read()` returns at EOF, and EOF needs *every* write handle | ||
| on the pipe to close — not just the shell Claude Code spawned the statusLine | ||
| through. On Windows a sibling process that inherited the handle keeps the pipe | ||
| open after that shell exits, so the render process blocks forever with the | ||
| complete payload already in memory. | ||
| """ | ||
|
|
||
| import json | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from claude_statusbar import render_thin | ||
|
|
||
|
|
||
| PAYLOAD = json.dumps({ | ||
| "session_id": "abc-123", | ||
| "model": {"id": "claude-opus-5", "display_name": "Opus 5"}, | ||
| "context_window": {"context_window_size": 1_000_000, | ||
| "used_percentage": 6, | ||
| "total_input_tokens": 63_824}, | ||
| }).encode("utf-8") | ||
|
|
||
|
|
||
| class _FakeBuffer: | ||
| """Hands out `chunks`, then blocks — standing in for a pipe that never | ||
| reaches EOF because someone else still holds the write end.""" | ||
|
|
||
| def __init__(self, chunks): | ||
| self._chunks = list(chunks) | ||
| self.reads = 0 | ||
|
|
||
| def read1(self, _size): | ||
| self.reads += 1 | ||
| if self._chunks: | ||
| return self._chunks.pop(0) | ||
| raise AssertionError("read1 called after the payload was complete " | ||
| "— this is the call that hangs in production") | ||
|
|
||
| def read(self, *_a): # pragma: no cover - guard, must never be reached | ||
| raise AssertionError("read() would block until EOF") | ||
|
|
||
|
|
||
| class _FakeStdin: | ||
| def __init__(self, chunks, tty=False): | ||
| self.buffer = _FakeBuffer(chunks) | ||
| self._tty = tty | ||
|
|
||
| def isatty(self): | ||
| return self._tty | ||
|
|
||
|
|
||
| def test_stops_at_end_of_json_without_waiting_for_eof(monkeypatch): | ||
| stdin = _FakeStdin([PAYLOAD]) | ||
| monkeypatch.setattr(sys, "stdin", stdin) | ||
|
|
||
| assert render_thin._consume_stdin() == PAYLOAD | ||
| assert stdin.buffer.reads == 1 | ||
|
|
||
|
|
||
| def test_reassembles_a_payload_split_across_chunks(monkeypatch): | ||
| half = len(PAYLOAD) // 2 | ||
| stdin = _FakeStdin([PAYLOAD[:half], PAYLOAD[half:]]) | ||
| monkeypatch.setattr(sys, "stdin", stdin) | ||
|
|
||
| assert render_thin._consume_stdin() == PAYLOAD | ||
| assert stdin.buffer.reads == 2 | ||
|
|
||
|
|
||
| def test_non_json_input_still_reads_to_eof(monkeypatch): | ||
| """No JSON document to bound on — fall back to the old EOF behaviour | ||
| rather than truncating whatever the caller sent.""" | ||
| stdin = _FakeStdin([b"not json", b""]) | ||
| monkeypatch.setattr(sys, "stdin", stdin) | ||
|
|
||
| assert render_thin._consume_stdin() == b"not json" | ||
|
|
||
|
|
||
| def test_interactive_stdin_returns_none(monkeypatch): | ||
| monkeypatch.setattr(sys, "stdin", _FakeStdin([PAYLOAD], tty=True)) | ||
|
|
||
| assert render_thin._consume_stdin() is None | ||
|
|
||
|
|
||
| def test_empty_stdin_returns_none(monkeypatch): | ||
| monkeypatch.setattr(sys, "stdin", _FakeStdin([b""])) | ||
|
|
||
| assert render_thin._consume_stdin() is None | ||
|
|
||
|
|
||
| def test_session_id_survives_the_bounded_read(monkeypatch): | ||
| """The bytes handed back must still route to the right session bucket.""" | ||
| monkeypatch.setattr(sys, "stdin", _FakeStdin([PAYLOAD])) | ||
|
|
||
| payload = render_thin._consume_stdin() | ||
|
|
||
| assert render_thin._extract_session_id(payload) == "abc-123" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("buf,expected", [ | ||
| (bytearray(b'{"a": 1}'), True), | ||
| (bytearray(b'{"a": 1'), False), | ||
| (bytearray(b'{"a": 1}\n'), True), | ||
| (bytearray(b''), False), | ||
| ]) | ||
| def test_payload_completeness_check(buf, expected): | ||
| assert render_thin._payload_is_complete(buf) is expected |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
_payload_is_completedoesn't catchRecursionErrorfrom deeply-nested JSON.json.loadsraisesRecursionError(aRuntimeError, notValueError) on pathologically nested documents. That exception isn't caught here, nor by_consume_stdin'sexcept (OSError, AttributeError):(Line 237), so it propagates out of_consume_stdin()and crashesrender()entirely instead of falling back gracefully like malformed JSON does.🛡️ Proposed fix
def _payload_is_complete(buf: bytearray) -> bool: """True once `buf` holds a whole JSON document.""" try: json.loads(buf.decode("utf-8", errors="replace")) - except ValueError: # JSONDecodeError ⊂ ValueError + except (ValueError, RecursionError): # JSONDecodeError ⊂ ValueError return False return True📝 Committable suggestion
🤖 Prompt for AI Agents