Skip to content

Commit 6bcd40d

Browse files
Qarddanielleadams
authored andcommitted
domain: fix vm promise tracking while keeping isolation
PR-URL: #43556 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent e4aa50f commit 6bcd40d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/domain.js

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
Promise,
4040
ReflectApply,
4141
SafeMap,
42+
SafeWeakMap,
4243
Symbol,
4344
} = primordials;
4445

@@ -69,6 +70,7 @@ ObjectDefineProperty(process, 'domain', {
6970
}
7071
});
7172

73+
const vmPromises = new SafeWeakMap();
7274
const pairing = new SafeMap();
7375
const asyncHook = createHook({
7476
init(asyncId, type, triggerAsyncId, resource) {
@@ -85,6 +87,11 @@ const asyncHook = createHook({
8587
value: process.domain,
8688
writable: true
8789
});
90+
// Because promises from other contexts don't get a domain field,
91+
// the domain needs to be held alive another way. Stuffing it in a
92+
// weakmap connected to the promise lifetime can fix that.
93+
} else {
94+
vmPromises.set(resource, process.domain);
8895
}
8996
}
9097
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const vm = require('vm');
7+
8+
// A promise created in a VM should not include a domain field but
9+
// domains should still be able to propagate through them.
10+
//
11+
// See; https://github.com/nodejs/node/issues/40999
12+
13+
const context = vm.createContext({});
14+
15+
function run(code) {
16+
const d = domain.createDomain();
17+
d.run(common.mustCall(() => {
18+
const p = vm.runInContext(code, context)();
19+
assert.strictEqual(p.domain, undefined);
20+
p.then(common.mustCall(() => {
21+
assert.strictEqual(process.domain, d);
22+
}));
23+
}));
24+
}
25+
26+
for (let i = 0; i < 1000; i++) {
27+
run('async () => null');
28+
}

0 commit comments

Comments
 (0)