@@ -70,15 +70,18 @@ class RequestHandler extends AsyncResource {
70
70
this . reason = this . signal . reason ?? new RequestAbortedError ( )
71
71
} else {
72
72
this . removeAbortListener = util . addAbortListener ( this . signal , ( ) => {
73
- this . removeAbortListener ?. ( )
74
- this . removeAbortListener = null
75
-
76
73
this . reason = this . signal . reason ?? new RequestAbortedError ( )
77
74
if ( this . res ) {
78
75
util . destroy ( this . res , this . reason )
79
76
} else if ( this . abort ) {
80
77
this . abort ( this . reason )
81
78
}
79
+
80
+ if ( this . removeAbortListener ) {
81
+ this . res ?. off ( 'close' , this . removeAbortListener )
82
+ this . removeAbortListener ( )
83
+ this . removeAbortListener = null
84
+ }
82
85
} )
83
86
}
84
87
}
@@ -114,10 +117,7 @@ class RequestHandler extends AsyncResource {
114
117
const body = new Readable ( { resume, abort, contentType, contentLength, highWaterMark } )
115
118
116
119
if ( this . removeAbortListener ) {
117
- // TODO (fix): 'close' is sufficient but breaks tests.
118
- body
119
- . on ( 'end' , this . removeAbortListener )
120
- . on ( 'error' , this . removeAbortListener )
120
+ body . on ( 'close' , this . removeAbortListener )
121
121
}
122
122
123
123
this . callback = null
@@ -156,9 +156,6 @@ class RequestHandler extends AsyncResource {
156
156
onError ( err ) {
157
157
const { res, callback, body, opaque } = this
158
158
159
- this . removeAbortListener ?. ( )
160
- this . removeAbortListener = null
161
-
162
159
if ( callback ) {
163
160
// TODO: Does this need queueMicrotask?
164
161
this . callback = null
@@ -179,6 +176,12 @@ class RequestHandler extends AsyncResource {
179
176
this . body = null
180
177
util . destroy ( body , err )
181
178
}
179
+
180
+ if ( this . removeAbortListener ) {
181
+ res ?. off ( 'close' , this . removeAbortListener )
182
+ this . removeAbortListener ( )
183
+ this . removeAbortListener = null
184
+ }
182
185
}
183
186
}
184
187
0 commit comments