From 07f78182c621296e6c694b64ead8f14695b2e3b7 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 1 May 2024 18:59:50 +0400 Subject: [PATCH] fix: use a fresh context for etcd unlock By the time unlock is called, context might be already canceled. Signed-off-by: Andrey Smirnov --- internal/pkg/etcd/lock.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/pkg/etcd/lock.go b/internal/pkg/etcd/lock.go index bb9a6a7fd5..79db309b56 100644 --- a/internal/pkg/etcd/lock.go +++ b/internal/pkg/etcd/lock.go @@ -7,6 +7,7 @@ package etcd import ( "context" "fmt" + "time" "go.etcd.io/etcd/client/v3/concurrency" "go.uber.org/zap" @@ -38,7 +39,16 @@ func WithLock(ctx context.Context, key string, logger *zap.Logger, f func() erro logger.Debug("mutex acquired", zap.String("key", key)) - defer mutex.Unlock(ctx) //nolint:errcheck + defer func() { + logger.Debug("releasing mutex", zap.String("key", key)) + + unlockCtx, unlockCancel := context.WithTimeout(context.Background(), 30*time.Second) + defer unlockCancel() + + if err = mutex.Unlock(unlockCtx); err != nil { + logger.Error("error releasing mutex", zap.String("key", key), zap.Error(err)) + } + }() return f() }