Skip to content

Commit

Permalink
(fix) Call NodeImpl#join asynchronously after shutting down node, #715,
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 authored Nov 23, 2021
1 parent 613fdde commit 426cd15
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public void onEvent(final LogEntryAndClosure event, final long sequence, final b
}

private void reset() {
for (final LogEntryAndClosure task : tasks) {
for (final LogEntryAndClosure task : this.tasks) {
task.reset();
}
this.tasks.clear();
Expand Down Expand Up @@ -2731,7 +2731,7 @@ public boolean isLeader(final boolean blocking) {
}

@Override
public void shutdown(final Closure done) {
public void shutdown(Closure done) {
List<RepeatedTimer> timers = null;
this.writeLock.lock();
try {
Expand Down Expand Up @@ -2785,23 +2785,34 @@ public void shutdown(final Closure done) {
if (this.state != State.STATE_SHUTDOWN) {
if (done != null) {
this.shutdownContinuations.add(done);
done = null;
}
return;
}

// This node is down, it's ok to invoke done right now. Don't invoke this
// in place to avoid the dead writeLock issue when done.Run() is going to acquire
// a writeLock which is already held by the caller
if (done != null) {
Utils.runClosureInThread(done);
}
} finally {
this.writeLock.unlock();

// Destroy all timers out of lock
if (timers != null) {
destroyAllTimers(timers);
}
// Call join() asynchronously
final Closure shutdownHook = done;
Utils.runInThread(() -> {
try {
join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
// This node is down, it's ok to invoke done right now. Don't invoke this
// in place to avoid the dead writeLock issue when done.Run() is going to acquire
// a writeLock which is already held by the caller
if (shutdownHook != null) {
shutdownHook.run(Status.OK());
}
}
});

}
}

Expand Down Expand Up @@ -2850,6 +2861,8 @@ public synchronized void join() throws InterruptedException {
}
this.shutdownLatch.await();
this.applyDisruptor.shutdown();
this.applyQueue = null;
this.applyDisruptor = null;
this.shutdownLatch = null;
}
if (this.fsmCaller != null) {
Expand Down

0 comments on commit 426cd15

Please sign in to comment.