Skip to content

fix: use a background context when piping derp connections #6750

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 1 commit into from
Mar 23, 2023
Merged

Conversation

kylecarbs
Copy link
Member

This was causing boatloads of connects to reestablish every time...

See #6746

@kylecarbs kylecarbs requested a review from mafredri March 23, 2023 14:13
@kylecarbs kylecarbs self-assigned this Mar 23, 2023
@kylecarbs kylecarbs force-pushed the bgctxderp branch 2 times, most recently from 105c88f to 139f905 Compare March 23, 2023 14:19
@@ -437,7 +435,7 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
defer left.Close()
defer right.Close()
brw := bufio.NewReadWriter(bufio.NewReader(right), bufio.NewWriter(right))
api.DERPServer.Accept(ctx, right, brw, r.RemoteAddr)
api.DERPServer.Accept(ctx, right, brw, "internal")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this on the previous version of this PR, but I guess the suggestion still applies:


How do we avoid leaks here? Is left guaranteed to be closed at an appropriate time and propagate to right?

Should we actually do ctx, cancel := context.WithCancel(api.ctx) outside here and call cancel() in agentConn.CloseFunc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, good point! Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@kylecarbs kylecarbs force-pushed the bgctxderp branch 2 times, most recently from e7a63dd to 7798533 Compare March 23, 2023 14:27
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving albeit uncommon use of context.

@@ -414,10 +414,8 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
httpapi.Write(ctx, rw, http.StatusOK, portsResponse)
}

func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*codersdk.WorkspaceAgentConn, error) {
ctx := r.Context()
func (api *API) dialWorkspaceAgentTailnet(ctx context.Context, agentID uuid.UUID) (*codersdk.WorkspaceAgentConn, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a context to a function that returns something tied to that context is a bit weird and uncommon (think net.Dial, would be awkward if it closed the connection once the context is cancelled).

Not a huge issue, but could trip someone up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I'm going to remove the ctx here now!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Give it one last look please just to confirm I didn't do anything wacky!

This was causing boatloads of connects to reestablish every time...

See #6746
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@kylecarbs kylecarbs enabled auto-merge (squash) March 23, 2023 14:39
@kylecarbs kylecarbs disabled auto-merge March 23, 2023 14:54
@kylecarbs kylecarbs merged commit ed9a3b9 into main Mar 23, 2023
@kylecarbs kylecarbs deleted the bgctxderp branch March 23, 2023 14:54
@github-actions github-actions bot locked and limited conversation to collaborators Mar 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants