fix: Race when shutting down and opening WebSockets (#576)

Adding to a WaitGroup while calling wait is a race condition. Surrounding
this in a mutex should solve the problem. Since context is used for
cancellation on all sockets, cleanup should occur properly.

See: https://github.com/coder/coder/runs/5701221057?check_suite_focus=true#step:10:98
This commit is contained in:
Kyle Carberry
2022-03-26 12:53:50 -06:00
committed by GitHub
parent 4448ba2778
commit 3a48e4000e
3 changed files with 12 additions and 1 deletions

View File

@ -176,7 +176,11 @@ func New(options *Options) (http.Handler, func()) {
})
})
r.NotFound(site.DefaultHandler().ServeHTTP)
return r, api.websocketWaitGroup.Wait
return r, func() {
api.websocketWaitMutex.Lock()
api.websocketWaitGroup.Wait()
api.websocketWaitMutex.Unlock()
}
}
// API contains all route handlers. Only HTTP handlers should
@ -184,5 +188,6 @@ func New(options *Options) (http.Handler, func()) {
type api struct {
*Options
websocketWaitMutex sync.Mutex
websocketWaitGroup sync.WaitGroup
}