Skip to content

Commit

Permalink
fix(sse): Handler stuck when awaiting aborted SSE stream (#2273)
Browse files Browse the repository at this point in the history
* cancel internal stream reader on responseReadable abort

* run denoify
  • Loading branch information
abhi12299 authored and yusukebe committed Feb 27, 2024
1 parent d7b1fe8 commit a3d9f13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions deno_dist/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions src/utils/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a3d9f13

Please sign in to comment.