From e1b958284eb27a7d54f57ebcbb0c685407b09f33 Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Thu, 26 Nov 2020 15:49:39 +0100 Subject: [PATCH] lib/api: Shut the api down gracefully (fixes #7138) (#7157) --- lib/api/api.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/api/api.go b/lib/api/api.go index 727675465..330169d80 100644 --- a/lib/api/api.go +++ b/lib/api/api.go @@ -415,7 +415,13 @@ func (s *service) Serve(ctx context.Context) error { // Restart due to listen/serve failure l.Warnln("GUI/API:", err, "(restarting)") } - srv.Close() + // Give it a moment to shut down gracefully, e.g. if we are restarting + // due to a config change through the API, let that finish successfully. + timeout, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + defer cancel() + if err := srv.Shutdown(timeout); err == timeout.Err() { + srv.Close() + } return err }