Node: Missing destroy event when using Promise

Created on 20 Apr 2020  路  2Comments  路  Source: nodejs/node

  • node: v8.16.0:
  • Platform: Ubuntu 18.04.1 LTS 2019, x64
  • Module: async_hooks:

The destroy event is never raised when resolving a Promise. Possible memory leak ?

const fs = require('fs');
const ah = require('async_hooks');

let hook = ah.createHook({
    init,
    destroy
});
hook.enable();
hook = null;

function init(asyncId) {
    fs.writeSync(1, `init ${asyncId}\n`);
}

function destroy(asyncId) {
    fs.writeSync(1, `destroy ${asyncId}\n`);
}

function run(cb) {
    return Promise.resolve(1)
        .then(() => {
            return cb();
        });
}

run(cb);

function cb() {
    setTimeout(onTimeout, 300);
}


function onTimeout() {
    console.log('onTimeout');
}

Output:

init 5
init 6
init 7
init 8
onTimeout
init 9
destroy 7
destroy 9
destroy 8

Expected output:

init 5
init 6
init 7
init 8
onTimeout
init 9
destroy 7
destroy 9
destroy 8
destroy 6
destroy 5
promises question

Most helpful comment

This is mostly the same as #32939.
If you add global.gc() in onTimeout the Promise is collected and destroy is emitted.
Triggering gc in the sync flow wont work here as the Promises are async and there are still references to it after the sync part is finished.

Promises are quite lightweight objects therefore quite a lot of them may be in memory until they get collected.

All 2 comments

This is mostly the same as #32939.
If you add global.gc() in onTimeout the Promise is collected and destroy is emitted.
Triggering gc in the sync flow wont work here as the Promises are async and there are still references to it after the sync part is finished.

Promises are quite lightweight objects therefore quite a lot of them may be in memory until they get collected.

Closing as answered.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akdor1154 picture akdor1154  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

Icemic picture Icemic  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments