Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 formattingStore = 'not called';
let downstreamStore = 'not called';

const thrown = {
[Symbol.toPrimitive]: common.mustCall(() => {
// Exception formatting re-enters JavaScript while the throwing
// microtask's context frame is still current, so the context is
// available here.
formattingStore = asyncLocalStorage.getStore();
// A microtask queued while the frame is still current captures that
// context.
queueMicrotask(common.mustCall(() => {
downstreamStore = asyncLocalStorage.getStore();
assert.strictEqual(downstreamStore, sensitive);
}));
return 'thrown';
}),
};

process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err, thrown);
// The AsyncLocalStorage context of the throwing microtask is still
// available to the uncaughtException handler.
assert.strictEqual(asyncLocalStorage.getStore(), sensitive);
}));

asyncLocalStorage.run(sensitive, () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test illustrates a secretive AsyncLocalStorage store. But to actually expose an AsyncLocalStorage value, it has to be exported programmatically, i.e. either export the AsyncLocalStorage instance, or export a value getter. So I don't find this context being available at uncaughtException problematic.

queueMicrotask(() => {
throw thrown;
});
});

setImmediate(common.mustCall(() => {
// Once the microtask queue has drained, the context frame is no longer
// current.
assert.strictEqual(formattingStore, sensitive);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}));
Loading