Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,24 @@ function nextTick(callback) {
}

function runMicrotask() {
this.runInAsyncScope(() => {
const callback = this.callback;
try {
callback();
} finally {
this.emitDestroy();
}
});
try {
this.runInAsyncScope(() => {
const callback = this.callback;
try {
callback();
} finally {
this.emitDestroy();
}
});
} catch (error) {
// V8 restores the continuation-preserved embedder data for each
// microtask, but currently does not clear it on exception paths before
// reporting the exception. Clear it here so user code re-entered during
// exception formatting cannot observe this microtask's AsyncLocalStorage
// context.
AsyncContextFrame.set(undefined);
throw error;
}
}

const defaultMicrotaskResourceOpts = { requireManualDestroy: true };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Flags: --async-context-frame
'use strict';

const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();
const sensitive = { secret: 'sensitive' };
let toPrimitiveStore = 'not called';
let downstreamStore = 'not called';

const thrown = {
[Symbol.toPrimitive]: common.mustCall(() => {
toPrimitiveStore = asyncLocalStorage.getStore();
queueMicrotask(common.mustCall(() => {
downstreamStore = asyncLocalStorage.getStore();
assert.strictEqual(downstreamStore, undefined);
}));
return 'thrown';
}),
};

process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err, thrown);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}));

asyncLocalStorage.run(sensitive, () => {
queueMicrotask(() => {
throw thrown;
});
});

setImmediate(common.mustCall(() => {
assert.strictEqual(toPrimitiveStore, undefined);
assert.strictEqual(downstreamStore, undefined);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}));
Loading