Repository navigation
fix(supervisor): reap only leased process descendants #29
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,136 @@ | ||
| package supervisor | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| const ( | ||
| detachedFixtureRole = "DEVSY_DETACHED_FIXTURE_ROLE" | ||
| detachedFixtureSocket = "DEVSY_DETACHED_FIXTURE_SOCKET" | ||
| ) | ||
|
|
||
| func TestRunnerDoesNotWaitForDetachedBackend(t *testing.T) { | ||
| options := fixtureOptions(t) | ||
| options.Args = []string{"-test.run=^TestDetachedBackendFixture$"} | ||
| socket := filepath.Join(shortDirectory(t), "backend.sock") | ||
| options.Env = append( | ||
| os.Environ(), | ||
| detachedFixtureRole+"=runtime", | ||
| detachedFixtureSocket+"="+socket, | ||
| ) | ||
| runtime, err := Runner(options)(nil, &exec.Cmd{}, shortDirectory(t)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = runtime.Stdout().Close(); _ = runtime.Stderr().Close() }) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
| defer cancel() | ||
| if err := runtime.Start(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = runtime.Kill(context.Background()) }) | ||
| stopRead := context.AfterFunc(ctx, func() { _ = runtime.Stdout().Close() }) | ||
| defer stopRead() | ||
| var pid int | ||
| if err := json.NewDecoder(runtime.Stdout()).Decode(&pid); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| // The backend has its own session and lifetime. Release it before waiting | ||
| // for supervisor cleanup even when the regression makes Wait time out. | ||
| t.Cleanup(func() { _ = unix.Kill(pid, unix.SIGKILL) }) | ||
| if err := runtime.Wait(ctx); err != nil { | ||
| t.Fatalf("supervisor waited for detached backend: %v", err) | ||
| } | ||
| assertDetachedBackendResponds(ctx, t, socket) | ||
| } | ||
|
|
||
| func assertDetachedBackendResponds(ctx context.Context, t *testing.T, socket string) { | ||
| t.Helper() | ||
| connection, err := (&net.Dialer{}).DialContext(ctx, "unix", socket) | ||
| if err != nil { | ||
| t.Fatalf("detached backend unavailable after session cleanup: %v", err) | ||
| } | ||
| defer func() { _ = connection.Close() }() | ||
| if err := connection.SetDeadline(time.Now().Add(5 * time.Second)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| data, err := io.ReadAll(connection) | ||
| if err != nil || string(data) != "alive" { | ||
| t.Fatalf("detached backend did not respond: %q (%v)", data, err) | ||
| } | ||
| } | ||
|
|
||
| func TestDetachedBackendFixture(t *testing.T) { | ||
| switch os.Getenv(detachedFixtureRole) { | ||
| case "backend": | ||
| serveDetachedBackend(t) | ||
| case "runtime": | ||
| runDetachedBackend(t) | ||
| } | ||
| } | ||
|
|
||
| func serveDetachedBackend(t *testing.T) { | ||
| t.Helper() | ||
| listener, err := net.Listen("unix", os.Getenv(detachedFixtureSocket)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := json.NewEncoder(os.Stdout).Encode(os.Getpid()); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| connection, err := listener.Accept() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := io.WriteString(connection, "alive"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _ = connection.Close() | ||
| _ = listener.Close() | ||
| time.Sleep(time.Minute) | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| func runDetachedBackend(t *testing.T) { | ||
| t.Helper() | ||
| binary, err := os.Executable() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| // #nosec G204 -- Relaunches this test executable in a separate backend session. | ||
| child := exec.Command(binary, "-test.run=^TestDetachedBackendFixture$") | ||
| child.Env = append(os.Environ(), detachedFixtureRole+"=backend") | ||
| child.SysProcAttr = &syscall.SysProcAttr{Setsid: true} | ||
| stdout, err := child.StdoutPipe() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := child.Start(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var pid int | ||
| if err := json.NewDecoder(stdout).Decode(&pid); err != nil { | ||
| _ = child.Process.Kill() | ||
| _ = child.Wait() | ||
| t.Fatal(err) | ||
| } | ||
| if err := stdout.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := fmt.Fprintln(os.Stdout, pid); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| os.Exit(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
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.
Uh oh!
There was an error while loading. Please reload this page.