diff --git a/codersdk/workspaceagents.go b/codersdk/workspaceagents.go index 687aa704e6..6979aaf46b 100644 --- a/codersdk/workspaceagents.go +++ b/codersdk/workspaceagents.go @@ -331,6 +331,14 @@ func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logg // Need to disable compression to avoid a data-race. CompressionMode: websocket.CompressionDisabled, }) + if isFirst { + if err != nil && res.StatusCode == http.StatusConflict { + first <- readBodyAsError(res) + return + } + isFirst = false + close(first) + } if err != nil { if errors.Is(err, context.Canceled) { return @@ -338,14 +346,6 @@ func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logg logger.Debug(ctx, "failed to dial", slog.Error(err)) continue } - if isFirst { - if res.StatusCode == http.StatusConflict { - first <- readBodyAsError(res) - return - } - isFirst = false - close(first) - } sendNode, errChan := tailnet.ServeCoordinator(websocket.NetConn(ctx, ws, websocket.MessageBinary), func(node []*tailnet.Node) error { return conn.UpdateNodes(node) })