-
Notifications
You must be signed in to change notification settings - Fork 64
fix: handle client disconnection without canceling stream #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle client disconnection without canceling stream #258
Conversation
Hi @usualoma Sorry for pinging again. Can you review this? |
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() |
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
} |
Co-authored-by: Taku Amano <[email protected]>
Thank you for the comment! I think the former is okay without preventing calling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. LGTM!
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-server
is different. With this PR,@hono/node-server
behaves the same as it.Fixes #239