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.

async hook stack has become corrupted

See original GitHub issue

Hello,

I met strange error after I added sentry to my project. When the error is thrown, the project crashes immediatly.

this:

 Error: async hook stack has become corrupted (actual: 286, expected: 0)

or this:

 Error: async hook stack has become corrupted (actual: 908, expected: 0)

sometimes this:

 node[1]: ../src/api/callback.cc:125:void node::InternalCallbackScope::Close(): Assertion `(env_->execution_async_id()) == (0)' failed.

I know it comes with sentry because I tried to remove Sentry.init and the error disappears. In my project, I’m using the async_hooks API and I fear a conflict with your tracing SDK. I am using async_hooks this way:

const { AsyncLocalStorage } = require('async_hooks');
const store = new AsyncLocalStorage();

// ...

app.use((req, res, next) => {
  store.enterWith(new Map());

  const instance = store.getStore();
  instance.set('request-id', uuid());

  return next();
});

I am declaring Sentry this way

Sentry.init({
    environment: process.env.NODE_ENV,
    dsn: '__',
    integrations: [
      // enable HTTP calls tracing
      new Sentry.Integrations.Http({ tracing: true }),
      // enable Express.js middleware tracing
      new Tracing.Integrations.Express({ app: router }),
    ],

    // Set tracesSampleRate to 1.0 to capture 100%
    // of transactions for performance monitoring.
    // We recommend adjusting this value in production
    tracesSampleRate: 1.0,
  });

  // RequestHandler creates a separate execution context using domains, so that every
  // transaction/span/breadcrumb is attached to its own Hub instance
  router.use(Sentry.Handlers.requestHandler());
  // TracingHandler creates a trace for every incoming request
  router.use(Sentry.Handlers.tracingHandler());

Do you know how this issue could be solved ?

Thank you for your work 😃

Version

Node: 16 (Using Docker image node:16-alpine)

Packages

@sentry/node: 6.4.1 @sentry/tracing: 6.4.1

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Hongbo-Miaocommented, May 27, 2021

Thanks @kamilogorek! I am aware of I don’t have OpenTelemetry server in this demo repo. I am using this as a demo to reproduce the bug. Yeah just confirmed it only started from 16.2.0. Opened a ticket on Node.js side at https://github.com/nodejs/node/issues/38814.

1reaction
kamilogorekcommented, May 27, 2021

This is some kind of regression in Node v16.2.0. It’s working fine in v16.1.0.

The problem however, is incorrect request rejections handling between domains and async_hooks. Even simplest domain.create().run(() => next()) as an express middleware, when used with async_hooks (which are used in opentelemetry for context management - https://github.com/open-telemetry/opentelemetry-js/blob/dfb77372d311587c3462a43497c2ec4b3867e9fa/packages/opentelemetry-node/src/NodeTracerProvider.ts#L61-L63)` will throw an error. For some strange reason, when used together, they cannot talk with each other correctly.

It’s most likely that error thrown within async_hooks context is not correctly propagated to the domain, and it crashes the process instead of being handled there. Might be related:

https://github.com/nodejs/node/issues/38503 https://github.com/nodejs/node/issues/38145

One additional information here though, is that what’s causing an actual error that is crashing the process, is TCP not being able to connect to the OpenTelemetry server, which you can see when using Node v16.1.0

{"stack":"Error: connect ECONNREFUSED 127.0.0.1:55681\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16)\n    at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:131:17)","message":"connect ECONNREFUSED 127.0.0.1:55681","errno":"-61","code":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":"55681","name":"Error"}

55681 is default port for OpenTelemetry, and because you are using CollectorTraceExporter, and not providing url override, it’ll try to send request to localhost:55681, which will reject, and effectively fail and crash the process.

There’s nothing that we can do from our side right now, as it’s infrastructure specific issue, not the SDK itself. We need to wait for Node to state clearly if it’s going to be fixed, and if not, then update docs appropriately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

async hook stack has become corrupted - node.js
Your code is taking too long to elaborate, which means that multiple requests are filling the async queue until it can't handle it....
Read more >
Error: async hook stack has become corrupted (actual: 73 ...
Expecting the app not crash. What do you see instead? Crash with error: Error: async hook stack has become corrupted (actual: 73, expected ......
Read more >
"async hook stack has become corrupted" after upgrading ...
The error in the logs is “Error: async hook stack has become corrupted (actual: 9558, expected: 9556)” with no further information or stack ......
Read more >
Error: async hook stack has become corrupted in node js ...
Basically there was timeout of 250ms which was causing the issue in async hooks lifecycle. I changed from timeout.timeout('waitTimer', 250, ...
Read more >
Node.js API :Error: async hook stack has become corrupted ...
Node.js API :Error: async hook stack has become corrupted (actual: 9, expected: 10) ... Can you please suggest some solution. Please note :...
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