Skip to content

Commit

Permalink
fix: abort signal when request stream closed (#82)
Browse files Browse the repository at this point in the history
* chore: make until-death script wait longer for gateway

* fix: abort signal when request stream is closed
  • Loading branch information
SgtPooki authored Mar 28, 2024
1 parent 4e332bd commit 0701b3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion debugging/until-death.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ start_gateway() {
start_gateway & process_pid=$!

ensure_gateway_running() {
npx wait-on "tcp:$PORT" -t 1000 || exit 1
npx wait-on "tcp:$PORT" -t 5000 || exit 1
}


Expand Down
25 changes: 20 additions & 5 deletions src/helia-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,27 @@ export class HeliaServer {

const opController = new AbortController()
setMaxListeners(Infinity, opController.signal)
request.raw.on('close', () => {
if (request.raw.aborted) {
this.log('request aborted by client')
opController.abort()
const cleanupFn = (): void => {
if (request.raw.readableAborted) {
this.log.trace('request aborted by client for url "%s"', url)
} else if (request.raw.destroyed) {
this.log.trace('request destroyed for url "%s"', url)
} else if (request.raw.complete) {
this.log.trace('request closed or ended in completed state for url "%s"', url)
} else {
this.log.trace('request closed or ended gracefully for url "%s"', url)
}
})
// we want to stop all further processing because the request is closed
opController.abort()
}
/**
* The 'close' event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. The event indicates that no more events will be emitted, and no further computation will occur.
* A Readable stream will always emit the 'close' event if it is created with the emitClose option.
*
* @see https://nodejs.org/api/stream.html#event-close_1
*/
request.raw.on('close', cleanupFn)

await this.isReady
const resp = await this.heliaFetch(url, { signal: opController.signal, redirect: 'manual' })
await this.#convertVerifiedFetchResponseToFastifyReply(resp, reply)
Expand Down

0 comments on commit 0701b3c

Please sign in to comment.