Skip to content

Conversation

@mcollina
Copy link
Member

Summary

Add a new --allow-async-hooks flag that must be passed to enable async_hooks.createHook(). This is a breaking change - createHook is now disabled by default.

Changes

  • Added --allow-async-hooks CLI flag (disabled by default)
  • async_hooks.createHook() now throws ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED when called without the flag
  • Updated all existing tests that use createHook() to include the flag
  • Added new tests for both enabled and disabled behaviors

Breaking Change

async_hooks.createHook() now requires the --allow-async-hooks flag to be passed when starting Node.js.

Most APM tools should now use AsyncLocalStorage instead, which is not affected by this change.

Migration

Add --allow-async-hooks to your Node.js command line or NODE_OPTIONS:

node --allow-async-hooks app.js
# or
NODE_OPTIONS="--allow-async-hooks" node app.js

Add a new `--allow-async-hooks` flag that must be passed to enable
`async_hooks.createHook()`. This is a breaking change - createHook is
now disabled by default.

When createHook() is called without the flag, it throws
ERR_ASYNC_HOOKS_CREATE_HOOK_DISABLED with a message indicating that
the flag is required.

This change affects all code using async_hooks.createHook(), including
APM tools and debugging utilities. Users must now explicitly opt-in
to this functionality.

BREAKING CHANGE: async_hooks.createHook() now requires --allow-async-hooks
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/config

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Dec 12, 2025
@mcollina mcollina added the semver-major PRs that contain breaking changes and should be released in the next major version. label Dec 12, 2025
@mcollina
Copy link
Member Author

Opening up to discuss this. I think this is the best path forward as they are extremely problematic to execute without somebody knowing what they are doing. I'm not sure if we would eventually remove themm, but this is the next step.

Given that most APMs are actually using AsyncLocalStorage now, this is viable.

@mcollina mcollina requested a review from RafaelGSS December 12, 2025 12:09
@targos targos added the needs-citgm PRs that need a CITGM CI run. label Dec 12, 2025
@marco-ippolito
Copy link
Member

I agree this is a problematic api, we should get rid of it in the future, so +1

@jasnell
Copy link
Member

jasnell commented Dec 12, 2025

I'm also +1 on this.

}
```

### `--allow-async-hooks`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be --enable-async-hooks, right? (and elsewhere in this PR)

@Flarna
Copy link
Member

Flarna commented Dec 13, 2025

I think we should either get rid of internal use in domain first (see here) or at least document there that use of domain requires this command line option set.

@Flarna
Copy link
Member

Flarna commented Dec 14, 2025

I think there is one more internal use: if --no-async-context-frame is given AsyncLocalStore uses async hooks.

I think this case should be handled to adapt --no-async-context-frame to implicit set --allow-async-hooks and add a line in the docs to clarify that.

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 on this, but I think we should also have a runtime API to enable this. Most APM tools offer the possibility of requiring their module to instrument the user's application. Enabling it via a CLI will cause them to create a wrapper for the user's application or make it a requirement in the user's configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. needs-citgm PRs that need a CITGM CI run. semver-major PRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants