Skip to content

Commit

Permalink
fix: OnConnected is called with a copied context.
Browse files Browse the repository at this point in the history
  • Loading branch information
tttoad committed Jan 7, 2025
1 parent f3500f0 commit 6192b0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions server/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"context"
"time"
)

// Detach returns a context that keeps all the values of its parent context
// but detaches from the cancellation and error handling.
func Detach(ctx context.Context) context.Context { return detachedContext{ctx} }

type detachedContext struct{ parent context.Context }

func (v detachedContext) Deadline() (time.Time, bool) { return time.Time{}, false }
func (v detachedContext) Done() <-chan struct{} { return nil }
func (v detachedContext) Err() error { return nil }
func (v detachedContext) Value(key interface{}) interface{} { return v.parent.Value(key) }
2 changes: 1 addition & 1 deletion server/serverimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (s *server) httpHandler(w http.ResponseWriter, req *http.Request) {

// Return from this func to reduce memory usage.
// Handle the connection on a separate goroutine.
go s.handleWSConnection(req.Context(), conn, connectionCallbacks)
go s.handleWSConnection(Detach(req.Context()), conn, connectionCallbacks)
}

func (s *server) handleWSConnection(reqCtx context.Context, wsConn *websocket.Conn, connectionCallbacks serverTypes.ConnectionCallbacks) {
Expand Down

0 comments on commit 6192b0e

Please sign in to comment.