Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

Commit 0b004a7

Browse files
committed
🐛 fix: Spin-block DefShardMan#shutdown() until shards disconnect or 5s passes
1 parent ed439e7 commit 0b004a7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
<groupId>ch.qos.logback</groupId>
159159
<artifactId>logback-classic</artifactId>
160160
<version>${logback.version}</version>
161-
<!-- <scope>test</scope>-->
161+
<scope>test</scope>
162162
</dependency>
163163

164164
<!-- Deep/recursive field by field comparing ignoring any equals() methods -->

src/main/java/com/mewna/catnip/shard/manager/DefaultShardManager.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.mewna.catnip.shard.CatnipShard;
3333
import com.mewna.catnip.shard.CatnipShardImpl;
3434
import com.mewna.catnip.shard.LifecycleEvent.Raw;
35+
import com.mewna.catnip.shard.LifecycleState;
3536
import com.mewna.catnip.shard.event.MessageConsumer;
3637
import com.mewna.catnip.util.task.QueueTask;
3738
import com.mewna.catnip.util.task.ShardConnectTask;
@@ -283,6 +284,28 @@ public void shutdown() {
283284
consumers.forEach(MessageConsumer::close);
284285
consumers.clear();
285286
shards.values().forEach(CatnipShard::disconnect);
287+
// Spin waiting for shards to disconnect
288+
final var blockingStates = Set.of(
289+
LifecycleState.CONNECTING,
290+
LifecycleState.CONNECTED,
291+
LifecycleState.IDENTIFYING,
292+
LifecycleState.RESUMING,
293+
LifecycleState.LOGGED_IN
294+
);
295+
var cycles = 0;
296+
while(shards.values().stream().anyMatch(shard -> shard.isConnected() || blockingStates.contains(shard.lifecycleState()))) {
297+
catnip().logAdapter().debug("Waiting for shards to disconnect...");
298+
try {
299+
//noinspection BusyWait
300+
Thread.sleep(50L);
301+
++cycles;
302+
if(cycles >= 100) {
303+
catnip().logAdapter().error("Shards took too long to disconnect (probably ~5s), giving up!");
304+
break;
305+
}
306+
} catch(final InterruptedException ignored) {
307+
}
308+
}
286309
shards.clear();
287310
}
288311
}

0 commit comments

Comments
 (0)