Skip to content

Today I learned

Why does my ReadableStream hang when pull() doesn't enqueue anything?

· updated #Web development

A ReadableStream that re-emits another stream (SSE from an LLM API, say) froze halfway: the reader waited forever, no error. The cause: when pull() resolves without calling controller.enqueue(), the stream does not call pull() again. Only an enqueue or a new read() while pull() is running schedules the next call, so a chunk of events you skip leaves the reader hanging.

Keep reading inside pull() until you have enqueued something, closed or errored:

ts
new ReadableStream<Uint8Array>({
  async pull(controller) {
    while (true) {
      const { done, value } = await upstream.read()
      if (done) return controller.close()
      const text = extractText(value) // may be '' for bookkeeping events
      if (text) {
        controller.enqueue(encoder.encode(text))
        return // hand back now so deltas flush as they arrive
      }
      // nothing enqueued: loop, don't return
    }
  },
  cancel: reason => upstream.cancel(reason),
})

Hit this while streaming OpenAI Responses events into a plain-text stream for a rewrite tool: events like response.created carry no text, and a pull that returned on them stalled the page. The same loop is also the place to controller.error() on a response.incomplete event, so a cut-off answer rejects the reader instead of looking finished.

Docs: ReadableStream() constructor, pull

Share LinkedIn Post on X

Related notes

Newsletter

Occasional notes on engineering, game tech, and new tools — no spam, unsubscribe anytime.

Ravy

Engineering manager, founder, and software developer building web platforms, game systems, real-time services, and automation tools. 15 years of shipped work — currently at Gaijin.net.

Get in Touch

Building an MVP, shipping a game feature, or automating a team workflow? Open source or paid — let's talk.

Contact Me
© 2020-2026 XPLOIT FZE. All trademarks, names and logos belong to their respective copyright holders.