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.

(Warning) Turning a Promise Sync, async hoot stack has become corrupted

See original GitHub issue

When turning a Promise into a synchronous Promise Facing the following error: async hook stack has become corrupted

Error: async hook stack has become corrupted (actual: 6, expected: 7)
 1: v8::SnapshotCreator::`default constructor closure'
 2: node::CallbackScope::~CallbackScope
 3: node::CallbackScope::~CallbackScope
 4: RAND_query_egd_bytes
 5: RAND_query_egd_bytes
 6: uv_timer_get_repeat
 7: uv_run
 8: 00007FFB97441281
 9: 00007FFB974410B6
10: v8::internal::wasm::SignatureMap::Find
11: v8::internal::Builtins::CallableFor
12: v8::internal::Builtins::CallableFor
13: v8::internal::Builtins::CallableFor
14: 000001B9D57043C1

It occures when an error occured during the execution of the Promise. I can reproduce the error:

function waitPromise(val, time) {
  let result = undefined;
  new Promise(res => {
    setTimeout(() => {
      throw new Error('whatever');
      res(val);
    }, time);
  }).then(r => {
    result = r;
  });
  while(!result) {
    require('deasync').runLoopOnce();
  }
  return result;
}

console.log(waitPromise('my name is Bond, JAMES BOND!', 2000));

Make sure that in your Promise you do not have any error at all or the error will occur. Solve it by catching all error in the promise and returning the error as the result.

function waitPromise(val, time) {
  let result = undefined;
  new Promise((res, rej) => {
    setTimeout(() => {
      try {
        throw new Error('whatever');
        res(val);  
      } catch (error) {
        rej(error);
      }
    }, time);
  }).then(r => {
    result = r;
  }).catch(e => {
    result = e;
  });
  while(!result) {
    require('deasync').runLoopOnce();
  }
  return result;
}
console.log(waitPromise('my name is Bond, JAMES BOND!', 2000));

The same error occurs when using deasync-promise.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
folkvircommented, May 7, 2020

As I said:

Solve it by catching all errors in the promise and returning the error as the result.

Instead of rejecting, resolve the error with the object. And process the error outside of the deasync code.

0reactions
trusktrcommented, Feb 6, 2021

@NotWearingPants Neat. I didn’t know about callbackify. Then my example would become:

const util = require('util')
const deasync = require('deasync')
const promiseSync = deasync(util.callbackify(promise => promise))
const importSync = specifier => promiseSync(import(specifier))

// Look, no await needed here!
const something = importSync('./something.js').default;

module.exports = {
  ...something,
  // ...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

(Warning) Turning a Promise Sync, async hoot stack has become ...
When turning a Promise into a synchronous Promise Facing the following error: async hook stack has become corrupted. Error: async hook stack has...
Read more >
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 >
Async Await in Node.js - How to Master it? - Blog - RisingStack
Learn how to use async await in Node.js (async functions) to simplify your callback or Promise based application.
Read more >
Features - Bluebird JS
then to get at the promise's value as the callback is always called asynchronously. See the API on synchronous inspection for more information....
Read more >
In JavaScript, how is awaiting the result of an async different ...
I'm having a hard time wrapping my head around the use of async/await and regular sync function calls ...
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