From: Simon Ruderich Date: Tue, 18 May 2021 08:50:43 +0000 (+0200) Subject: safcm: don't hang on error before a connection is established X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=9d0d090fc1d683accc8bd1b354425c23cbe9fb6a;p=safcm%2Fsafcm.git safcm: don't hang on error before a connection is established --- diff --git a/cmd/safcm/main_sync_test.go b/cmd/safcm/main_sync_test.go index 16aad16..fdbe98f 100644 --- a/cmd/safcm/main_sync_test.go +++ b/cmd/safcm/main_sync_test.go @@ -348,6 +348,20 @@ executed 2 command(s): logRegexp := regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `) detectedRegexp := regexp.MustCompile(`detected_\S+`) + t.Run("error before connection is established", func(t *testing.T) { + // Fake $PATH so safcm cannot find the `ssh` binary. + path := os.Getenv("PATH") + os.Setenv("PATH", "") + defer os.Setenv("PATH", path) + + cmd := exec.Command("../../../../../safcm", + "sync", "-n", "no-settings.example.org") + _, err := cmd.CombinedOutput() + if err == nil { + t.Errorf("err = nil") + } + }) + for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { if tc.remove { diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 6fe65fa..1a482b3 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -380,6 +380,7 @@ func (s *Sync) Host(wg *sync.WaitGroup) error { SshConfig: s.config.SshConfig, }) if err != nil { + conn.Kill() return err } defer conn.Kill() diff --git a/rpc/conn.go b/rpc/conn.go index 9786941..03b7485 100644 --- a/rpc/conn.go +++ b/rpc/conn.go @@ -124,9 +124,14 @@ func (c *Conn) wait() error { } // Kill forcefully terminates the connection. It's safe to call Kill (and -// Wait) multiple times. +// Wait) multiple times. Calling it before Dial*() was called will only close +// the Events channel. func (c *Conn) Kill() error { if c.cmd == nil { + if c.events != nil { + close(c.events) + c.events = nil + } return fmt.Errorf("Dial*() not called or already terminated") }