| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestUnixTerminalCloseReleasesPTYHeldByDescendant(t *testing.T) { |
| 14 | var manager terminalManager |
| 15 | t.Cleanup(manager.close) |
| 16 | |
| 17 | |
| 18 | |
| 19 | cmd := exec.Command("/bin/sh", "-c", `trap '' HUP; sleep 60 & printf 'descendant:%s\n' "$!"; exit 0`) |
| 20 | session, err := manager.start(cmd, "", "shell") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | sub := session.subscribe() |
| 25 | defer sub.Close() |
| 26 | text := readTerminalUntil(t, sub, "descendant:") |
| 27 | |
| 28 | |
| 29 | for !strings.Contains(text[strings.Index(text, "descendant:"):], "\n") { |
| 30 | text += readTerminalUntil(t, sub, "\n") |
| 31 | } |
| 32 | line := strings.Split(strings.SplitN(text, "descendant:", 2)[1], "\n")[0] |
| 33 | pid, err := strconv.Atoi(strings.TrimSpace(line)) |
| 34 | if err != nil || pid <= 0 { |
| 35 | t.Fatalf("invalid descendant PID: %q", text) |
| 36 | } |
| 37 | child, err := os.FindProcess(pid) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | t.Cleanup(func() { _ = child.Kill(); _ = child.Release() }) |
| 42 | if err := session.file.Wait(); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | closed := make(chan struct{}) |
| 46 | go func() { manager.close(); close(closed) }() |
| 47 | awaitTerminalSignal(t, closed) |
| 48 | awaitTerminalSignal(t, session.done) |
| 49 | if manager.hasLive() { |
| 50 | t.Fatal("explicit close left the terminal active") |
| 51 | } |
| 52 | } |