fix: use a fresh context for etcd unlock

By the time unlock is called, context might be already canceled.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
This commit is contained in:
Andrey Smirnov 2024-05-01 18:59:50 +04:00
parent 84cd7dbec4
commit 07f78182c6
No known key found for this signature in database
GPG Key ID: FE042E3D4085A811

View File

@ -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()
}