-
-
Notifications
You must be signed in to change notification settings - Fork 77
fix: prevent indefinite hang on Windows when child processes hold pipes open #273
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,24 +12,44 @@ import ( | |
| // uses bufio.Reader internally, so there is no line length limit. | ||
| // strips trailing \n and \r\n from lines before passing to handler (matching bufio.ScanLines behavior). | ||
| // returns nil on EOF, or a wrapped error on context cancellation or read failure. | ||
| // | ||
| // each blocking ReadString call runs in a goroutine so that context cancellation | ||
| // is detected even while the pipe read is in progress (important on Windows where | ||
| // child processes may hold the pipe open after the parent exits, causing ReadString | ||
| // to block indefinitely until the process is explicitly killed). | ||
| func readLines(ctx context.Context, r io.Reader, handler func(string)) error { | ||
| type readResult struct { | ||
| line string | ||
| err error | ||
| } | ||
|
|
||
| reader := bufio.NewReader(r) | ||
| ch := make(chan readResult, 1) // buffered: lets abandoned goroutine exit after kill | ||
|
|
||
| doRead := func() { | ||
| line, err := reader.ReadString('\n') | ||
| ch <- readResult{line, err} | ||
| } | ||
|
|
||
| go doRead() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| // goroutine is still blocked on ReadString; it will unblock when the process | ||
| // is killed (killProcess fires on context cancel) and drain into the buffered ch. | ||
| return fmt.Errorf("read lines: %w", ctx.Err()) | ||
| default: | ||
| } | ||
| line, err := reader.ReadString('\n') | ||
| if line != "" { | ||
| line = trimLineEnding(line) | ||
| handler(line) | ||
| } | ||
| if err != nil { | ||
| if errors.Is(err, io.EOF) { | ||
| return nil | ||
| case res := <-ch: | ||
| if res.line != "" { | ||
| handler(trimLineEnding(res.line)) | ||
| } | ||
| if res.err != nil { | ||
| if errors.Is(res.err, io.EOF) { | ||
| return nil | ||
| } | ||
| return fmt.Errorf("read lines: %w", res.err) | ||
| } | ||
| return fmt.Errorf("read lines: %w", err) | ||
| go doRead() | ||
| } | ||
|
Comment on lines
+49
to
53
|
||
| } | ||
| } | ||
|
|
||
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.
readLines() now always starts a ReadString goroutine before checking ctx. If ctx is already canceled, there’s a race where the read goroutine can fill
chand the select may take theres := <-chbranch, potentially draining input and returning nil/EOF instead ofcontext.Canceled(can make cancellation propagation and tests flaky). Consider checkingctx.Err()up front (before starting the first goroutine) and/or prioritizing cancellation inside the loop (e.g., ifctx.Err()!=nilwhen receivingres, return the context error without processing/spawning another read).