-
Notifications
You must be signed in to change notification settings - Fork 7
Stage interface redesign: pipe type negotiation, pooled buffer copies #51
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
Open
znull
wants to merge
21
commits into
main
Choose a base branch
from
znull/stage-v2-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c4146f4
Shush the linter
mhagger de3225f
pipeline_test.go: remove unnecessary tmpdirs and simplify test setup
mhagger 9fd5b2c
Add some benchmarks of moving a bunch of data through a pipeline
mhagger ca78f76
Simplify the `NopCloser`s
mhagger 2f59602
Stage: change the interface to make stdin/stdout handling more flexible
mhagger cc9cd67
Add some tests that `Pipeline.Start()` picks the right stdin/stdout
mhagger 6c6dfe5
Port MemoryLimitWithObserver to new Stage interface
znull cd47557
Restore panic handler for Function stages
znull 48dffbd
Fix memoryWatchStage.Wait() to always call stopWatching()
znull 92ad57c
Fix lint errors
znull 6a0abf3
Restore identity-copy behavior for empty pipelines
znull 9606368
Add tests pinning command stdout fd-pass fast path
znull 3a1f8d8
Pool buffers for the non-*os.File command stdout copy path
znull 46f19e4
Forward panic handler through wrapper stages
znull 121ae94
Avoid leaking pooled-stdout goroutine when cmd.Start() fails
znull bf5820b
Bump module path to v2 for breaking Stage interface change
znull 6102173
remove IOPreferenceNil
znull 754784b
properly handle nil input/output
znull 9dca23e
allow earlier GC of lateClosers
znull 2129780
rewrite cleanup/unwind in forward order
znull 02f5ed9
Thread panic handler through Start; drop StagePanicHandlerAware
znull 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| # go-pipe [](https://pkg.go.dev/github.com/github/go-pipe) | ||
| # go-pipe [](https://pkg.go.dev/github.com/github/go-pipe/v2) | ||
| A package used to easily build command pipelines in your Go applications | ||
|
|
||
| # Important | ||
| We have not thoroughly tested this package on OSs other than Linux, especially Windows. At this time, using this package on Windows based systems is considered experimental and will be supported only on a best effort basis. | ||
|
|
||
| # Links | ||
|
|
||
| * [Docs](https://pkg.go.dev/github.com/github/go-pipe) | ||
| * [Docs](https://pkg.go.dev/github.com/github/go-pipe/v2) |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| module github.com/github/go-pipe | ||
| module github.com/github/go-pipe/v2 | ||
|
|
||
| go 1.24.0 | ||
|
|
||
|
|
||
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
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
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,63 @@ | ||
| package pipe_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os/exec" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/github/go-pipe/v2/pipe" | ||
| ) | ||
|
|
||
| // TestCommandStageStartFailureNoRace verifies that when `cmd.Start()` | ||
| // fails (e.g. command not found), the goroutine that | ||
| // `setupPooledStdout` spawned does not leak past `Pipeline.Run()`. | ||
| // `bytes.Buffer.ReadFrom` writes to the buffer's slice header via | ||
| // `grow()` before its first `Read()`, so a leaked goroutine races | ||
| // with the caller's access to the destination buffer once Run | ||
| // returns the error. Run a tight loop so `-race` is likely to catch | ||
| // any regression. | ||
| func TestCommandStageStartFailureNoRace(t *testing.T) { | ||
| for i := 0; i < 50; i++ { | ||
| var buf bytes.Buffer | ||
| p := pipe.New(pipe.WithStdout(&buf)) | ||
| p.Add(pipe.CommandStage("nope", exec.Command("this-binary-does-not-exist-xyz123"))) | ||
| if err := p.Run(context.Background()); err == nil { | ||
| t.Fatalf("expected error from non-existent command, got nil") | ||
| } | ||
| _ = buf.String() | ||
| } | ||
| } | ||
|
|
||
| // trackingWriteCloser is a non-`*os.File` `io.WriteCloser` that records | ||
| // whether it has been closed. Because it isn't an `*os.File`, a command | ||
| // stage routes it through `setupPooledStdout` and closes it as a "late | ||
| // closer" (i.e. only after the command finishes / cleanup runs). | ||
| type trackingWriteCloser struct { | ||
| closed atomic.Bool | ||
| } | ||
|
|
||
| func (w *trackingWriteCloser) Write(p []byte) (int, error) { return len(p), nil } | ||
|
|
||
| func (w *trackingWriteCloser) Close() error { | ||
| w.closed.Store(true) | ||
| return nil | ||
| } | ||
|
|
||
| // TestCommandStageStartFailureClosesLateClosers verifies that a | ||
| // `WithStdoutCloser` on the last stage is closed even when `cmd.Start()` | ||
| // fails. The closer is registered as a "late closer," which is normally | ||
| // drained by `Wait()`; since `Wait()` never runs when `Start()` fails, | ||
| // the start-failure cleanup path must close it instead. | ||
| func TestCommandStageStartFailureClosesLateClosers(t *testing.T) { | ||
| w := &trackingWriteCloser{} | ||
| p := pipe.New(pipe.WithStdoutCloser(w)) | ||
| p.Add(pipe.CommandStage("nope", exec.Command("this-binary-does-not-exist-xyz123"))) | ||
| if err := p.Run(context.Background()); err == nil { | ||
| t.Fatalf("expected error from non-existent command, got nil") | ||
| } | ||
| if !w.closed.Load() { | ||
| t.Fatalf("expected late closer to be closed after Start() failure") | ||
| } | ||
| } |
Oops, something went wrong.
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.
What would you think about setting
s.lateClosers = nilhere, to allow those objects to be freed in case the stage object remains reachable for longer?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.
Seems reasonable and low risk, I'll add it