@@ -878,13 +878,15 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
878
878
})
879
879
return
880
880
}
881
- nconn := websocket . NetConn (ctx , ws , websocket .MessageBinary )
881
+ ctx , nconn := websocketNetConn (ctx , ws , websocket .MessageBinary )
882
882
defer nconn .Close ()
883
883
884
884
// Slurp all packets from the connection into io.Discard so pongs get sent
885
- // by the websocket package.
885
+ // by the websocket package. We don't do any reads ourselves so this is
886
+ // necessary.
886
887
go func () {
887
888
_ , _ = io .Copy (io .Discard , nconn )
889
+ _ = nconn .Close ()
888
890
}()
889
891
890
892
go func (ctx context.Context ) {
@@ -899,13 +901,11 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
899
901
return
900
902
}
901
903
902
- // We don't need a context that times out here because the ping will
903
- // eventually go through. If the context times out, then other
904
- // websocket read operations will receive an error, obfuscating the
905
- // actual problem.
904
+ ctx , cancel := context .WithTimeout (ctx , 30 * time .Second )
906
905
err := ws .Ping (ctx )
906
+ cancel ()
907
907
if err != nil {
908
- _ = ws .Close (websocket . StatusInternalError , err . Error () )
908
+ _ = nconn .Close ()
909
909
return
910
910
}
911
911
}
@@ -920,7 +920,7 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
920
920
if lastDERPMap == nil || ! tailnet .CompareDERPMaps (lastDERPMap , derpMap ) {
921
921
err := json .NewEncoder (nconn ).Encode (derpMap )
922
922
if err != nil {
923
- _ = ws .Close (websocket . StatusInternalError , err . Error () )
923
+ _ = nconn .Close ()
924
924
return
925
925
}
926
926
lastDERPMap = derpMap
0 commit comments