@@ -4,8 +4,7 @@ const { errorMonitor } = require('events')
4
4
const util = require ( 'util' )
5
5
6
6
const {
7
- addHook,
8
- AsyncResource
7
+ addHook
9
8
} = require ( './helpers/instrument' )
10
9
const shimmer = require ( '../../datadog-shimmer' )
11
10
const dc = require ( 'dc-polyfill' )
@@ -80,7 +79,8 @@ function createContextFromChildProcessInfo (childProcessInfo) {
80
79
const context = {
81
80
command : childProcessInfo . command ,
82
81
file : childProcessInfo . file ,
83
- shell : childProcessInfo . shell
82
+ shell : childProcessInfo . shell ,
83
+ abortController : new AbortController ( )
84
84
}
85
85
86
86
if ( childProcessInfo . fileArgs ) {
@@ -98,17 +98,12 @@ function wrapChildProcessSyncMethod (returnError, shell = false) {
98
98
}
99
99
100
100
const childProcessInfo = normalizeArgs ( arguments , shell )
101
+ const context = createContextFromChildProcessInfo ( childProcessInfo )
101
102
102
- const innerResource = new AsyncResource ( 'bound-anonymous-fn' )
103
- return innerResource . runInAsyncScope ( ( ) => {
104
- const context = createContextFromChildProcessInfo ( childProcessInfo )
105
- const abortController = new AbortController ( )
106
-
107
- childProcessChannel . start . publish ( { ...context , abortController } )
108
-
103
+ return childProcessChannel . start . runStores ( context , ( ) => {
109
104
try {
110
- if ( abortController . signal . aborted ) {
111
- const error = abortController . signal . reason || new Error ( 'Aborted' )
105
+ if ( context . abortController . signal . aborted ) {
106
+ const error = context . abortController . signal . reason || new Error ( 'Aborted' )
112
107
// expected behaviors on error are different
113
108
return returnError ( error , context )
114
109
}
@@ -141,21 +136,17 @@ function wrapChildProcessCustomPromisifyMethod (customPromisifyMethod, shell) {
141
136
const context = createContextFromChildProcessInfo ( childProcessInfo )
142
137
143
138
const { start, end, asyncStart, asyncEnd, error } = childProcessChannel
144
- const abortController = new AbortController ( )
145
-
146
- start . publish ( {
147
- ...context ,
148
- abortController
149
- } )
139
+ start . publish ( context )
150
140
151
141
let result
152
- if ( abortController . signal . aborted ) {
153
- result = Promise . reject ( abortController . signal . reason || new Error ( 'Aborted' ) )
142
+ if ( context . abortController . signal . aborted ) {
143
+ result = Promise . reject ( context . abortController . signal . reason || new Error ( 'Aborted' ) )
154
144
} else {
155
145
try {
156
146
result = customPromisifyMethod . apply ( this , arguments )
157
147
} catch ( error ) {
158
- error . publish ( { ...context , error } )
148
+ context . error = error
149
+ error . publish ( context )
159
150
throw error
160
151
} finally {
161
152
end . publish ( context )
@@ -192,26 +183,15 @@ function wrapChildProcessAsyncMethod (ChildProcess, shell = false) {
192
183
193
184
const childProcessInfo = normalizeArgs ( arguments , shell )
194
185
195
- const cb = arguments [ arguments . length - 1 ]
196
- if ( typeof cb === 'function' ) {
197
- const callbackResource = new AsyncResource ( 'bound-anonymous-fn' )
198
- arguments [ arguments . length - 1 ] = callbackResource . bind ( cb )
199
- }
200
-
201
- const innerResource = new AsyncResource ( 'bound-anonymous-fn' )
202
- return innerResource . runInAsyncScope ( ( ) => {
203
- const context = createContextFromChildProcessInfo ( childProcessInfo )
204
- const abortController = new AbortController ( )
205
-
206
- childProcessChannel . start . publish ( { ...context , abortController } )
207
-
186
+ const context = createContextFromChildProcessInfo ( childProcessInfo )
187
+ return childProcessChannel . start . runStores ( context , ( ) => {
208
188
let childProcess
209
- if ( abortController . signal . aborted ) {
189
+ if ( context . abortController . signal . aborted ) {
210
190
childProcess = new ChildProcess ( )
211
191
childProcess . on ( 'error' , ( ) => { } ) // Original method does not crash when non subscribers
212
192
213
193
process . nextTick ( ( ) => {
214
- const error = abortController . signal . reason || new Error ( 'Aborted' )
194
+ const error = context . abortController . signal . reason || new Error ( 'Aborted' )
215
195
childProcess . emit ( 'error' , error )
216
196
217
197
const cb = arguments [ arguments . length - 1 ]
@@ -230,17 +210,16 @@ function wrapChildProcessAsyncMethod (ChildProcess, shell = false) {
230
210
231
211
childProcess . on ( errorMonitor , ( e ) => {
232
212
errorExecuted = true
233
- childProcessChannel . error . publish ( e )
213
+ context . error = e
214
+ childProcessChannel . error . publish ( context )
234
215
} )
235
216
236
217
childProcess . on ( 'close' , ( code = 0 ) => {
237
218
if ( ! errorExecuted && code !== 0 ) {
238
- childProcessChannel . error . publish ( )
219
+ childProcessChannel . error . publish ( context )
239
220
}
240
- childProcessChannel . asyncEnd . publish ( {
241
- ...context ,
242
- result : code
243
- } )
221
+ context . result = code
222
+ childProcessChannel . asyncEnd . publish ( context )
244
223
} )
245
224
}
246
225
0 commit comments