From dca936c7e3b04cf7b1a998f410874f7a439b6fb6 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Mon, 27 Jul 2026 12:43:18 +0200 Subject: [PATCH] Clearing additional objects map Besides calling Closeable for every additional object, the holding map should be clear in case the WorkflowInstance is not removed from memory (which currrently might happen only for scheduled instances) Signed-off-by: Francisco Javier Tirado Sarti --- .../impl/WorkflowMutableInstance.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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); }