Skip to content

Commit 8ffe0b4

Browse files
committed
Don't delete local socket when reverse forward
When we are forwarding a local socket, we should not delete it. Doing so would interrupt the local service, if delete succeeds. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 31d6828 commit 8ffe0b4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pkg/hostagent/hostagent.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
597597
logrus.Infof("Forwarding %q (host) to %q (guest)", local, remote)
598598
} else {
599599
logrus.Infof("Forwarding %q (guest) to %q (host)", remote, local)
600-
}
601-
if err := os.RemoveAll(local); err != nil {
602-
logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local)
600+
if err := os.RemoveAll(local); err != nil {
601+
logrus.WithError(err).Warnf("Failed to clean up %q (host) before setting up forwarding", local)
602+
}
603603
}
604604
if err := os.MkdirAll(filepath.Dir(local), 0750); err != nil {
605605
return fmt.Errorf("can't create directory for local socket %q: %w", local, err)
@@ -609,22 +609,24 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
609609
logrus.Infof("Stopping forwarding %q (host) to %q (guest)", local, remote)
610610
} else {
611611
logrus.Infof("Stopping forwarding %q (guest) to %q (host)", remote, local)
612+
defer func() {
613+
if err := os.RemoveAll(local); err != nil {
614+
logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local)
615+
}
616+
}()
612617
}
613-
defer func() {
614-
if err := os.RemoveAll(local); err != nil {
615-
logrus.WithError(err).Warnf("Failed to clean up %q (host) after stopping forwarding", local)
616-
}
617-
}()
618618
default:
619619
panic(fmt.Errorf("invalid verb %q", verb))
620620
}
621621
}
622622
cmd := exec.CommandContext(ctx, sshConfig.Binary(), args...)
623623
if out, err := cmd.Output(); err != nil {
624624
if verb == verbForward && strings.HasPrefix(local, "/") {
625-
logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local)
626-
if removeErr := os.RemoveAll(local); err != nil {
627-
logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)
625+
if !reverse {
626+
logrus.WithError(err).Warnf("Failed to set up forward from %q (guest) to %q (host)", remote, local)
627+
if removeErr := os.RemoveAll(local); err != nil {
628+
logrus.WithError(removeErr).Warnf("Failed to clean up %q (host) after forwarding failed", local)
629+
}
628630
}
629631
}
630632
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)

0 commit comments

Comments
 (0)