fix(agent): unregister temporal cancellation listener on completion

This commit is contained in:
yiling
2026-07-14 16:08:43 +08:00
parent a7dc7bbff2
commit 636e92af4c
2 changed files with 11 additions and 7 deletions
@@ -195,6 +195,7 @@ describe('Agent Activities', () => {
} as unknown as AgentExecutionContext
const addEventListenerSpy = vi.spyOn(mockTemporalContext.cancellationSignal, 'addEventListener')
const removeEventListenerSpy = vi.spyOn(mockTemporalContext.cancellationSignal, 'removeEventListener')
const executionPromise = agentChatActivity({
teamId: 't1',
@@ -223,7 +224,9 @@ describe('Agent Activities', () => {
})
await executionPromise
expect(removeEventListenerSpy).toHaveBeenCalledWith('abort', expect.any(Function))
addEventListenerSpy.mockRestore()
removeEventListenerSpy.mockRestore()
})
it('should include error message in text when stopReason is error', async () => {
+8 -7
View File
@@ -153,6 +153,10 @@ If you need to create files in the local filesystem (for example, a temporary fi
let heartbeatInterval: ReturnType<typeof setInterval> | null = null
const onAbort = () => {
harness.abort()
}
let temporalSignal: EventTarget | undefined
try {
temporalSignal = Context.current().cancellationSignal
@@ -161,13 +165,7 @@ If you need to create files in the local filesystem (for example, a temporary fi
}
if (temporalSignal) {
temporalSignal.addEventListener(
'abort',
() => {
harness.abort()
},
{ once: true },
)
temporalSignal.addEventListener('abort', onAbort, { once: true })
heartbeatInterval = setInterval(() => {
try {
@@ -259,6 +257,9 @@ If you need to create files in the local filesystem (for example, a temporary fi
if (heartbeatInterval) {
clearInterval(heartbeatInterval)
}
if (temporalSignal) {
temporalSignal.removeEventListener('abort', onAbort)
}
if (params.taskId) {
unregisterLocalCancelHandler(params.taskId)
}