Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 7d9726b

Browse files
authored
Merge pull request #1270 from sdboyer/cmd-ctrl
Break context chain and move subprocesses into separate pgroups
2 parents 7b36282 + 239acda commit 7d9726b

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

internal/gps/cmd_unix.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"os"
1313
"os/exec"
14+
"syscall"
1415
"time"
1516

1617
"github.com/pkg/errors"
@@ -25,11 +26,26 @@ type cmd struct {
2526
}
2627

2728
func commandContext(ctx context.Context, name string, arg ...string) cmd {
28-
// Grab the caller's context and pass a derived one to CommandContext.
29-
c := cmd{ctx: ctx}
30-
ctx, cancel := context.WithCancel(ctx)
31-
c.Cmd = exec.CommandContext(ctx, name, arg...)
32-
c.cancel = cancel
29+
// Create a one-off cancellable context for use by the CommandContext, in
30+
// the event that we have to force a Process.Kill().
31+
ctx2, cancel := context.WithCancel(context.Background())
32+
33+
c := cmd{
34+
Cmd: exec.CommandContext(ctx2, name, arg...),
35+
cancel: cancel,
36+
ctx: ctx,
37+
}
38+
39+
// Force subprocesses into their own process group, rather than being in the
40+
// same process group as the dep process. Because Ctrl-C sent from a
41+
// terminal will send the signal to the entire currently running process
42+
// group, this allows us to directly manage the issuance of signals to
43+
// subprocesses.
44+
c.Cmd.SysProcAttr = &syscall.SysProcAttr{
45+
Setpgid: true,
46+
Pgid: 0,
47+
}
48+
3349
return c
3450
}
3551

@@ -47,6 +63,7 @@ func (c cmd) CombinedOutput() ([]byte, error) {
4763
var b bytes.Buffer
4864
c.Cmd.Stdout = &b
4965
c.Cmd.Stderr = &b
66+
5067
if err := c.Cmd.Start(); err != nil {
5168
return nil, err
5269
}

internal/gps/source_manager.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,29 @@ const (
741741
ctExportTree
742742
)
743743

744+
func (ct callType) String() string {
745+
switch ct {
746+
case ctHTTPMetadata:
747+
return "Retrieving go get metadata"
748+
case ctListVersions:
749+
return "Retrieving latest version list"
750+
case ctGetManifestAndLock:
751+
return "Reading manifest and lock data"
752+
case ctListPackages:
753+
return "Parsing PackageTree"
754+
case ctSourcePing:
755+
return "Checking for upstream existence"
756+
case ctSourceInit:
757+
return "Initializing local source cache"
758+
case ctSourceFetch:
759+
return "Fetching latest data into local source cache"
760+
case ctExportTree:
761+
return "Writing code tree out to disk"
762+
default:
763+
panic("unknown calltype")
764+
}
765+
}
766+
744767
// callInfo provides metadata about an ongoing call.
745768
type callInfo struct {
746769
name string

0 commit comments

Comments
 (0)