File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -468,14 +468,25 @@ func (c *Client) connect(ctx context.Context) error {
468468 // Start ping sender (sends to write channel, not directly to websocket)
469469 pingCtx , cancelPing := context .WithCancel (ctx )
470470 defer cancelPing ()
471- go c .sendPings (pingCtx )
471+ pingDone := make (chan struct {})
472+ go func () {
473+ c .sendPings (pingCtx )
474+ close (pingDone )
475+ }()
472476
473477 // Read events - when this returns, cancel everything
474478 readErr := c .readEvents (ctx , ws )
475479
476- // Stop write pump and ping sender
477- cancelWrite ()
480+ // Stop ping sender first - this ensures no more writes will be queued
478481 cancelPing ()
482+ <- pingDone // Wait for ping sender to fully exit
483+
484+ // Stop write pump
485+ cancelWrite ()
486+
487+ // Close write channel to signal writePump to exit cleanly
488+ // Safe to close now because ping sender has exited and won't write anymore
489+ close (c .writeCh )
479490
480491 // Wait for write pump to finish
481492 writeErr := <- writeDone
You can’t perform that action at this time.
0 commit comments