Skip to content

Commit 0733daa

Browse files
committed
domain: fix vm promise tracking while keeping isolation
1 parent 3507b3f commit 0733daa

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/domain.js

Lines changed: 7 additions & 0 deletions
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
},
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const domain = require('domain');
5+
const vm = require('vm');
6+
7+
// A promise created in a VVM should not track back into the parent.
8+
//
9+
// See; https://github.com/nodejs/node/issues/40999
10+
11+
const context = vm.createContext({});
12+
13+
function run(code) {
14+
const d = domain.createDomain();
15+
d.run(common.mustCall(() => {
16+
vm.runInContext(code, context)()
17+
.then(common.mustCall(() => { }));
18+
}));
19+
}
20+
21+
for (let i = 0; i < 1000; i++) {
22+
run('async () => null');
23+
}

0 commit comments

Comments
 (0)