@@ -11,10 +11,8 @@ import (
1111 "net"
1212 "os"
1313 "os/exec"
14- "runtime"
1514 "strings"
1615 "sync"
17- "syscall"
1816 "testing"
1917 "time"
2018
@@ -117,15 +115,6 @@ func (b *BuildxEnv) dialStdio(ctx context.Context) error {
117115 // the buildx dial-stdio process from cleaning up its resources properly.
118116 cmd := exec .Command ("docker" , args ... )
119117 cmd .Env = os .Environ ()
120- cmd .SysProcAttr = & syscall.SysProcAttr {
121- // Put the child in its own process group so we can kill the entire
122- // group (docker + docker-buildx plugin) during cleanup.
123- Setpgid : true ,
124- // Send SIGTERM to the child process when the parent (test process) dies.
125- // This prevents dial-stdio processes from being orphaned when the test
126- // suite is interrupted or crashes.
127- Pdeathsig : syscall .SIGTERM ,
128- }
129118
130119 c1 , c2 := net .Pipe ()
131120 cmd .Stdin = c1
@@ -139,29 +128,16 @@ func (b *BuildxEnv) dialStdio(ctx context.Context) error {
139128 ww := io .MultiWriter (w , errBuf )
140129 cmd .Stderr = ww
141130
142- // processDone is closed when cmd.Wait() returns, signaling the cleanup
143- // function that the process has exited.
144- processDone := make (chan struct {})
145- go func () {
146- // Lock this goroutine to its OS thread for the lifetime of the child process.
147- // Pdeathsig delivers the signal when the *thread* that forked the child exits,
148- // not when the process exits. Without locking, the Go runtime may destroy the
149- // thread that called cmd.Start(), prematurely delivering SIGTERM to the child.
150- runtime .LockOSThread ()
151- defer runtime .UnlockOSThread ()
152-
153- if err := cmd .Start (); err != nil {
154- // Propagate the start error through the stderr pipe so the
155- // scanner below will surface it via scanner.Err().
156- w .CloseWithError (err )
157- return
158- }
131+ if err := cmd .Start (); err != nil {
132+ return nil , err
133+ }
159134
135+ chWait := make (chan struct {})
136+ go func () {
160137 err := cmd .Wait ()
161- close (processDone )
162138 c1 .Close ()
163139 // pkgerrors.Wrap will return nil if err is nil, otherwise it will give
164- // us a wrapped error with the buffered stderr from the command.
140+ // us a wrapped error with the buffered stderr from he command.
165141 w .CloseWithError (pkgerrors .Wrapf (err , "%s" , errBuf ))
166142 }()
167143
@@ -195,7 +171,7 @@ func (b *BuildxEnv) dialStdio(ctx context.Context) error {
195171 }
196172
197173 select {
198- case <- processDone :
174+ case <- chWait :
199175 case <- time .After (10 * time .Second ):
200176 // If it still doesn't exit, force kill
201177 cmd .Process .Kill () //nolint:errcheck // Force kill if it doesn't exit after interrupt
0 commit comments