- Notifications
You must be signed in to change notification settings - Fork329
Closed
Description
nhooyr.io/websocket v1.8.10
The library uses deferdefer wg.Wait()
in multiple functions and that causes calls to deadlock instead of properly shutting down when connection is terminated.
One possible situation is this:
- The server calls
CloseRead
- The Client sends some message
- Goroutine in
CloseRead
unblocks fromReader(ctx)
and callsClose
Close
blocks ondefer c.wg.Wait()
but it can't succeed due toCloseRead
callingwg.Add(1)
and expecting it to be decremented withdefer wg.Done()
on exit fromCloseRead
. ButCloseRead
cannot exit due toClose
blocking. We have a deadlock and a goroutine leak. Context is never cancelled either which means all the other code that interacts with websocket also can't properly exit.
Switching to manually callingRead
in a separate goroutine and cancelling context from it solves this problem. Basically, you have to reimplementCloseRead
but without blocking on waitGroup