From 5f28220eec60c00057700efbbb71fe000680cd30 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Mon, 25 Mar 2024 22:04:15 -0500 Subject: [PATCH] fix(coderd): add timeout to websocket waitgroup on shutdown (#12754) --- coderd/coderd.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/coderd/coderd.go b/coderd/coderd.go index eaeb46e46d..1de79b7e53 100644 --- a/coderd/coderd.go +++ b/coderd/coderd.go @@ -1285,9 +1285,23 @@ func (api *API) Close() error { api.derpCloseFunc() } - api.WebsocketWaitMutex.Lock() - api.WebsocketWaitGroup.Wait() - api.WebsocketWaitMutex.Unlock() + wsDone := make(chan struct{}) + timer := time.NewTimer(10 * time.Second) + defer timer.Stop() + go func() { + api.WebsocketWaitMutex.Lock() + defer api.WebsocketWaitMutex.Unlock() + api.WebsocketWaitGroup.Wait() + close(wsDone) + }() + // This will technically leak the above func if the timer fires, but this is + // maintly a last ditch effort to un-stuck coderd on shutdown. This + // shouldn't affect tests at all. + select { + case <-wsDone: + case <-timer.C: + api.Logger.Warn(api.ctx, "websocket shutdown timed out after 10 seconds") + } api.dbRolluper.Close() api.metricsCache.Close()