Skip to content

Commit c5b3fa0

Browse files
fix(pro): release workspace lock early in local-agent + proxy mode
1 parent 0ddf4f2 commit c5b3fa0

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

cmd/ssh.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os/exec"
1212
"path/filepath"
1313
"strings"
14-
"sync"
1514
"time"
1615

1716
"github.com/loft-sh/devpod/cmd/agent/workspace"
@@ -263,12 +262,11 @@ func (cmd *SSHCmd) jumpContainer(
263262
log log.Logger,
264263
) error {
265264
// lock the workspace as long as we init the connection
266-
unlockOnce := sync.Once{}
267265
err := client.Lock(ctx)
268266
if err != nil {
269267
return err
270268
}
271-
defer unlockOnce.Do(client.Unlock)
269+
defer client.Unlock()
272270

273271
// start the workspace
274272
err = startWait(ctx, client, false, log)
@@ -283,9 +281,21 @@ func (cmd *SSHCmd) jumpContainer(
283281

284282
// We can optimize if we know we're on pro and the client is local
285283
if cmd.Proxy && client.AgentLocal() {
286-
return cmd.jumpLocalProxyContainer(ctx, devPodConfig, client, log, func(ctx context.Context, command string, sshClient *ssh.Client) error {
287-
// we have a connection to the container, make sure others can connect as well
288-
unlockOnce.Do(client.Unlock)
284+
encodedWorkspaceInfo, _, err := client.AgentInfo(provider.CLIOptions{Proxy: true})
285+
if err != nil {
286+
return fmt.Errorf("prepare workspace info: %w", err)
287+
}
288+
// we don't need the client anymore, can unlock
289+
client.Unlock()
290+
291+
shouldExit, workspaceInfo, err := agent.WorkspaceInfo(encodedWorkspaceInfo, log)
292+
if err != nil {
293+
return err
294+
} else if shouldExit {
295+
return nil
296+
}
297+
298+
return cmd.jumpLocalProxyContainer(ctx, devPodConfig, workspaceInfo, log, func(ctx context.Context, command string, sshClient *ssh.Client) error {
289299
writer := log.Writer(logrus.InfoLevel, false)
290300
defer writer.Close()
291301

@@ -297,7 +307,7 @@ func (cmd *SSHCmd) jumpContainer(
297307
return tunnel.NewContainerTunnel(client, cmd.Proxy, log).
298308
Run(ctx, func(ctx context.Context, containerClient *ssh.Client) error {
299309
// we have a connection to the container, make sure others can connect as well
300-
unlockOnce.Do(client.Unlock)
310+
client.Unlock()
301311

302312
// start ssh tunnel
303313
return cmd.startTunnel(ctx, devPodConfig, containerClient, client, log)
@@ -672,19 +682,8 @@ func (cmd *SSHCmd) setupGPGAgent(
672682
// This completely skips the agent.
673683
//
674684
// WARN: This is considered experimental for the time being!
675-
func (cmd *SSHCmd) jumpLocalProxyContainer(ctx context.Context, devPodConfig *config.Config, client client2.WorkspaceClient, log log.Logger, exec func(ctx context.Context, command string, sshClient *ssh.Client) error) error {
676-
encodedWorkspaceInfo, _, err := client.AgentInfo(provider.CLIOptions{Proxy: true})
677-
if err != nil {
678-
return fmt.Errorf("prepare workspace info: %w", err)
679-
}
680-
shouldExit, workspaceInfo, err := agent.WorkspaceInfo(encodedWorkspaceInfo, log)
681-
if err != nil {
682-
return err
683-
} else if shouldExit {
684-
return nil
685-
}
686-
687-
_, err = workspace.InitContentFolder(workspaceInfo, log)
685+
func (cmd *SSHCmd) jumpLocalProxyContainer(ctx context.Context, devPodConfig *config.Config, workspaceInfo *provider.AgentWorkspaceInfo, log log.Logger, exec func(ctx context.Context, command string, sshClient *ssh.Client) error) error {
686+
_, err := workspace.InitContentFolder(workspaceInfo, log)
688687
if err != nil {
689688
return err
690689
}
@@ -759,7 +758,7 @@ func (cmd *SSHCmd) jumpLocalProxyContainer(ctx context.Context, devPodConfig *co
759758
}
760759
}()
761760

762-
workdir := filepath.Join("/workspaces", client.Workspace())
761+
workdir := filepath.Join("/workspaces", workspaceInfo.Workspace.ID)
763762
if cmd.WorkDir != "" {
764763
workdir = cmd.WorkDir
765764
}

0 commit comments

Comments
 (0)