Skip to content

Commit

Permalink
fix: use a fresh context for etcd unlock
Browse files Browse the repository at this point in the history
By the time unlock is called, context might be already canceled.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed May 1, 2024
1 parent 84cd7db commit 07f7818
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/pkg/etcd/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package etcd
import (
"context"
"fmt"
"time"

"go.etcd.io/etcd/client/v3/concurrency"
"go.uber.org/zap"
Expand Down Expand Up @@ -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()
}

0 comments on commit 07f7818

Please sign in to comment.