FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add SSH master connection feature by deansheather · Pull Request #116 · coder/sshcode · GitHub

This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Repository navigation

Prev Previous commit
Next Next commit
add "process is running" check to checkSSHMaster()
Checks if the master process is running by sending signal 0 to it.
According to kill(2), sending a signal of 0 will send no signal but will
still perform error checking.

To prevent the SSH master from becoming a zombie process, a wait call
was added in a goroutine.
  • Loading branch information
deansheather committed Jun 13, 2019
commit 4d64fc0393636307296b2bd5db7577ecb5ea28a4
43 changes: 29 additions & 14 deletions sshcode.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"os/signal"
"path/filepath"
"strconv"
"syscall"
"strings"
"syscall"
"time"

"github.com/pkg/browser"
Expand Down Expand Up @@ -67,9 +67,13 @@ func sshCode(host, dir string, o options) error {
sshMasterCmd.Stdin = os.Stdin
sshMasterCmd.Stdout = os.Stdout
sshMasterCmd.Stderr = os.Stderr
Comment thread
sreya marked this conversation as resolved.
Outdated
stopSSHMaster := func () {
stopSSHMaster := func() {
if sshMasterCmd.Process != nil {
err := sshMasterCmd.Process.Signal(syscall.SIGTERM)
err := sshMasterCmd.Process.Signal(syscall.Signal(0))
Comment thread
deansheather marked this conversation as resolved.
Outdated
if err != nil {
return
}
err = sshMasterCmd.Process.Signal(syscall.SIGTERM)
if err != nil {
flog.Error("failed to send SIGTERM to SSH master process: %v", err)
}
Expand All @@ -78,12 +82,13 @@ func sshCode(host, dir string, o options) error {
defer stopSSHMaster()

err = sshMasterCmd.Start()
go sshMasterCmd.Wait()
Comment thread
deansheather marked this conversation as resolved.
Outdated
if err != nil {
flog.Error("failed to start SSH master connection, disabling connection reuse feature: %v", err)
o.noReuseConnection = true
stopSSHMaster()
Comment thread
deansheather marked this conversation as resolved.
Outdated
} else {
err = checkSSHMaster(newSSHFlags, host)
err = checkSSHMaster(sshMasterCmd, newSSHFlags, host)
if err != nil {
flog.Error("SSH master failed to be ready in time, disabling connection reuse feature: %v", err)
o.noReuseConnection = true
Expand Down Expand Up @@ -307,22 +312,32 @@ func randomPort() (string, error) {

// checkSSHMaster polls every second for 30 seconds to check if the SSH master
// is ready.
func checkSSHMaster(sshFlags string, host string) (err error) {
maxTries := 30
check := func() error {
func checkSSHMaster(sshMasterCmd *exec.Cmd, sshFlags string, host string) error {
var (
maxTries = 30
sleepDur = time.Second
err error
)
for i := 0; i < maxTries; i++ {
// Check if the master is running
if sshMasterCmd.Process == nil {
return xerrors.Errorf("SSH master process not running")
}
err = sshMasterCmd.Process.Signal(syscall.Signal(0))
Comment thread
deansheather marked this conversation as resolved.
Outdated
if err != nil {
return xerrors.Errorf("failed to check if SSH master process was alive: %v", err)
}

// Check if it's ready
sshCmdStr := fmt.Sprintf(`ssh %v -O check %v`, sshFlags, host)
sshCmd := exec.Command("sh", "-c", sshCmdStr)
return sshCmd.Run()
}

for i := 0; i < maxTries; i++ {
err = check()
err = sshCmd.Run()
if err == nil {
return nil
}
time.Sleep(time.Second)
time.Sleep(sleepDur)
}
return err
return xerrors.Errorf("max number of tries exceeded: %d", maxTries)
}

func syncUserSettings(sshFlags string, host string, back bool) error {
Expand Down

Back | FazBrowse Home | New Git URL