From a3d9f1371b12844e39751d61f3d4692dd8516a97 Mon Sep 17 00:00:00 2001 From: Abhishek Mehandiratta <36722596+abhi12299@users.noreply.github.com> Date: Tue, 27 Feb 2024 09:27:06 +0530 Subject: [PATCH] fix(sse): Handler stuck when awaiting aborted SSE stream (#2273) * cancel internal stream reader on responseReadable abort * run denoify --- deno_dist/utils/stream.ts | 7 +++++++ src/utils/stream.ts | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/deno_dist/utils/stream.ts b/deno_dist/utils/stream.ts index 7dfd4e4e5..52a8a5d89 100644 --- a/deno_dist/utils/stream.ts +++ b/deno_dist/utils/stream.ts @@ -12,6 +12,13 @@ export class StreamingApi { const reader = _readable.getReader() + // in case the user disconnects, let the reader know to cancel + // this in-turn results in responseReadable being closed + // and writeSSE method no longer blocks indefinitely + this.abortSubscribers.push(async () => { + await reader.cancel() + }) + this.responseReadable = new ReadableStream({ async pull(controller) { const { done, value } = await reader.read() diff --git a/src/utils/stream.ts b/src/utils/stream.ts index 7dfd4e4e5..52a8a5d89 100644 --- a/src/utils/stream.ts +++ b/src/utils/stream.ts @@ -12,6 +12,13 @@ export class StreamingApi { const reader = _readable.getReader() + // in case the user disconnects, let the reader know to cancel + // this in-turn results in responseReadable being closed + // and writeSSE method no longer blocks indefinitely + this.abortSubscribers.push(async () => { + await reader.cancel() + }) + this.responseReadable = new ReadableStream({ async pull(controller) { const { done, value } = await reader.read()