Skip to content

Commit 426cd15

Browse files
authored
(fix) Call NodeImpl#join asynchronously after shutting down node, #715, #718 (#722)
1 parent 613fdde commit 426cd15

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public void onEvent(final LogEntryAndClosure event, final long sequence, final b
315315
}
316316

317317
private void reset() {
318-
for (final LogEntryAndClosure task : tasks) {
318+
for (final LogEntryAndClosure task : this.tasks) {
319319
task.reset();
320320
}
321321
this.tasks.clear();
@@ -2731,7 +2731,7 @@ public boolean isLeader(final boolean blocking) {
27312731
}
27322732

27332733
@Override
2734-
public void shutdown(final Closure done) {
2734+
public void shutdown(Closure done) {
27352735
List<RepeatedTimer> timers = null;
27362736
this.writeLock.lock();
27372737
try {
@@ -2785,23 +2785,34 @@ public void shutdown(final Closure done) {
27852785
if (this.state != State.STATE_SHUTDOWN) {
27862786
if (done != null) {
27872787
this.shutdownContinuations.add(done);
2788+
done = null;
27882789
}
27892790
return;
27902791
}
2791-
2792-
// This node is down, it's ok to invoke done right now. Don't invoke this
2793-
// in place to avoid the dead writeLock issue when done.Run() is going to acquire
2794-
// a writeLock which is already held by the caller
2795-
if (done != null) {
2796-
Utils.runClosureInThread(done);
2797-
}
27982792
} finally {
27992793
this.writeLock.unlock();
28002794

28012795
// Destroy all timers out of lock
28022796
if (timers != null) {
28032797
destroyAllTimers(timers);
28042798
}
2799+
// Call join() asynchronously
2800+
final Closure shutdownHook = done;
2801+
Utils.runInThread(() -> {
2802+
try {
2803+
join();
2804+
} catch (InterruptedException e) {
2805+
Thread.currentThread().interrupt();
2806+
} finally {
2807+
// This node is down, it's ok to invoke done right now. Don't invoke this
2808+
// in place to avoid the dead writeLock issue when done.Run() is going to acquire
2809+
// a writeLock which is already held by the caller
2810+
if (shutdownHook != null) {
2811+
shutdownHook.run(Status.OK());
2812+
}
2813+
}
2814+
});
2815+
28052816
}
28062817
}
28072818

@@ -2850,6 +2861,8 @@ public synchronized void join() throws InterruptedException {
28502861
}
28512862
this.shutdownLatch.await();
28522863
this.applyDisruptor.shutdown();
2864+
this.applyQueue = null;
2865+
this.applyDisruptor = null;
28532866
this.shutdownLatch = null;
28542867
}
28552868
if (this.fsmCaller != null) {

0 commit comments

Comments
 (0)