Skip to content

Fix Redis lock key serialization inconsistency in RedisTemplateSimpleDistributedLock #3650

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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 24, 2025

Problem

The RedisTemplateSimpleDistributedLock class had an inconsistency between tryLock() and unlock() methods when handling key serialization:

  • tryLock(): Used low-level RedisConnection API with manual byte conversion, bypassing custom key serializers
  • unlock(): Used high-level StringRedisTemplate API which applies custom key serializers

This caused locks to fail to unlock when custom key serializers (like adding prefixes) were configured, because:

  • Lock acquired with key: wechat_cp_lock:ww948:1000010:accessToken
  • Unlock attempted with key: System:wechat_cp_lock:ww948:1000010:accessToken

Solution

Modified tryLock() method to use the same high-level StringRedisTemplate API as unlock():

// Before: Low-level API bypassing serializers
final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
List<Object> redisResults = redisTemplate.executePipelined((RedisCallback<String>) connection -> {
  connection.set(keyBytes, valueBytes, Expiration.milliseconds(leaseMilliseconds), RedisStringCommands.SetOption.SET_IF_ABSENT);
  // ...
});

// After: High-level API respecting serializers  
Boolean lockAcquired = redisTemplate.opsForValue().setIfAbsent(key, value, leaseMilliseconds, TimeUnit.MILLISECONDS);

Changes Made

  • Main fix: Replaced low-level Redis API calls with high-level StringRedisTemplate API in tryLock()
  • Cleanup: Removed unused imports and dependencies
  • Testing: Added comprehensive test with custom key serializers
  • Minimal impact: Only 10 lines added, 14 lines removed in core implementation

Testing

Added RedisTemplateSimpleDistributedLockSerializationTest which verifies:

  • Lock operations work correctly with custom key serializers
  • Both acquisition and release use consistent key serialization
  • Reentrant behavior is maintained
  • Lock exclusivity between instances is preserved

Fixes #3631.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] RedisTemplateSimpleDistributedLock 在trylock和unlock 中针对Key的序列化方式不一致导致无法unlock的问题。 Fix Redis lock key serialization inconsistency in RedisTemplateSimpleDistributedLock Jul 24, 2025
@Copilot Copilot AI requested a review from binarywang July 24, 2025 07:55
Copilot finished work on behalf of binarywang July 24, 2025 07:55
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

Successfully merging this pull request may close these issues.

RedisTemplateSimpleDistributedLock 在trylock和unlock 中针对Key的序列化方式不一致导致无法unlock的问题。
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy