From 9d0d090fc1d683accc8bd1b354425c23cbe9fb6a Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 18 May 2021 10:50:43 +0200 Subject: [PATCH] safcm: don't hang on error before a connection is established --- cmd/safcm/main_sync_test.go | 14 ++++++++++++++ cmd/safcm/sync.go | 1 + rpc/conn.go | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) 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") } -- 2.45.2