Skip to content

Comments

fix(node): preserve AsyncLocalStorage context in unhandledRejection handlers#32264

Open
bartlomieju wants to merge 1 commit intodenoland:mainfrom
bartlomieju:fix/async-local-storage-unhandled-rejection
Open

fix(node): preserve AsyncLocalStorage context in unhandledRejection handlers#32264
bartlomieju wants to merge 1 commit intodenoland:mainfrom
bartlomieju:fix/async-local-storage-unhandled-rejection

Conversation

@bartlomieju
Copy link
Member

Summary

  • Fixes AsyncLocalStorage.getStore() returning undefined inside unhandledRejection / unhandledrejection event handlers
  • Now matches Node.js behavior where the async context active at the time of rejection is preserved

Problem

import { AsyncLocalStorage } from 'node:async_hooks';
import process from 'node:process';

const store = new AsyncLocalStorage();

process.on('unhandledRejection', () => {
    console.log(store.getStore()); // Node: "data", Deno (before): undefined
});

await store.run("data", async () => {
    new Promise((_, reject) => setTimeout(() => reject(new Error('test')), 50));
});

Fix

The core fix is in deno_core (denoland/deno_core#1308):

  1. Captures the async context (CPED) at promise rejection time in PromiseRejectWithNoHandler
  2. Passes it alongside the promise/reason in the rejections array
  3. Restores it before invoking the unhandled rejection handler in eventLoopTick

This PR adds the spec test. Once the deno_core PR is merged and the dependency is bumped, the test will pass.

Test plan

  • Spec test node::async_local_storage_unhandled_rejection verifies the fix
  • Manually verified with the reproduction script from the issue

Fixes #30135
Depends on denoland/deno_core#1308

🤖 Generated with Claude Code

Adds a spec test verifying that AsyncLocalStorage context is preserved
when unhandledRejection handlers are invoked, matching Node.js behavior.

Refs denoland#30135
Depends on denoland/deno_core#1308

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsyncLocalStorage not preserved in unhandledRejection (unlike Node)

1 participant