fix: bound stdin read so the process cannot outlive its render - #548
Open
filipemb wants to merge 1 commit into
Open
fix: bound stdin read so the process cannot outlive its render#548filipemb wants to merge 1 commit into
filipemb wants to merge 1 commit into
Conversation
readStdin() iterated process.stdin until EOF. A host that writes the status JSON but keeps the write end open leaves that iteration suspended forever: the line renders, but the process stays resident. With a short refresh interval these accumulate. Race the read against a timeout (5s default, CCSTATUSLINE_STDIN_TIMEOUT_MS to override) and resolve with whatever already arrived — the payload is written in one shot, so a missing EOF does not mean missing data and the status line still renders. Clear the timer and pause/unref the stream on the way out. readStdin moves to src/utils/stdin.ts because the entrypoint runs main() on import, which makes the function untestable in place.
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.
Problem
readStdin()iteratesprocess.stdinuntil EOF. When the host writes the status JSON but keeps the write end open, that iteration never settles: the render finishes, but the process stays resident. On a long-lived host with a short refresh interval these accumulate — I found 77 of them alive, the oldest at 257 hours, holding ~2.3 GB.Repro — spawn the CLI, write the payload, never call
end():Before: still alive, indefinitely. After: exits in ~5s, having rendered the line.
Fix
Race the read against a timeout — 5000 ms default,
CCSTATUSLINE_STDIN_TIMEOUT_MSto override. On timeout it resolves with whatever already arrived instead of discarding it: the payload is written in one shot, so a missing EOF means the EOF is missing, not the data, and the status line still renders correctly.finallyclears the timer and pauses/unrefs the stream.readStdinmoves tosrc/utils/stdin.tsbecause the entrypoint runsmain()on import, which makes the function untestable where it was.Tests
src/utils/__tests__/stdin.test.tscovers three cases: TTY returns null, a normal close reads the payload, and no-EOF resolves with what arrived. The third one fails onmain(times out) and passes with this change. The rest of the suite shows no new failures.