From 52db0a28ebd7002697d3d9ef6f38032cc012e2a0 Mon Sep 17 00:00:00 2001 From: yxw Date: Wed, 12 Jun 2024 09:48:27 +0800 Subject: [PATCH] fix: ThreadId miss unlock(#1107) --- .../src/main/java/com/alipay/sofa/jraft/util/ThreadId.java | 5 ++--- .../test/java/com/alipay/sofa/jraft/util/ThreadIdTest.java | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/util/ThreadId.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/util/ThreadId.java index 6ee10d5d1..209058d75 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/util/ThreadId.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/util/ThreadId.java @@ -124,10 +124,9 @@ public void setError(final int errorCode) { if (this.onError != null) { this.onError.onError(this, this.data, errorCode); } - } finally { - // Maybe destroyed in callback - if (!this.destroyed) { + // It may have been released during onError to avoid throwing an exception. + if (this.lock.isHeldByCurrentThread()) { this.lock.unlock(); } } diff --git a/jraft-core/src/test/java/com/alipay/sofa/jraft/util/ThreadIdTest.java b/jraft-core/src/test/java/com/alipay/sofa/jraft/util/ThreadIdTest.java index 3d4bc4d5a..e33881df7 100644 --- a/jraft-core/src/test/java/com/alipay/sofa/jraft/util/ThreadIdTest.java +++ b/jraft-core/src/test/java/com/alipay/sofa/jraft/util/ThreadIdTest.java @@ -59,7 +59,7 @@ public void run() { Thread.sleep(1000); this.id.unlock(); latch.await(); - assertEquals(1000, cost.get(), 10); + assertEquals(1000, cost.get(), 20); } @Test