Skip to content

Commit 320a4b2

Browse files
authored
fix(core): Release LS trace clients on run finalization (no-changelog) (#28254)
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
1 parent 98534c6 commit 320a4b2

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

packages/@n8n/instance-ai/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type { DomainAccessTracker } from './domain-access';
1717
export {
1818
createInstanceAiTraceContext,
1919
continueInstanceAiTraceContext,
20+
releaseTraceClient,
2021
withCurrentTraceSpan,
2122
} from './tracing/langsmith-tracing';
2223
export { createInstanceAgent } from './agent/instance-agent';

packages/@n8n/instance-ai/src/tracing/langsmith-tracing.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,15 @@ function mergeRunTreeInputs(
423423
};
424424
}
425425

426+
/**
427+
* Unconditionally remove the cached LangSmith Client for a trace.
428+
* Call after run finalization (success or failure) so the Client and
429+
* its RunTree hierarchy can be garbage-collected.
430+
*/
431+
export function releaseTraceClient(traceId: string): void {
432+
traceClients.delete(traceId);
433+
}
434+
426435
export function getTraceParentRun(): RunTree | undefined {
427436
const overrideRun = traceParentOverrideStorage.getStore()?.current;
428437
if (overrideRun) {

packages/cli/src/modules/instance-ai/instance-ai.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
PlannedTaskCoordinator,
3535
PlannedTaskStorage,
3636
applyPlannedTaskPermissions,
37+
releaseTraceClient,
3738
resumeAgentRun,
3839
RunStateRegistry,
3940
startBuildWorkflowAgentTask,
@@ -544,6 +545,8 @@ export class InstanceAiService {
544545
threadId: tracing.rootRun.metadata?.thread_id,
545546
error: getErrorMessage(error),
546547
});
548+
} finally {
549+
releaseTraceClient(tracing.rootRun.traceId);
547550
}
548551
}
549552

@@ -574,6 +577,7 @@ export class InstanceAiService {
574577
private deleteTraceContextsForThread(threadId: string): void {
575578
for (const [runId, entry] of this.traceContextsByRunId) {
576579
if (entry.threadId === threadId) {
580+
releaseTraceClient(entry.tracing.rootRun.traceId);
577581
this.traceContextsByRunId.delete(runId);
578582
}
579583
}
@@ -609,6 +613,8 @@ export class InstanceAiService {
609613
traceRunId: traceContext.rootRun.id,
610614
error: getErrorMessage(error),
611615
});
616+
} finally {
617+
releaseTraceClient(traceContext.rootRun.traceId);
612618
}
613619
}
614620

0 commit comments

Comments
 (0)