@@ -11,8 +11,8 @@ import (
11
11
"os/signal"
12
12
"path/filepath"
13
13
"strconv"
14
- "syscall"
15
14
"strings"
15
+ "syscall"
16
16
"time"
17
17
18
18
"github.com/pkg/browser"
@@ -67,9 +67,13 @@ func sshCode(host, dir string, o options) error {
67
67
sshMasterCmd .Stdin = os .Stdin
68
68
sshMasterCmd .Stdout = os .Stdout
69
69
sshMasterCmd .Stderr = os .Stderr
70
- stopSSHMaster := func () {
70
+ stopSSHMaster := func () {
71
71
if sshMasterCmd .Process != nil {
72
- err := sshMasterCmd .Process .Signal (syscall .SIGTERM )
72
+ err := sshMasterCmd .Process .Signal (syscall .Signal (0 ))
73
+ if err != nil {
74
+ return
75
+ }
76
+ err = sshMasterCmd .Process .Signal (syscall .SIGTERM )
73
77
if err != nil {
74
78
flog .Error ("failed to send SIGTERM to SSH master process: %v" , err )
75
79
}
@@ -78,12 +82,13 @@ func sshCode(host, dir string, o options) error {
78
82
defer stopSSHMaster ()
79
83
80
84
err = sshMasterCmd .Start ()
85
+ go sshMasterCmd .Wait ()
81
86
if err != nil {
82
87
flog .Error ("failed to start SSH master connection, disabling connection reuse feature: %v" , err )
83
88
o .noReuseConnection = true
84
89
stopSSHMaster ()
85
90
} else {
86
- err = checkSSHMaster (newSSHFlags , host )
91
+ err = checkSSHMaster (sshMasterCmd , newSSHFlags , host )
87
92
if err != nil {
88
93
flog .Error ("SSH master failed to be ready in time, disabling connection reuse feature: %v" , err )
89
94
o .noReuseConnection = true
@@ -307,22 +312,32 @@ func randomPort() (string, error) {
307
312
308
313
// checkSSHMaster polls every second for 30 seconds to check if the SSH master
309
314
// is ready.
310
- func checkSSHMaster (sshFlags string , host string ) (err error ) {
311
- maxTries := 30
312
- check := func () error {
315
+ func checkSSHMaster (sshMasterCmd * exec.Cmd , sshFlags string , host string ) error {
316
+ var (
317
+ maxTries = 30
318
+ sleepDur = time .Second
319
+ err error
320
+ )
321
+ for i := 0 ; i < maxTries ; i ++ {
322
+ // Check if the master is running
323
+ if sshMasterCmd .Process == nil {
324
+ return xerrors .Errorf ("SSH master process not running" )
325
+ }
326
+ err = sshMasterCmd .Process .Signal (syscall .Signal (0 ))
327
+ if err != nil {
328
+ return xerrors .Errorf ("failed to check if SSH master process was alive: %v" , err )
329
+ }
330
+
331
+ // Check if it's ready
313
332
sshCmdStr := fmt .Sprintf (`ssh %v -O check %v` , sshFlags , host )
314
333
sshCmd := exec .Command ("sh" , "-c" , sshCmdStr )
315
- return sshCmd .Run ()
316
- }
317
-
318
- for i := 0 ; i < maxTries ; i ++ {
319
- err = check ()
334
+ err = sshCmd .Run ()
320
335
if err == nil {
321
336
return nil
322
337
}
323
- time .Sleep (time . Second )
338
+ time .Sleep (sleepDur )
324
339
}
325
- return err
340
+ return xerrors . Errorf ( "max number of tries exceeded: %d" , maxTries )
326
341
}
327
342
328
343
func syncUserSettings (sshFlags string , host string , back bool ) error {
0 commit comments