-
Notifications
You must be signed in to change notification settings - Fork 156
feat(workflow): Automatically cleanup AsyncLocalStorage on workflow context disposal #1871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mjameswh
merged 10 commits into
temporalio:main
from
mjameswh:asynclocalstorage-auto-cleanup
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
15eda53
chore: add failing test for GH 1866
chris-olszewski 29e177b
fix: no longer disable cancellation scope storage on workflow disposal
chris-olszewski 760d73d
fix: disable ALS stores when no longer needed
chris-olszewski 5d0ca24
wip
mjameswh 3c3a547
feat(workflow): Automatically cleanup AsyncLocalStorage on workflow c…
mjameswh 8463e46
Address review comments
mjameswh 94a48d6
Merge branch 'main' into asynclocalstorage-auto-cleanup
mjameswh 78b42b2
Remove leftover console.log
mjameswh b72ae2f
Merge branch 'main' into asynclocalstorage-auto-cleanup
mjameswh db71341
Remove unused import
mjameswh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
10 changes: 4 additions & 6 deletions
10
packages/interceptors-opentelemetry/src/workflow/context-manager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { AsyncLocalStorage } from 'async_hooks'; | ||
| import * as workflow from '@temporalio/workflow'; | ||
| import { helpers, makeTestFunction } from './helpers-integration'; | ||
| import { unblockSignal } from './workflows/testenv-test-workflows'; | ||
|
|
||
| const test = makeTestFunction({ | ||
| workflowsPath: __filename, | ||
| workflowInterceptorModules: [require.resolve('./workflows/otel-interceptors')], | ||
| }); | ||
|
|
||
| export async function asyncLocalStorageWorkflow(explicitlyDisable: boolean): Promise<void> { | ||
| const myAls: AsyncLocalStorage<unknown> = new (globalThis as any).AsyncLocalStorage('My Workflow ALS'); | ||
| try { | ||
| await myAls.run({}, async () => { | ||
| let signalReceived = false; | ||
| workflow.setHandler(unblockSignal, () => { | ||
| signalReceived = true; | ||
| }); | ||
| await workflow.condition(() => signalReceived); | ||
| }); | ||
| } finally { | ||
| if (explicitlyDisable) { | ||
| myAls.disable(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("AsyncLocalStorage in workflow context doesn't throw when disabled", async (t) => { | ||
| const { createWorker, startWorkflow } = helpers(t); | ||
|
|
||
| const worker = await createWorker({ | ||
| // We disable the workflow cache to ensure that some workflow executions will get | ||
| // evicted from cache, forcing early disposal of the corresponding workflow vms. | ||
| maxCachedWorkflows: 0, | ||
| maxConcurrentWorkflowTaskExecutions: 1, | ||
|
|
||
| sinks: { | ||
| exporters: { | ||
| export: { | ||
| fn: () => void 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| await worker.runUntil(async () => { | ||
| const wfs = await Promise.all([ | ||
| startWorkflow(asyncLocalStorageWorkflow, { args: [true] }), | ||
| startWorkflow(asyncLocalStorageWorkflow, { args: [false] }), | ||
| startWorkflow(asyncLocalStorageWorkflow, { args: [true] }), | ||
| startWorkflow(asyncLocalStorageWorkflow, { args: [false] }), | ||
| ]); | ||
|
|
||
| await Promise.all([wfs[0].signal(unblockSignal), wfs[1].signal(unblockSignal)]); | ||
| await Promise.all([wfs[0].result(), wfs[1].result()]); | ||
| }); | ||
|
|
||
| // We're only asserting that no error is thrown. There's unfortunately no way | ||
| // to programmatically confirm that ALS instances were properly disposed. | ||
| t.pass(); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { condition } from '@temporalio/workflow'; | ||
|
|
||
| /** | ||
| * Workflow for reproducing the GH #1866 | ||
| * | ||
| * When the bug is present: | ||
| * 1. First condition times out | ||
| * 2. finally block calls CancellationScope.current().cancel() | ||
| * 3. With disabled storage, current() returns rootScope | ||
| * 4. rootScope.cancel() is called, failing the workflow with "Workflow cancelled" | ||
| * | ||
| * Actual failure happens on the second `condition` where when creating the new | ||
| * cancellation scope, we see that the parent/root scope is already canceled. | ||
| */ | ||
| export async function conditionWithTimeoutAfterDisposal(): Promise<string> { | ||
| const alwaysFalse = false; | ||
| await condition(() => alwaysFalse, '500ms'); | ||
| await condition(() => alwaysFalse, '500ms'); | ||
| return 'done'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any additional assertions that would be valuable here aside from "it doesn't throw"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I played around with the possibility of observing the destruction of ALS by escaping the sandbox to create a
FinalizationRegistry, which worked. The problem, though, is that I couldn't get that test to fail with the previous code, so that's not proving anything.With that in mind, and given the complexity and fragility of that test, I decided not to commit it.
And I really can't think of any other side effect to look for.