Skip to content

[async_hooks] Wrong after callback in case of uncaughtException #22982

Closed
@Flarna

Description

@Flarna
  • Version: 8.12.0, 10.11.0, master
  • Platform: windows
  • Subsystem: async_hooks

async_hooks call after with wrong id if an uncaughtException happens which does not end the process.

const async_hooks = require("async_hooks");
const fs = require("fs");

function init(asyncId, type, triggerAsyncId, resource) {
  fs.writeSync(1, `${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${async_hooks.executionAsyncId()}\n`);
}

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

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

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

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

process.nextTick(() => {
//setTimeout(() => {
  fs.writeSync(1, "in callback\n");
  throw new Error("foo");
}, 100);

process.on("uncaughtException", (e) => fs.writeSync(1, "uncaughtException\n"));

Results in

TickObject(5): trigger: 1 execution: 1
before:  5
in callback
uncaughtException
Immediate(6): trigger: 5 execution: 5
after:   5
after:   1   <=== I think this is not correct
destroy: 5
before:  6
after:   6
destroy: 6

if I change the sample above to use setTimeout instead process.nextTick the issue is limited to master and the async_id emitted is 0 instead 1.

I tried to find the root cause and I thought it's in _fatalException which should emit after for all except the last ids and in case there are no hooks clear all except the last.
But if I check the working setTimeout case in NodeJs 8.12.0 the stack only holds the async_ids for Timeout and TIMERWRAP but not the 0/1 entries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions