fix(core): Release LS trace clients on run finalization (no-changelog) (#28254)

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
This commit is contained in:
oleg 2026-04-10 11:42:00 +02:00 committed by GitHub
parent 98534c6db9
commit 320a4b244d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 0 deletions

View file

@ -17,6 +17,7 @@ export type { DomainAccessTracker } from './domain-access';
export {
createInstanceAiTraceContext,
continueInstanceAiTraceContext,
releaseTraceClient,
withCurrentTraceSpan,
} from './tracing/langsmith-tracing';
export { createInstanceAgent } from './agent/instance-agent';

View file

@ -423,6 +423,15 @@ function mergeRunTreeInputs(
};
}
/**
* Unconditionally remove the cached LangSmith Client for a trace.
* Call after run finalization (success or failure) so the Client and
* its RunTree hierarchy can be garbage-collected.
*/
export function releaseTraceClient(traceId: string): void {
traceClients.delete(traceId);
}
export function getTraceParentRun(): RunTree | undefined {
const overrideRun = traceParentOverrideStorage.getStore()?.current;
if (overrideRun) {

View file

@ -34,6 +34,7 @@ import {
PlannedTaskCoordinator,
PlannedTaskStorage,
applyPlannedTaskPermissions,
releaseTraceClient,
resumeAgentRun,
RunStateRegistry,
startBuildWorkflowAgentTask,
@ -544,6 +545,8 @@ export class InstanceAiService {
threadId: tracing.rootRun.metadata?.thread_id,
error: getErrorMessage(error),
});
} finally {
releaseTraceClient(tracing.rootRun.traceId);
}
}
@ -574,6 +577,7 @@ export class InstanceAiService {
private deleteTraceContextsForThread(threadId: string): void {
for (const [runId, entry] of this.traceContextsByRunId) {
if (entry.threadId === threadId) {
releaseTraceClient(entry.tracing.rootRun.traceId);
this.traceContextsByRunId.delete(runId);
}
}
@ -609,6 +613,8 @@ export class InstanceAiService {
traceRunId: traceContext.rootRun.id,
error: getErrorMessage(error),
});
} finally {
releaseTraceClient(traceContext.rootRun.traceId);
}
}