@@ -36,6 +36,22 @@ test('client-side abort below error threshold - call end', { timeout: 10000 }, f
36
36
37
37
var server = http . createServer ( function ( req , res ) {
38
38
setTimeout ( function ( ) {
39
+ // Explicitly respond with headers before aborting the client request,
40
+ // because:
41
+ // (a) `assert(t, data)` above asserts that `trans.result` has been set
42
+ // to "HTTP 2xx", which depends on the wrapped `writeHead` having been
43
+ // called, and
44
+ // (b) calling res.write('...') or res.end('...') *after* a clientReq.abort()
45
+ // in node >=15 leads to a race on whether `ServerResponse.writeHead()`
46
+ // is called.
47
+ //
48
+ // The race:
49
+ // - clientReq.abort() closes the client-side of the socket
50
+ // - The server-side of the socket closes (`onClose` in lib/_http_agent.js)
51
+ // - (race) If the server-side socket is closed before `res.write` is
52
+ // called, then res.writeHead() will not be called as of this change:
53
+ // https://github.com/nodejs/node/pull/31818/files#diff-48d21edbddb6e855d1ee5716c49bcdc0d913c11ee8a24a98ea7dbc60cd253556L661-R706
54
+ res . writeHead ( 200 )
39
55
clientReq . abort ( )
40
56
res . write ( 'sync write' )
41
57
process . nextTick ( function ( ) {
@@ -80,9 +96,10 @@ test('client-side abort above error threshold - call end', function (t) {
80
96
81
97
var server = http . createServer ( function ( req , res ) {
82
98
setTimeout ( function ( ) {
99
+ res . writeHead ( 200 ) // See race comment above.
83
100
clientReq . abort ( )
84
101
setTimeout ( function ( ) {
85
- res . write ( 'Hello' ) // server emits clientError if written in same tick as abort
102
+ res . write ( 'Hello' )
86
103
setTimeout ( function ( ) {
87
104
res . end ( ' World' )
88
105
} , 10 )
@@ -197,6 +214,7 @@ test('server-side abort below error threshold and socket closed - call end', fun
197
214
}
198
215
199
216
var server = http . createServer ( function ( req , res ) {
217
+ res . writeHead ( 200 ) // See race comment above.
200
218
setTimeout ( function ( ) {
201
219
t . ok ( timedout , 'should have closed socket' )
202
220
t . notOk ( ended , 'should not have ended transaction' )
@@ -240,6 +258,7 @@ test('server-side abort above error threshold and socket closed - call end', fun
240
258
}
241
259
242
260
var server = http . createServer ( function ( req , res ) {
261
+ res . writeHead ( 200 ) // See race comment above.
243
262
setTimeout ( function ( ) {
244
263
t . ok ( timedout , 'should have closed socket' )
245
264
t . notOk ( ended , 'should not have ended transaction' )
0 commit comments