Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bcd40d

Browse filesBrowse files
Qarddanielleadams
authored andcommitted
domain: fix vm promise tracking while keeping isolation
PR-URL: #43556 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent e4aa50f commit 6bcd40d
Copy full SHA for 6bcd40d

File tree

Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed
Open diff view settings
Collapse file

‎lib/domain.js‎

Copy file name to clipboardExpand all lines: lib/domain.js
+7Lines 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
},
Collapse file
+28Lines changed: 28 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.