From 5d61d1c7b076a82767bf380b1bd7e133d1bc1f0a Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Fri, 12 Jul 2024 17:27:10 +0100 Subject: [PATCH] log/sockstatlog: don't block for more than 5s on shutdown Fixes tailscale/corp#21618 Signed-off-by: Anton Tolchanov --- ipn/ipnlocal/local.go | 4 +++- log/sockstatlog/logger.go | 4 ++-- log/sockstatlog/logger_test.go | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 7dc8d770e..1d4057256 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -730,7 +730,9 @@ func (b *LocalBackend) Shutdown() { b.webClientShutdown() if b.sockstatLogger != nil { - b.sockstatLogger.Shutdown() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + b.sockstatLogger.Shutdown(ctx) } if b.peerAPIServer != nil { b.peerAPIServer.taildrop.Shutdown() diff --git a/log/sockstatlog/logger.go b/log/sockstatlog/logger.go index 2897b907b..3cc27c22d 100644 --- a/log/sockstatlog/logger.go +++ b/log/sockstatlog/logger.go @@ -242,12 +242,12 @@ func (l *Logger) Flush() { l.logger.StartFlush() } -func (l *Logger) Shutdown() { +func (l *Logger) Shutdown(ctx context.Context) { if l.cancelFn != nil { l.cancelFn() } l.filch.Close() - l.logger.Shutdown(context.Background()) + l.logger.Shutdown(ctx) type closeIdler interface { CloseIdleConnections() diff --git a/log/sockstatlog/logger_test.go b/log/sockstatlog/logger_test.go index b5779b932..31fb17e46 100644 --- a/log/sockstatlog/logger_test.go +++ b/log/sockstatlog/logger_test.go @@ -4,6 +4,7 @@ package sockstatlog import ( + "context" "testing" "github.com/google/go-cmp/cmp" @@ -28,7 +29,7 @@ func TestResourceCleanup(t *testing.T) { t.Fatal(err) } lg.Write([]byte("hello")) - lg.Shutdown() + lg.Shutdown(context.Background()) } func TestDelta(t *testing.T) {