@@ -23,6 +23,7 @@ import (
2323 "fmt"
2424 "net/http"
2525 "strings"
26+ "sync"
2627 "time"
2728
2829 "github.com/coder/websocket"
@@ -262,9 +263,12 @@ func (s *SignalWebsocket) connectLoop(
262263
263264 responseChannels := make (map [uint64 ]chan * signalpb.WebSocketResponseMessage )
264265 loopCtx , loopCancel := context .WithCancelCause (ctx )
266+ var wg sync.WaitGroup
267+ wg .Add (3 )
265268
266269 // Read loop (for reading incoming reqeusts and responses to outgoing requests)
267270 go func () {
271+ defer wg .Done ()
268272 err := readLoop (loopCtx , ws , incomingRequestChan , responseChannels )
269273 // Don't want to put an err into loopCancel if we don't have one
270274 if err != nil {
@@ -276,6 +280,7 @@ func (s *SignalWebsocket) connectLoop(
276280
277281 // Write loop (for sending outgoing requests and responses to incoming requests)
278282 go func () {
283+ defer wg .Done ()
279284 err := writeLoop (loopCtx , ws , s .sendChannel , responseChannels )
280285 // Don't want to put an err into loopCancel if we don't have one
281286 if err != nil {
@@ -287,6 +292,7 @@ func (s *SignalWebsocket) connectLoop(
287292
288293 // Ping loop (send a keepalive Ping every 30s)
289294 go func () {
295+ defer wg .Done ()
290296 ticker := time .NewTicker (30 * time .Second )
291297 defer ticker .Stop ()
292298
@@ -352,6 +358,7 @@ func (s *SignalWebsocket) connectLoop(
352358 close (responseChannel )
353359 }
354360 loopCancel (nil )
361+ wg .Wait ()
355362 log .Debug ().Msg ("Finished websocket cleanup" )
356363 if errorCount > 500 {
357364 // Something is really wrong, we better panic.
0 commit comments