Skip to content

Commit

Permalink
Revert "fix(pro): wait for outer tunnels to exit and clean up properly"
Browse files Browse the repository at this point in the history
This reverts commit 4856dbd.
  • Loading branch information
pascalbreuninger committed Nov 25, 2024
1 parent 112e81a commit 8f2b433
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 41 deletions.
3 changes: 2 additions & 1 deletion cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type SSHCmd struct {
SetEnvVars []string

Stdio bool
JumpContainer bool
AgentForwarding bool
GPGAgentForwarding bool
GitSSHSignatureForwarding bool
Expand Down Expand Up @@ -435,7 +436,7 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config,
if cmd.Proxy {
go func() {
if err := cmd.startRunnerServices(ctx, devPodConfig, containerClient, log); err != nil {
log.Debug(err)
log.Error(err)
}
}()
}
Expand Down
24 changes: 8 additions & 16 deletions pkg/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"io"
"os"
"strings"
"sync"
"time"

"github.com/loft-sh/devpod/pkg/command"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
perrors "github.com/pkg/errors"
)
Expand Down Expand Up @@ -116,8 +114,7 @@ func InjectAndExecute(
case err = <-execErrChan:
result = <-injectChan
case result = <-injectChan:
// give exec some time to properly terminate and clean up
util.WaitForChan(execErrChan, 2*time.Second)
// we don't wait for the command termination here and will just retry on error
}

// prefer result error
Expand All @@ -129,7 +126,7 @@ func InjectAndExecute(
return result.wasExecuted, nil
}

log.Debug("Rerun command as binary was injected")
log.Debugf("Rerun command as binary was injected")
delayedStderr.Start()
return true, exec(ctx, scriptParams.Command, stdin, stdout, delayedStderr)
}
Expand Down Expand Up @@ -284,19 +281,14 @@ func readLine(reader io.Reader) (string, error) {
}

func pipe(toStdin io.Writer, fromStdin io.Reader, toStdout io.Writer, fromStdout io.Reader) error {
var err error
wg := sync.WaitGroup{}
wg.Add(1)
errChan := make(chan error, 2)
go func() {
defer wg.Done()
_, err = io.Copy(toStdout, fromStdout)
_, err := io.Copy(toStdout, fromStdout)
errChan <- err
}()
wg.Add(1)
go func() {
defer wg.Done()
_, err = io.Copy(toStdin, fromStdin)
_, err := io.Copy(toStdin, fromStdin)
errChan <- err
}()

wg.Wait()
return err
return <-errChan
}
7 changes: 3 additions & 4 deletions pkg/ssh/server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *Server) handler(sess ssh.Session) {
var err error
if isPty {
s.log.Debugf("Execute SSH server PTY command: %s", strings.Join(cmd.Args, " "))
err = s.HandlePTY(sess, ptyReq, winCh, cmd, nil)
err = HandlePTY(sess, ptyReq, winCh, cmd, nil)
} else {
s.log.Debugf("Execute SSH server command: %s", strings.Join(cmd.Args, " "))
err = s.HandleNonPTY(sess, cmd)
Expand Down Expand Up @@ -201,17 +201,16 @@ func (s *Server) HandleNonPTY(sess ssh.Session, cmd *exec.Cmd) (err error) {
}
}()

// order is important here!
err = cmd.Wait()
waitGroup.Wait()
err = cmd.Wait()
if err != nil {
return err
}

return nil
}

func (s *Server) HandlePTY(
func HandlePTY(
sess ssh.Session,
ptyReq ssh.Pty,
winCh <-chan ssh.Window,
Expand Down
6 changes: 3 additions & 3 deletions pkg/tunnel/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/provider"
devssh "github.com/loft-sh/devpod/pkg/ssh"
"github.com/loft-sh/devpod/pkg/util"
"github.com/loft-sh/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -129,10 +128,8 @@ func (c *ContainerHandler) Run(ctx context.Context, handler Handler, cfg *config
// wait for result
select {
case err := <-containerChan:
util.WaitForChan(tunnelChan, 2*time.Second)
return errors.Wrap(err, "tunnel to container")
case err := <-tunnelChan:
util.WaitForChan(containerChan, 2*time.Second)
return errors.Wrap(err, "connect to server")
}
}
Expand Down Expand Up @@ -207,6 +204,9 @@ func (c *ContainerHandler) runRunInContainer(ctx context.Context, sshClient *ssh
defer stdoutWriter.Close()
defer cancel()

c.log.Debugf("Run container tunnel")
defer c.log.Debugf("Container tunnel exited")

command := fmt.Sprintf("'%s' agent container-tunnel --workspace-info '%s'", c.client.AgentPath(), workspaceInfo)
if c.log.GetLevel() == logrus.DebugLevel {
command += " --debug"
Expand Down
4 changes: 0 additions & 4 deletions pkg/tunnel/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"io"
"os"
"time"

devssh "github.com/loft-sh/devpod/pkg/ssh"
"github.com/loft-sh/devpod/pkg/util"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -54,10 +52,8 @@ func NewTunnel(ctx context.Context, tunnel Tunnel, handler Handler) error {
// wait for result
select {
case err := <-innerTunnelChan:
util.WaitForChan(outerTunnelChan, 2*time.Second)
return errors.Wrap(err, "inner tunnel")
case err := <-outerTunnelChan:
util.WaitForChan(innerTunnelChan, 2*time.Second)
return errors.Wrap(err, "outer tunnel")
}
}
13 changes: 0 additions & 13 deletions pkg/util/channel.go

This file was deleted.

0 comments on commit 8f2b433

Please sign in to comment.