diff --git a/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java index 748a426a8..683518c87 100644 --- a/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java +++ b/impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowMutableInstance.java @@ -109,20 +109,25 @@ protected final CompletableFuture startExecution( l -> l.onWorkflowCompleted( new WorkflowCompletedEvent(workflowContext, model))) - .thenApply(__ -> model))); + .thenApply(__ -> model))) + .whenComplete(this::cleanUp); futureRef.set(future); return future; } private void whenCompleted(WorkflowModel result, Throwable ex) { completedAt = Instant.now(); + if (ex != null) { + handleException(ex instanceof CompletionException ? ex = ex.getCause() : ex); + } + } + + private void cleanUp(WorkflowModel result, Throwable ex) { additionalObjects.values().stream() .filter(AutoCloseable.class::isInstance) .map(AutoCloseable.class::cast) .forEach(WorkflowUtils::safeClose); - if (ex != null) { - handleException(ex instanceof CompletionException ? ex = ex.getCause() : ex); - } + additionalObjects.clear(); workflowContext.definition().removeInstance(this); }