]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
safcm: don't hang on error before a connection is established
authorSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 08:50:43 +0000 (10:50 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 09:08:22 +0000 (11:08 +0200)
cmd/safcm/main_sync_test.go
cmd/safcm/sync.go
rpc/conn.go

index 16aad166cb5c854842453f89659901dfd959f3d7..fdbe98fba8764565551f7d4e8845789957c12db4 100644 (file)
@@ -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 {
index 6fe65fae326c2b09fa74b51202a964afb44a11ce..1a482b3dd958c274f5fad95a03114596df959d14 100644 (file)
@@ -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()
index 9786941c7e808966a9a76841bfd6e03981f81ba6..03b748545f12c1c588acb24bac2761e0b439b25c 100644 (file)
@@ -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")
        }