binarywang/WxJava · error · WxRuntimeException

lock failed

Error message

lock failed

What it means

Thrown by JedisDistributedLock.lock() in the catch(InterruptedException) branch. If the acquiring thread is interrupted while JedisLock.acquire() blocks, the interrupt is converted to a WxRuntimeException('lock failed') wrapping the original InterruptedException.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/util/locks/JedisDistributedLock.java:35

 */
@Deprecated
public class JedisDistributedLock implements Lock {
  private final Pool<Jedis> jedisPool;
  private final JedisLock lock;

  public JedisDistributedLock(Pool<Jedis> jedisPool, String key){
    this.jedisPool = jedisPool;
    this.lock = new JedisLock(key);
  }

  @Override
  public void lock() {
    try (Jedis jedis = jedisPool.getResource()) {
      if (!lock.acquire(jedis)) {
        throw new WxRuntimeException("acquire timeouted");
      }
    } catch (InterruptedException e) {
      throw new WxRuntimeException("lock failed", e);
    }
  }

  @Override
  public void lockInterruptibly() throws InterruptedException {
    try (Jedis jedis = jedisPool.getResource()) {
      if (!lock.acquire(jedis)) {
        throw new WxRuntimeException("acquire timeouted");
      }
    }
  }

  @Override
  public boolean tryLock() {
    try (Jedis jedis = jedisPool.getResource()) {
      return lock.acquire(jedis);
    } catch (InterruptedException e) {
      throw new WxRuntimeException("lock failed", e);

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Avoid interrupting threads that perform blocking lock acquisition, or use tryLock(long, TimeUnit) which propagates InterruptedException.
  2. Catch WxRuntimeException around lock() and check getCause() instanceof InterruptedException to handle shutdown cleanly.
  3. Restore the interrupt status if you swallow it (Thread.currentThread().interrupt()).

Example fix

// before
distributedLock.lock();
// after - prefer interruptible variant for cancellable contexts
try {
  distributedLock.lockInterruptibly();
} catch (InterruptedException e) {
  Thread.currentThread().interrupt();
  throw new ServiceShutdownException(e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try {
  distributedLock.lock();
} catch (WxRuntimeException e) {
  if (e.getCause() instanceof InterruptedException) {
    Thread.currentThread().interrupt();
    // shutdown path
  }
  throw e;
}

Prevention

When it happens

Trigger: The thread executing lock() is interrupted (e.g. by application shutdown, a future cancellation, or a thread-pool eviction) while acquire() is sleeping/retrying against Redis.

Common situations: Graceful shutdown interrupting worker threads that hold pending lock acquisitions; async framework cancelling a task that was waiting on the lock; test teardown interrupting threads.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/e4eaed97972f51dd. Report an issue: GitHub.