File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Open diff view settings
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Open diff view settings
Original file line number Diff line number Diff 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 ( ) ;
7274const pairing = new SafeMap ( ) ;
7375const 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments