Skip to content

Commit ba9ac84

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
Fix shutdown panic
1 parent 1f4f8fa commit ba9ac84

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pkg/client/client.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)