fix: handle client disconnection without canceling stream#258
Merged
Conversation
Member
Author
|
Hi @usualoma Sorry for pinging again. Can you review this? |
Member
|
Hi @yusukebe When diff --git i/src/utils.ts w/src/utils.ts
index c6839bc..a54a707 100644
--- i/src/utils.ts
+++ w/src/utils.ts
@@ -9,46 +9,30 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
}
const reader = stream.getReader()
- let clientDisconnected = false
-
- const handleClientDisconnect = () => {
- clientDisconnected = true
- }
-
const handleError = () => {
- clientDisconnected = true
+ // add an error handler to prevent abnormal termination, but simply ignore the error
}
- writable.on('close', handleClientDisconnect)
writable.on('error', handleError)
reader.read().then(flow, handleStreamError)
return reader.closed.finally(() => {
- writable.off('close', handleClientDisconnect)
writable.off('error', handleError)
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleStreamError(error: any) {
- if (!clientDisconnected) {
- if (error) {
- writable.destroy(error)
- }
+ if (error) {
+ writable.destroy(error)
}
}
function onDrain() {
- if (!clientDisconnected) {
- reader.read().then(flow, handleStreamError)
- }
+ reader.read().then(flow, handleStreamError)
}
function flow({ done, value }: ReadableStreamReadResult<Uint8Array>): void | Promise<void> {
- if (clientDisconnected) {
- return
- }
-
try {
if (done) {
writable.end() |
Member
|
Alternatively, if you want to ensure that diff --git i/src/utils.ts w/src/utils.ts
index c6839bc..c22d09d 100644
--- i/src/utils.ts
+++ w/src/utils.ts
@@ -9,43 +9,33 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
}
const reader = stream.getReader()
- let clientDisconnected = false
-
- const handleClientDisconnect = () => {
- clientDisconnected = true
- }
-
const handleError = () => {
- clientDisconnected = true
+ // add an error handler to prevent abnormal termination, but simply ignore the error
}
- writable.on('close', handleClientDisconnect)
writable.on('error', handleError)
reader.read().then(flow, handleStreamError)
return reader.closed.finally(() => {
- writable.off('close', handleClientDisconnect)
writable.off('error', handleError)
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleStreamError(error: any) {
- if (!clientDisconnected) {
- if (error) {
- writable.destroy(error)
- }
+ if (error) {
+ writable.destroy(error)
}
}
function onDrain() {
- if (!clientDisconnected) {
+ if (!writable.writable) {
reader.read().then(flow, handleStreamError)
}
}
function flow({ done, value }: ReadableStreamReadResult<Uint8Array>): void | Promise<void> {
- if (clientDisconnected) {
+ if (!writable.writable) {
return
} |
Member
Author
|
Thank you for the comment! I think the former is okay without preventing calling |
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.
Currently, if the client disconnects, the response stream will be canceled immediately:
node-server/src/utils.ts
Line 8 in b5663eb
node-server/src/utils.ts
Line 21 in b5663eb
I'm wondering what the correct behavior is in this situation, but it causes #239. To fix it, it should handle client disconnection gracefully.
Referring to https://github.com/hi-ogawa/reproductions/tree/main/web-to-node-handler-body-cancel, the behavior of
@mjackson/node-fetch-serveris different. With this PR,@hono/node-serverbehaves the same as it.Fixes #239