question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoid using destroy hook on async-hooks

See original GitHub issue

There is a new development on V8/Nodejs sides (Node16 and will be backported to Node14) that should optimize async-hooks, but from another side, if destroy hook is set, it will cause performance degradation

I’m not sure it is possible to avoid the use of destroy here but better keep it in mind

https://github.com/nodejs/node/pull/36394 https://github.com/nodejs/node/pull/38577

Before:

❯ ./node-base benchmark/async_hooks/promises.js
async_hooks/promises.js asyncHooks="enabled" n=1000000: 255,804.136249129
async_hooks/promises.js asyncHooks="enabledWithDestroy" n=1000000: 161,508.90344245007
async_hooks/promises.js asyncHooks="enabledWithInitOnly" n=1000000: 304,650.28581800853
async_hooks/promises.js asyncHooks="disabled" n=1000000: 1,558,729.7299161875

After:

❯ ./node benchmark/async_hooks/promises.js
async_hooks/promises.js asyncHooks="enabled" n=1000000: 1,059,976.9918850197
async_hooks/promises.js asyncHooks="enabledWithDestroy" n=1000000: 155,428.08296446863
async_hooks/promises.js asyncHooks="enabledWithInitOnly" n=1000000: 972,299.3745827201
async_hooks/promises.js asyncHooks="disabled" n=1000000: 1,548,472.043353055

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mcollinacommented, May 20, 2021

Another thing I’d like to look at is leveraging the executionAsyncResrouce approach at some point to see if we can use fewer hooks.

executionAsyncResource has been designed for this exact need. The destroy hook is a known performance issue and it should not be used as it creates an extremely heavy toll on the GC and Node.js in general (it’s a call from C++ to JS when every promise or native resource is collected).

FWIW, you can also take a look at https://nodejs.org/api/async_hooks.html#async_hooks_class_asynclocalstorage. It’s going to stabilize before the rest of the async_hooks machinery.

All the above was part of a long-term plan to improve the performance of Node.js when APMs are used. Take a read at https://docs.google.com/document/d/1g8OrG5lMIUhRn1zbkutgY83MiTSMx-0NHDs8Bf-nXxM/edit to get the gist of the plan. Some things are a bit outdated, but it’s still going.

1reaction
michaelgoincommented, May 20, 2021

Thanks! That is definitely why I want to look into it. I have previously done a quick experiment where some supported promise cases broke, so I think we are gonna have to dig through where our existing (and old) design and assumptions need to be reworked and if there are any limitations for what we are trying to support that just aren’t possible by using the new mechanisms. I definitely expect we’d benefit greatly, though.

I appreciate sharing the plan doc, we’ll definitely take a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

async hooks promise not destroyed · Issue #14446 - GitHub
i want to implement a 'long stack trace' using async hooks like this: store stack in a map when async resource's initiated.
Read more >
node async_hooks destroy lifecycle event is not called for all ...
I'm finding that the destroy callback isn't always called for each corresponding init callbacks. Here's a simple repro: const asyncHooks = ...
Read more >
Async hooks | Node.js v19.3.0 Documentation
If the resource does not depend on garbage collection, then this will not be an issue. Using the destroy hook results in additional...
Read more >
Understanding Async Resources with Async Hooks
You might notice that the destroy hook was not called at all because the resource was not garbage collected even if the garbage...
Read more >
Using Async Hooks for Request Context Handling in Node.js
Let's use Async Hooks to track HTTP requests. ... destroy - Called after the async resource is destroyed; promiseResolve - Called when the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found