Skip to content

Commit

Permalink
Throw when sync loading chunks after shutdown
Browse files Browse the repository at this point in the history
The caller would block indefinitely if a sync load is requested
during shutdown. Rather than block indefinitely, we should throw
an exception to indicate to the caller that the chunk system is
unable to process requests.
  • Loading branch information
Spottedleaf committed Dec 3, 2024
1 parent 4bd7eb8 commit 93b9083
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ private ChunkAccess syncLoad(final int chunkX, final int chunkZ, final ChunkStat
completable::complete
);

if (!completable.isDone() && chunkTaskScheduler.hasShutdown()) {
throw new IllegalStateException(
"Chunk system has shut down, cannot process chunk requests in world '" + ca.spottedleaf.moonrise.common.util.WorldUtil.getWorldName(this.level) + "' at "
+ "(" + chunkX + "," + chunkZ + ") status: " + toStatus
);
}

if (TickThread.isTickThreadFor(this.level, chunkX, chunkZ)) {
ChunkTaskScheduler.pushChunkWait(this.level, chunkX, chunkZ);
this.mainThreadProcessor.managedBlock(completable::isDone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ public void close(final boolean save, final boolean halt) {
LOGGER.error("Failed to close '" + type.name() + "' regionfile cache for world '" + WorldUtil.getWorldName(this.world) + "'", ex);
}
}

this.taskScheduler.setShutdown(true);
}

void ensureInAutosave(final NewChunkHolder holder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ public final int getChunkSystemLockShift() {
return this.lockShift;
}

private volatile boolean shutdown;

public boolean hasShutdown() {
return this.shutdown;
}

public void setShutdown(final boolean shutdown) {
this.shutdown = shutdown;
}

public ChunkTaskScheduler(final ServerLevel world) {
this.world = world;
// must be >= region shift (in paper, doesn't exist) and must be >= ticket propagator section shift
Expand Down Expand Up @@ -525,6 +535,13 @@ public ChunkAccess syncLoadNonFull(final int chunkX, final int chunkZ, final Chu
return loaded;
}

if (this.hasShutdown()) {
throw new IllegalStateException(
"Chunk system has shut down, cannot process chunk requests in world '" + ca.spottedleaf.moonrise.common.util.WorldUtil.getWorldName(this.world) + "' at "
+ "(" + chunkX + "," + chunkZ + ") status: " + status
);
}

final Long ticketId = getNextNonFullLoadId();
final int ticketLevel = getTicketLevel(status);
this.chunkHolderManager.addTicketAtLevel(NON_FULL_CHUNK_LOAD, chunkX, chunkZ, ticketLevel, ticketId);
Expand Down

0 comments on commit 93b9083

Please sign in to comment.