Skip to content

Commit 3d66efa

Browse files
authored
fix(machine): signal downcast causes gvproxy and vfkit/krunkit exit prematurely (#74)
这个 PR 修复了主进程收到 ctrl-C 信号向下广播信号导致 gvproxy 和 vfkit/krunkit 过早退出导致 DiskSync 失败的问题。 setpgid 将被调用进程的进程组 ID 设置为它自己的进程 ID,从而创建了一个新的进程组,被调用的进程成为了组长,当 signal 发生时,signal 则信号不会被传递到被调用进程,与此同时,ctx 仍能控制被调用进程何时结束(SIGKILL)。 https://pubs.opengroup.org/onlinepubs/009604599/functions/setpgid.html
1 parent 1ef0b8b commit 3d66efa

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/machine/shim/network_darwin.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ package shim
88
import (
99
"context"
1010
"fmt"
11+
"io"
1112
"os"
1213
"os/exec"
1314
"reflect"
1415
"time"
1516
"unsafe"
1617

18+
"bauklotze/pkg/pty"
19+
1720
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
1821
"github.com/containers/storage/pkg/fileutils"
1922
"github.com/sirupsen/logrus"
@@ -84,14 +87,17 @@ func startForwarder(ctx context.Context, mc *vmconfig.MachineConfig, socksOnHost
8487
mc.GvProxy.Sockets = bArray
8588

8689
gvpExecCmd := exec.CommandContext(ctx, gvpBin.GetPath(), gvproxyCommand.ToCmdline()...)
87-
gvpExecCmd.Stdout = os.Stdout
88-
gvpExecCmd.Stderr = os.Stderr
8990

9091
logrus.Infof("GVPROXY FULL CMDLINE: %q", gvpExecCmd.Args)
9192
events.NotifyRun(events.StartGvProxy, "staring...")
92-
if err := gvpExecCmd.Start(); err != nil {
93+
94+
ptmx, err := pty.RunInPty(gvpExecCmd)
95+
if err != nil {
9396
return fmt.Errorf("unable to execute: %q: %w", gvpExecCmd.Args, err)
9497
}
98+
go func() {
99+
_, _ = io.Copy(os.Stdout, ptmx)
100+
}()
95101

96102
events.NotifyRun(events.StartGvProxy, "finished")
97103

pkg/pty/pty.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ func RunInPty(c *exec.Cmd) (*os.File, error) {
2222
Rows: rows,
2323
X: rows,
2424
Y: cols,
25-
}, &syscall.SysProcAttr{})
25+
}, &syscall.SysProcAttr{
26+
Setpgid: true,
27+
})
2628
}

0 commit comments

Comments
 (0)