clean up chunk build outputs and renderer resources after job failures #54

Merged
embeddedt merged 2 commits from rdh/celeritas:thingy into stonecutter 2026-11-24 09:02:00 -05:00
2 changed files with 32 additions and 10 deletions

No files matched your search

@@ -789,24 +789,45 @@ public abstract class RenderSectionManager {
} }
private ArrayList<ChunkJobResult.Success<? extends ChunkTaskOutput>> collectChunkBuildResults() { private ArrayList<ChunkJobResult.Success<? extends ChunkTaskOutput>> collectChunkBuildResults() {
ArrayList<ChunkJobResult.Success<? extends ChunkTaskOutput>> results = new ArrayList<>(); var results = drainChunkBuildResults(this.buildResults);
ChunkJobResult<? extends ChunkTaskOutput> result;
while ((result = this.buildResults.poll()) != null) { for (var successfulResult : results) {
if (result instanceof ChunkJobResult.Success<? extends ChunkTaskOutput> successfulResult) {
this.jobMetricsTracker.collectMetrics(successfulResult); this.jobMetricsTracker.collectMetrics(successfulResult);
if (successfulResult.output() instanceof ChunkSortOutput sort && sort.render.isTreeSorted() && successfulResult.executionTimeNanos() >= 0) { if (successfulResult.output() instanceof ChunkSortOutput sort && sort.render.isTreeSorted() && successfulResult.executionTimeNanos() >= 0) {
this.treeSortMetrics.collect(successfulResult.executionTimeNanos()); this.treeSortMetrics.collect(successfulResult.executionTimeNanos());
} }
}
return results;
}
static ArrayList<ChunkJobResult.Success<? extends ChunkTaskOutput>> drainChunkBuildResults(Queue<ChunkJobResult<? extends ChunkTaskOutput>> buildResults) {
ArrayList<ChunkJobResult.Success<? extends ChunkTaskOutput>> results = new ArrayList<>();
RuntimeException firstFailure = null;
ChunkJobResult<? extends ChunkTaskOutput> result;
while ((result = buildResults.poll()) != null) {
if (result instanceof ChunkJobResult.Success<? extends ChunkTaskOutput> successfulResult) {
results.add(successfulResult); results.add(successfulResult);
} else if (result instanceof ChunkJobResult.Failure<? extends ChunkTaskOutput> failure) { } else if (result instanceof ChunkJobResult.Failure<? extends ChunkTaskOutput> failure) {
failure.abort(); if (firstFailure == null) {
firstFailure = failure.asException();
} else if (firstFailure != failure.throwable()) {
firstFailure.addSuppressed(failure.throwable());
}
} else { } else {
throw new AssertionError(); throw new AssertionError();
} }
} }
if (firstFailure != null) {
for (var successfulResult : results) {
successfulResult.output().delete();
}
throw firstFailure;
}
return results; return results;
} }
@@ -917,10 +938,11 @@ public abstract class RenderSectionManager {
this.builder.shutdown(); // stop all the workers, and cancel any tasks this.builder.shutdown(); // stop all the workers, and cancel any tasks
try {
for (var result : this.collectChunkBuildResults()) { for (var result : this.collectChunkBuildResults()) {
result.output().delete(); // delete resources for any pending tasks (including those that were cancelled) result.output().delete(); // delete resources for any pending tasks (including those that were cancelled)
} }
} finally {
this.renderListManager.destroy(); this.renderListManager.destroy();
if (this.shadowRenderListManager != null) { if (this.shadowRenderListManager != null) {
this.shadowRenderListManager.destroy(); this.shadowRenderListManager.destroy();
@@ -937,6 +959,7 @@ public abstract class RenderSectionManager {
this.sectionsWithGlobalEntities.clear(); this.sectionsWithGlobalEntities.clear();
} }
}
public int getTotalSections() { public int getTotalSections() {
return this.sectionByPosition.size(); return this.sectionByPosition.size();
@@ -12,12 +12,11 @@ public sealed interface ChunkJobResult<OUTPUT> permits ChunkJobResult.Success, C
Objects.requireNonNull(throwable); Objects.requireNonNull(throwable);
} }
public void abort() { public RuntimeException asException() {
if (this.throwable instanceof RuntimeException crashException) { if (this.throwable instanceof RuntimeException crashException) {
// Propagate RuntimeExceptions directly to provide extra information if they are a vanilla crash exception return crashException;
throw crashException;
} else { } else {
throw new RuntimeException("Exception thrown while executing job", this.throwable); return new RuntimeException("Exception thrown while executing job", this.throwable);
} }
} }
} }