Skip to content

Commit 7ca508c

Browse files
authored
Merge pull request #75 from dispatchrun/signal-handling
run: fix signal handling
2 parents 8434291 + eb2f58f commit 7ca508c

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

cli/run.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os/exec"
1717
"os/signal"
1818
"path/filepath"
19+
"runtime"
1920
"slices"
2021
"strconv"
2122
"strings"
@@ -197,12 +198,16 @@ Run 'dispatch help run' to learn about Dispatch sessions.`, BridgeSession)
197198
select {
198199
case <-ctx.Done():
199200
return
200-
case <-signals:
201+
case s := <-signals:
201202
if !signaled {
202203
signaled = true
203-
_ = cmd.Process.Signal(syscall.SIGTERM)
204204
} else {
205-
_ = cmd.Process.Kill()
205+
s = os.Kill
206+
}
207+
if cmd.Process != nil && cmd.Process.Pid > 0 {
208+
// Sending the signal to -pid sends it to all processes
209+
// in the process group.
210+
_ = syscall.Kill(-cmd.Process.Pid, s.(syscall.Signal))
206211
}
207212
}
208213
}
@@ -282,6 +287,9 @@ Run 'dispatch help run' to learn about Dispatch sessions.`, BridgeSession)
282287
}
283288
})
284289

290+
runtime.LockOSThread()
291+
defer runtime.UnlockOSThread()
292+
285293
if err = cmd.Start(); err != nil {
286294
return fmt.Errorf("failed to start %s: %v", strings.Join(args, " "), err)
287295
}

cli/run_darwin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cli
2+
3+
import "syscall"
4+
5+
func setSysProcAttr(attr *syscall.SysProcAttr) {
6+
attr.Setpgid = true
7+
}

cli/run_default.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !linux
1+
//go:build !linux && !darwin
22

33
package cli
44

0 commit comments

Comments
 (0)