From 31416dbf13a74d17e83afae6594c0a931243cb64 Mon Sep 17 00:00:00 2001 From: Jan Baraniewski Date: Tue, 26 Nov 2024 08:33:42 +0100 Subject: [PATCH] Add ssh keep alive to ssh tunnel to workspace --- cmd/ssh.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cmd/ssh.go b/cmd/ssh.go index f95da43f2..7a8ecd46d 100644 --- a/cmd/ssh.go +++ b/cmd/ssh.go @@ -434,6 +434,8 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config, // Traffic is coming in from the outside, we need to forward it to the container if cmd.Proxy || cmd.Stdio { if cmd.Proxy { + go startSSHKeepAlive(ctx, containerClient, log) + go func() { if err := cmd.startRunnerServices(ctx, devPodConfig, containerClient, log); err != nil { log.Error(err) @@ -451,6 +453,7 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config, !cmd.Proxy && cmd.AgentForwarding && devPodConfig.ContextOption(config.ContextOptionSSHAgentForwarding) == "true", func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error { + go startSSHKeepAlive(ctx, containerClient, log) return devssh.Run(ctx, containerClient, command, stdin, stdout, stderr, envVars) }, writer, @@ -659,3 +662,21 @@ func preparePipes() (io.Reader, io.WriteCloser, io.Reader, io.WriteCloser, error return stdoutReader, stdoutWriter, stdinReader, stdinWriter, nil } + +func startSSHKeepAlive(ctx context.Context, client *ssh.Client, log log.Logger) { + ticker := time.NewTicker(55 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + log.Infof("SSH keepalive routine stopped: %v", ctx.Err()) + return + case <-ticker.C: + _, _, err := client.SendRequest("keepalive@openssh.com", true, nil) + if err != nil { + log.Errorf("Failed to send keepalive: %w", err) + } + } + } +}