Skip to content

fix(codersdk): keep workspace agent connection open after dial context #10863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,20 @@ func TestWorkspaceAgentTailnet(t *testing.T) {
_ = agenttest.New(t, client.URL, authToken)
resources := coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)

ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
})
conn, err := func() (*codersdk.WorkspaceAgentConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() // Connection should remain open even if the dial context is canceled.

return client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
})
}()
require.NoError(t, err)
defer conn.Close()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
session, err := sshClient.NewSession()
Expand Down Expand Up @@ -1416,12 +1423,20 @@ func TestWorkspaceAgent_UpdatedDERP(t *testing.T) {
agentID := resources[0].Agents[0].ID

// Connect from a client.
ctx := testutil.Context(t, testutil.WaitLong)
conn1, err := client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger.Named("client1"),
})
conn1, err := func() (*codersdk.WorkspaceAgentConn, error) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel() // Connection should remain open even if the dial context is canceled.

return client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{
Logger: logger.Named("client1"),
})
}()
require.NoError(t, err)
defer conn1.Close()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

ok := conn1.AwaitReachable(ctx)
require.True(t, ok)

Expand Down
43 changes: 29 additions & 14 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ type DialWorkspaceAgentOptions struct {
BlockEndpoints bool
}

func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, options *DialWorkspaceAgentOptions) (agentConn *WorkspaceAgentConn, err error) {
func (c *Client) DialWorkspaceAgent(dialCtx context.Context, agentID uuid.UUID, options *DialWorkspaceAgentOptions) (agentConn *WorkspaceAgentConn, err error) {
if options == nil {
options = &DialWorkspaceAgentOptions{}
}

connInfo, err := c.WorkspaceAgentConnectionInfo(ctx, agentID)
connInfo, err := c.WorkspaceAgentConnectionInfo(dialCtx, agentID)
if err != nil {
return nil, xerrors.Errorf("get connection info: %w", err)
}
Expand Down Expand Up @@ -302,7 +302,10 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
tokenHeader = c.SessionTokenHeader
}
headers.Set(tokenHeader, c.SessionToken())
ctx, cancel := context.WithCancel(ctx)

// New context, separate from dialCtx. We don't want to cancel the
// connection if dialCtx is canceled.
ctx, cancel := context.WithCancel(context.Background())
defer func() {
if err != nil {
cancel()
Expand All @@ -314,7 +317,9 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
return nil, xerrors.Errorf("parse url: %w", err)
}
closedCoordinator := make(chan struct{})
firstCoordinator := make(chan error)
// Must only ever be used once, send error OR close to avoid
// reassignment race. Buffered so we don't hang in goroutine.
firstCoordinator := make(chan error, 1)
go func() {
defer close(closedCoordinator)
isFirst := true
Expand Down Expand Up @@ -366,7 +371,9 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
return nil, xerrors.Errorf("parse url: %w", err)
}
closedDerpMap := make(chan struct{})
firstDerpMap := make(chan error)
// Must only ever be used once, send error OR close to avoid
// reassignment race. Buffered so we don't hang in goroutine.
firstDerpMap := make(chan error, 1)
go func() {
defer close(closedDerpMap)
isFirst := true
Expand Down Expand Up @@ -420,13 +427,21 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
}
}()

err = <-firstCoordinator
if err != nil {
return nil, err
}
err = <-firstDerpMap
if err != nil {
return nil, err
for firstCoordinator != nil || firstDerpMap != nil {
select {
case <-dialCtx.Done():
return nil, xerrors.Errorf("timed out waiting for coordinator and derp map: %w", dialCtx.Err())
case err = <-firstCoordinator:
if err != nil {
return nil, xerrors.Errorf("start coordinator: %w", err)
}
firstCoordinator = nil
case err = <-firstDerpMap:
if err != nil {
return nil, xerrors.Errorf("receive derp map: %w", err)
}
firstDerpMap = nil
}
}

agentConn = NewWorkspaceAgentConn(conn, WorkspaceAgentConnOptions{
Expand All @@ -444,9 +459,9 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
},
})

if !agentConn.AwaitReachable(ctx) {
if !agentConn.AwaitReachable(dialCtx) {
_ = agentConn.Close()
return nil, xerrors.Errorf("timed out waiting for agent to become reachable: %w", ctx.Err())
return nil, xerrors.Errorf("timed out waiting for agent to become reachable: %w", dialCtx.Err())
}

return agentConn, nil
Expand Down