Skip to content

Questions: async_hooks destroy callback vs setImmediate #13262

Description

@JiaLiPassion
  • Version:
    v8.0.0-nightly20170527f84666f923

  • Platform:
    Mac OS

  • Subsystem:

I just tried the new async_hooks callback API, I want to confirm the behaviors below is correct or not?

const async_hooks = require('async_hooks');

function init(id, provider, parentId, parentHandle) {
    process._rawDebug('init ', id, provider);
}

function before(id) { 
    process._rawDebug('before', id);
 }
function after(id) { 
    process._rawDebug('after', id);
 }
function destroy(id) {
    process._rawDebug('destroy', id);
}

async_hooks.createHook({init, before, after, destroy}).enable();

const timerId1 = setImmediate(() => {
    process._rawDebug('setImmediate');
});
clearImmediate(timerId1);

It is just a simple setImmediate and it was clear by clearImmediate.
I expect the result will be

 init  2 Immediate
destroy 2

but the result is

 init  2 Immediate

only.

If I add some other setTimeout logic in the test program, the destroy will be called.

const async_hooks = require('async_hooks');

function init(id, provider, parentId, parentHandle) {
    process._rawDebug('init ', id, provider, parentId, parentHandle);
}

function before(id) { 
    process._rawDebug('before', id);
 }
function after(id) { 
    process._rawDebug('after', id);
 }
function destroy(id) {
    process._rawDebug('destroy', id);
}

async_hooks.createHook({init, before, after, destroy}).enable();

const timerId1 = setImmediate(() => {
    process._rawDebug('setImmediate');
});
clearImmediate(timerId1);

const timerId = setTimeout(() => {
    process._rawDebug('settimeout');
}, 100);
clearTimeout(timerId);

the result will look like

init  2 Immediate
init  3 Timeout
init  4 TIMERWRAP
destroy 2
destroy 3

Is this the correct behaviors? And how to ensure the destroy callback is called ?

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.confirmed-bugIssues and PRs for confirmed bugs.timersIssues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions