Skip to content

Commit 4982c3b

Browse files
committed
gopls/internal/debug: KillTraceViewers: kill process group on UNIX
The "go tool trace" child process begets a "cmd/trace" grandchild, and os.Process.Kill only gets the child. Use kill(-pid) on UNIX to terminate the whole process group. This isn't very portable, but this is only needed for a test that only runs on UNIX. Fixes golang/go#74668 Change-Id: Ic500ef45f7f7a78cd681ff995cefba6b1925cb8a Reviewed-on: https://go-review.googlesource.com/c/tools/+/689476 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 4a9ef58 commit 4982c3b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

gopls/internal/debug/flight.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ import (
1717
"runtime/trace"
1818
"strings"
1919
"sync"
20+
"syscall"
2021
"time"
2122
)
2223

2324
var (
2425
traceviewersMu sync.Mutex
2526
traceviewers []*os.Process
27+
28+
kill = (*os.Process).Kill // windows, plan9; UNIX impl kills whole process group
29+
sysProcAttr syscall.SysProcAttr // UNIX configuration to create process group
2630
)
2731

2832
// KillTraceViewers kills all "go tool trace" processes started by
2933
// /flightrecorder requests, for use in tests (see #74668).
3034
func KillTraceViewers() {
3135
traceviewersMu.Lock()
3236
for _, p := range traceviewers {
33-
p.Kill() // ignore error
37+
kill(p) // ignore error
3438
}
3539
traceviewers = nil
3640
traceviewersMu.Unlock()
@@ -87,6 +91,7 @@ func startFlightRecorder() (http.HandlerFunc, error) {
8791
// web server process. It will run until gopls terminates.
8892
// (It would be nicer if we could just link it in; see #66843.)
8993
cmd := exec.Command("go", "tool", "trace", tracefile)
94+
cmd.SysProcAttr = &sysProcAttr
9095

9196
// Don't connect trace's std{out,err} to our os.Stderr directly,
9297
// otherwise the child may outlive the parent in tests,

gopls/internal/debug/flight_unix.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build go1.25 && unix
6+
7+
package debug
8+
9+
import (
10+
"os"
11+
"syscall"
12+
)
13+
14+
func init() {
15+
// UNIX: kill the whole process group, since
16+
// "go tool trace" starts a cmd/trace child.
17+
kill = killGroup
18+
sysProcAttr.Setpgid = true
19+
}
20+
21+
func killGroup(p *os.Process) error {
22+
return syscall.Kill(-p.Pid, syscall.SIGKILL)
23+
}

0 commit comments

Comments
 (0)