Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock already taken, locked nodes: [0] on unlockContext #168

Open
devang304 opened this issue Feb 25, 2025 · 0 comments
Open

lock already taken, locked nodes: [0] on unlockContext #168

devang304 opened this issue Feb 25, 2025 · 0 comments

Comments

@devang304
Copy link

Hi I have 2 helper methods that I'm using to acquire and release the mutex

func (c RedisCache) AcquireLockWithExpiry(ctx context.Context, mutexId string, ttl time.Duration) error {
	redisClient, err := c.redisClientProvider.Get(ctx, c.redisConfig)
	if err != nil {
		return errors.Join(err, errors.New("could not get redis client"))
	}

	pool := goredis.NewPool(redisClient)
	rs := redsync.New(pool)

	mutex := rs.NewMutex(mutexId, redsync.WithExpiry(ttl), redsync.WithValue(mutexId))
	c.logger.Infow(ctx, "acquiring lock in redis cache", "mutexId", mutexId, "mutexValue", mutex.Value())

	if err := mutex.LockContext(ctx); err != nil {
		c.logger.Warnw(ctx, "failed to acquire lock in redis cache", "err", err, "mutexId", mutexId)
		return errors.Join(err, internalErrors.MutexAlreadyExists)
	}

	return nil
}

func (c RedisCache) ReleaseLock(ctx context.Context, mutexId string) (bool, error) {
	redisClient, err := c.redisClientProvider.Get(ctx, c.redisConfig)
	if err != nil {
		return false, errors.Join(err, errors.New("could not get redis client"))
	}
	pool := goredis.NewPool(redisClient)
	rs := redsync.New(pool)

	mutex := rs.NewMutex(mutexId, redsync.WithValue(mutexId))
	c.logger.Infow(ctx, "releasing lock in redis cache", "mutexId", mutexId, "mutexValue", mutex.Value())

	if _, unlockErr := mutex.Unlock(); unlockErr != nil {
		c.logger.Errorw(ctx, "failed to release lock in redis cache", "unlockErr", unlockErr, "mutexId", mutexId)
		return false, errors.Join(err, internalErrors.MutexAlreadyExists)
	}

	return true, nil
}

I have an async workflow where first I call the AcquireLockWithExpiry to lock the mutex. Then I perform some business logic and call the ReleaseLock function to unlock. I use the WithValue option to use a pre-determined value that I use while locking and unlocking. Unfortunately while unlocking I get the error lock already taken, locked nodes: [0]. How do I fix this? Is there any recommendation around this? Am I supposed to use the same pool to create the mutex for locking and unlocking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant