8000 fix: Use of r.Context() in workspaceAgentPTY · coder/coder@f61c2ad · GitHub
[go: up one dir, main page]

Skip to content

Commit f61c2ad

Browse files
committed
fix: Use of r.Context() in workspaceAgentPTY
1 parent 0b0aac0 commit f61c2ad

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

coderd/workspaceagents.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ func (api *API) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {
376376
})
377377
return
378378
}
379-
defer func() {
380-
_ = conn.Close(websocket.StatusNormalClosure, "ended")
381-
}()
379+
382380
// Accept text connections, because it's more developer friendly.
383-
wsNetConn := websocket.NetConn(r.Context(), conn, websocket.MessageBinary)
384-
agentConn, err := api.dialWorkspaceAgent(r, workspaceAgent.ID)
381+
ctx, wsNetConn := websocketNetConn(r.Context(), conn, websocket.MessageBinary)
382+
defer wsNetConn.Close() // Also closes conn.
383+
384+
agentConn, err := api.dialWorkspaceAgent(ctx, r, workspaceAgent.ID)
385385
if err != nil {
386386
_ = conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("dial workspace agent: %s", err))
387387
return
@@ -400,11 +400,13 @@ func (api *API) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {
400400
_, _ = io.Copy(ptNetConn, wsNetConn)
401401
}
402402

403-
// dialWorkspaceAgent connects to a workspace agent by ID.
404-
func (api *API) dialWorkspaceAgent(r *http.Request, agentID uuid.UUID) (*agent.Conn, error) {
403+
// dialWorkspaceAgent connects to a workspace agent by ID. Only rely on
404+
// r.Context() for cancellation if it's use is safe or r.Hijack() has
405+
// not been performed.
406+
func (api *API) dialWorkspaceAgent(ctx context.Context, r *http.Request, agentID uuid.UUID) (*agent.Conn, error) {
405407
client, server := provisionersdk.TransportPipe()
406408
go func() {
407-
_ = peerbroker.ProxyListen(r.Context(), server, peerbroker.ProxyOptions{
409+
_ = peerbroker.ProxyListen(ctx, server, peerbroker.ProxyOptions{
408410
ChannelID: agentID.String(),
409411
Logger: api.Logger.Named("peerbroker-proxy-dial"),
410412
Pubsub: api.Pubsub,
@@ -414,7 +416,7 @@ func (api *API) dialWorkspaceAgent(r *http.Request, agentID uuid.UUID) (*agent.C
414416
}()
415417

416418
peerClient := proto.NewDRPCPeerBrokerClient(provisionersdk.Conn(client))
417-
stream, err := peerClient.NegotiateConnection(r.Context())
419+
stream, err := peerClient.NegotiateConnection(ctx)
418420
if err != nil {
419421
return nil, xerrors.Errorf("negotiate: %w", err)
420422
}
@@ -426,7 +428,7 @@ func (api *API) dialWorkspaceAgent(r *http.Request, agentID uuid.UUID) (*agent.C
426428
options.SettingEngine.SetICEProxyDialer(turnconn.ProxyDialer(func() (c net.Conn, err error) {
427429
clientPipe, serverPipe := net.Pipe()
428430
go func() {
429-
<-r.Context().Done()
431+
<-ctx.Done()
430432
_ = clientPipe.Close()
431433
_ = serverPipe.Close()
432434
}()

0 commit comments

Comments
 (0)
0