Skip to content

Commit 8c2e73c

Browse files
committed
fix: do not abort when stream payload consumed
1 parent 0ecd502 commit 8c2e73c

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ module.exports = fp(
9393
if (raw.socket.destroyed) {
9494
throw new Errors.SOCKET_CLOSED(reqId)
9595
} else {
96-
raw.once(
96+
raw.socket.once(
9797
'close',
9898
function () {
9999
if (controllers.has(this)) {

test/index.test.js

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
'use strict'
2+
const { pipeline } = require('node:stream/promises')
3+
const { Readable, PassThrough } = require('node:stream')
4+
25
const { promisify } = require('util')
36

47
const tap = require('tap')
@@ -86,7 +89,7 @@ tap.test('fastify-racing#decoration', subtest => {
8689
})
8790

8891
tap.test('fastify-racing#promise', { only: true }, subtest => {
89-
subtest.plan(4)
92+
subtest.plan(6)
9093

9194
subtest.test('Should handle a request aborted', t => {
9295
t.plan(3)
@@ -124,13 +127,111 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
124127
t.ok(err)
125128
}
126129
)
127-
128130
// Allow a full event loop cycle
129131
await sleep(5)
130132
abtCtlr.abort()
131133
})
132134
})
133135

136+
subtest.test('Should not incorrectly abort request when body stream consumed', t => {
137+
t.plan(4)
138+
139+
const app = fastify()
140+
app.register(plugin)
141+
app.addContentTypeParser('application/octet-stream', {}, (_req, payload, done) => done(null, payload))
142+
143+
t.teardown(() => app.close())
144+
145+
app.post('/', async (req, _reply) => {
146+
const signal = req.race()
147+
148+
// consume stream
149+
await pipeline(req.body, new PassThrough())
150+
await sleep(5) // Allow a full event loop cycle
151+
t.equal(signal.aborted, false)
152+
153+
const result = await Promise.race([signal, dummy(signal)])
154+
t.equal(typeof result, 'string')
155+
156+
if (result.type === 'aborted') return ''
157+
else return `${result}-world`
158+
})
159+
160+
app
161+
.ready()
162+
.then(() => app.listen({ port: 0 }))
163+
.then(async () => {
164+
request(
165+
`http://localhost:${app.server.address().port}`,
166+
{
167+
method: 'POST',
168+
path: '/',
169+
signal: undefined,
170+
body: Readable.from('stream data'),
171+
headers: {
172+
'Content-Type': 'application/octet-stream'
173+
}
174+
},
175+
(err, res) => {
176+
t.error(err)
177+
t.equal(res.statusCode, 200)
178+
}
179+
)
180+
})
181+
})
182+
183+
subtest.test('Should handle a stream request aborted', t => {
184+
t.plan(5)
185+
186+
const app = fastify()
187+
const abtCtlr = new AbortController()
188+
app.register(plugin)
189+
app.addContentTypeParser('application/octet-stream', {}, (_req, payload, done) => done(null, payload))
190+
191+
t.teardown(() => app.close())
192+
193+
app.post('/', async (req, _reply) => {
194+
const signal = req.race()
195+
196+
// consume stream
197+
await pipeline(req.body, new PassThrough())
198+
await sleep(5) // Allow a full event loop cycle
199+
t.equal(signal.aborted, false)
200+
201+
const result = await Promise.race([signal, dummy(signal)])
202+
t.equal(signal.aborted, true)
203+
t.equal(typeof result, 'object')
204+
t.equal(result.type, 'abort')
205+
206+
if (result.type === 'aborted') return ''
207+
else return `${result}-world`
208+
})
209+
210+
app
211+
.ready()
212+
.then(() => app.listen({ port: 0 }))
213+
.then(async () => {
214+
request(
215+
`http://localhost:${app.server.address().port}`,
216+
{
217+
method: 'POST',
218+
path: '/',
219+
signal: abtCtlr.signal,
220+
body: Readable.from('stream data'),
221+
headers: {
222+
'Content-Type': 'application/octet-stream'
223+
}
224+
},
225+
err => {
226+
t.ok(err)
227+
}
228+
)
229+
// Allow multiple event loop cycles
230+
await sleep(20)
231+
abtCtlr.abort()
232+
})
233+
})
234+
134235
subtest.test(
135236
'Should be able to handle more than one race check within a request',
136237
t => {

0 commit comments

Comments
 (0)