mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
fix: avoid derp-map updates endpoint leak (#9390)
This commit is contained in:
@ -878,13 +878,15 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nconn := websocket.NetConn(ctx, ws, websocket.MessageBinary)
|
ctx, nconn := websocketNetConn(ctx, ws, websocket.MessageBinary)
|
||||||
defer nconn.Close()
|
defer nconn.Close()
|
||||||
|
|
||||||
// Slurp all packets from the connection into io.Discard so pongs get sent
|
// Slurp all packets from the connection into io.Discard so pongs get sent
|
||||||
// by the websocket package.
|
// by the websocket package. We don't do any reads ourselves so this is
|
||||||
|
// necessary.
|
||||||
go func() {
|
go func() {
|
||||||
_, _ = io.Copy(io.Discard, nconn)
|
_, _ = io.Copy(io.Discard, nconn)
|
||||||
|
_ = nconn.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func(ctx context.Context) {
|
go func(ctx context.Context) {
|
||||||
@ -899,13 +901,11 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't need a context that times out here because the ping will
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
// eventually go through. If the context times out, then other
|
|
||||||
// websocket read operations will receive an error, obfuscating the
|
|
||||||
// actual problem.
|
|
||||||
err := ws.Ping(ctx)
|
err := ws.Ping(ctx)
|
||||||
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = ws.Close(websocket.StatusInternalError, err.Error())
|
_ = nconn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -920,7 +920,7 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
|
|||||||
if lastDERPMap == nil || !tailnet.CompareDERPMaps(lastDERPMap, derpMap) {
|
if lastDERPMap == nil || !tailnet.CompareDERPMaps(lastDERPMap, derpMap) {
|
||||||
err := json.NewEncoder(nconn).Encode(derpMap)
|
err := json.NewEncoder(nconn).Encode(derpMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = ws.Close(websocket.StatusInternalError, err.Error())
|
_ = nconn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lastDERPMap = derpMap
|
lastDERPMap = derpMap
|
||||||
|
Reference in New Issue
Block a user