Repository navigation
fix(listener): cancel the response body when the client has already disconnected - #412
Open
RaphaelFakhri wants to merge 1 commit into
Open
RaphaelFakhri wants to merge 1 commit into
RaphaelFakhri wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #411
If the client disconnects while an async handler is still running, the
Responsebody that the handler returns afterwards is never cancelled. The stream'scancel()never runs, so the producer behind the body (an upstreamfetch, an SSE generator, a database cursor) stays open until the process exits.writeFromReadableStreamDefaultReadercancels the reader from thecloseanderrorlisteners it attaches to the outgoing response. When the response is already destroyed,closehas already fired and does not fire again, so the reader is never cancelled. The same happens for a disconnect inside thesetTimeouttick inresponseViaResponseObject, before the listeners are attached.writeFromReadableStreamreturns early for a destroyed writable, but it also leaves the stream uncancelled.This change:
reader.closedinwriteFromReadableStreamDefaultReaderwhen the writable is already destroyed.writeFromReadableStream, which covers the cached-response path.Tests
test/listener.test.ts: a handler waits for the request signal to abort, then returns a streaming body. The test asserts that the body'scancel()runs. This is the reproduction from the issue.test/utils.test.ts: one case each forwriteFromReadableStreamandwriteFromReadableStreamDefaultReaderwith a writable that is already closed.mainand pass with this change.format,lintandtypecheckare clean.utils.test.tsandlistener.test.tspass on Node 20, 22 and 24.serve-static.test.ts > Should handle read permission errors gracefullyalso fails onmainon that machine, because the tests run as root andchmoddoes not stop root from reading the file.