Skip to content

Commit

Permalink
fix(core): reset the MDC when we clean the run context
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jan 28, 2025
1 parent fd4a512 commit c9095bd
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ public void cleanup() {
} catch (IOException ex) {
logger().warn("Unable to cleanup worker task", ex);
}

logger.resetMDC();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private Executor onEnd(Executor executor) {

logService.logExecution(
newExecution,
flow.logger(),
logger,
Level.INFO,
"Flow completed with state {} in {}",
newExecution.getState().getCurrent(),
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/io/kestra/core/runners/RunContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public abstract class RunContext {

/**
* Cleanup any temporary resources, files created through this context.
* Also reset logs MDC so the logger should not be used after this point.
*/
public abstract void cleanup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ public org.slf4j.Logger logger() {
return this.logger;
}

public void resetMDC() {
MDC.clear();
}

@Override
public org.slf4j.Logger get() {
return logger();
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/io/kestra/core/runners/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ private void handleTask(final WorkerTask workerTask) {
}
}
} finally {
runContext.cleanup();
this.logTerminated(workerTask);
runContext.cleanup();
}
} else {
throw new RuntimeException("Unable to process the task '" + workerTask.getTask().getId() + "' as it's not a runnable task");
Expand Down Expand Up @@ -570,8 +570,6 @@ private void handleTrigger(WorkerTrigger workerTrigger) {
} catch (Exception e) {
this.handleTriggerError(workerTrigger, e);
} finally {
workerTrigger.getConditionContext().getRunContext().cleanup();

logService.logTrigger(
workerTrigger.getTriggerContext(),
runContext.logger(),
Expand All @@ -580,6 +578,8 @@ private void handleTrigger(WorkerTrigger workerTrigger) {
workerTrigger.getTrigger().getType(),
DurationFormatUtils.formatDurationHMS(stopWatch.getTime(TimeUnit.MILLISECONDS))
);

workerTrigger.getConditionContext().getRunContext().cleanup();
}

this.evaluateTriggerRunningCount.get(workerTrigger.getTriggerContext().uid()).addAndGet(-1);
Expand Down Expand Up @@ -691,12 +691,12 @@ private WorkerTaskResult run(WorkerTask workerTask, Boolean cleanUp) {
}
return workerTaskResult;
} finally {
this.logTerminated(workerTask);

// remove tmp directory
if (cleanUp) {
workerTask.getRunContext().cleanup();
}

this.logTerminated(workerTask);
}
}

Expand Down

0 comments on commit c9095bd

Please sign in to comment.