Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

domain: fix vm promise tracking while keeping isolation #43556

Merged
merged 1 commit into from
Jul 14, 2022
Merged
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
7 changes: 7 additions & 0 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const {
Promise,
ReflectApply,
SafeMap,
SafeWeakMap,
Symbol,
} = primordials;

Expand Down Expand Up @@ -69,6 +70,7 @@ ObjectDefineProperty(process, 'domain', {
}
});

const vmPromises = new SafeWeakMap();
const pairing = new SafeMap();
const asyncHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
Expand All @@ -85,6 +87,11 @@ const asyncHook = createHook({
value: process.domain,
writable: true
});
// Because promises from other contexts don't get a domain field,
// the domain needs to be held alive another way. Stuffing it in a
// weakmap connected to the promise lifetime can fix that.
} else {
vmPromises.set(resource, process.domain);
Qard marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-domain-vm-promise-isolation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const domain = require('domain');
const vm = require('vm');

// A promise created in a VM should not include a domain field but
// domains should still be able to propagate through them.
//
// See; https://github.com/nodejs/node/issues/40999

const context = vm.createContext({});

function run(code) {
const d = domain.createDomain();
d.run(common.mustCall(() => {
const p = vm.runInContext(code, context)();
assert.strictEqual(p.domain, undefined);
p.then(common.mustCall(() => {
assert.strictEqual(process.domain, d);
}));
}));
}

for (let i = 0; i < 1000; i++) {
run('async () => null');
}