Skip to content
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

[v2] SSE connection close leads to [ERR_STREAM_PREMATURE_CLOSE]: Premature close #107

Closed
dan-lee opened this issue Dec 1, 2021 · 1 comment
Labels
bug Something isn't working fixed-in-next Fixed in next release

Comments

@dan-lee
Copy link
Contributor

dan-lee commented Dec 1, 2021

I've got a simple worker with handling SSE:

// worker.js
export default {
  async fetch() {
    const { readable, writable } = new TransformStream()
    const encoder = new TextEncoder()
    const writer = writable.getWriter()

    setInterval(() => {
      writer.write(encoder.encode(`data: hello\n\n`))
    }, 2000)

    return new Response(readable, {
      status: 200,
      headers: {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        Connection: 'keep-alive',
      },
    })
  },
}

and the runner (node --experimental-vm-modules run.js)

// run.js
import { Miniflare } from 'miniflare'

new Miniflare({
  scriptPath: './worker.js',
  modules: true,
}).startServer()

Now opening up http://localhost:8787 waiting for a few events and closing the page leads to miniflare crashing with the following node error:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.onclose (node:internal/streams/end-of-stream:140:30)
    at ServerResponse.emit (node:events:402:35)
    at Socket.onServerResponseClose (node:_http_server:235:23)
    at Socket.emit (node:events:402:35)
    at TCP.<anonymous> (node:net:687:12) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'

This problem does not occur with v1.4.1

@mrbbot mrbbot added bug Something isn't working fixed-in-next Fixed in next release labels Dec 4, 2021
@mrbbot
Copy link
Contributor

mrbbot commented Dec 8, 2021

Hey! 👋 miniflare@2.0.0-rc.3 has just been released, including the fix for this. You can find the changelog here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed-in-next Fixed in next release
Projects
None yet
Development

No branches or pull requests

2 participants