miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

Worker reload in `watch` mode doesn't close HTTP connections

Open jed opened this issue 3 years ago • 1 comments

(This is a reboot of https://github.com/cloudflare/miniflare/issues/173, with a working repro.)

As I understand it, when a Durable Object's code is updated on Cloudflare, all connected sockets are closed. This is a behavior mismatch with Miniflare, which does not disconnect upon code update in watch mode.

Steps to reproduce:

  1. Create a file called worker.js with the following:
export class DO {
  fetch(request) {
    let start = controller => {
      setInterval(() => {
        let now = new Date().toISOString()
        let data = new TextEncoder().encode(now + '\n')
        controller.enqueue(data)
      }, 500)
    }

    let stream = new ReadableStream({start})
    return new Response(stream)
  }
}

export default {
  fetch (request, env) {
    return env.DO.get(env.DO.newUniqueId()).fetch(request)
  }
}
  1. In a terminal window, run miniflare worker.js -m -w -o DO=DO to run the worker in watch mode.
  2. In another terminal window, run curl -N -v http://127.0.0.1:8787.
  3. Observe that a timestamp is written every 500ms.
  4. Edit the worker.js file, such as by adding a string to now.
  5. Note that Worker reloaded! is logged to the terminal window from (1).
  6. Note that the terminal window from (2) is unchanged; the string from (5) is not reflected, and the HTTP connection is not closed.
  7. Kill the miniflare process.
  8. Note that the HTTP connection has been closed in the terminal window from (2).

jed avatar Feb 15 '22 07:02 jed

Hey! 👋 Thanks for raising this and apologies for the delayed response. We should be able to fix this with customsetInterval and setTimeout implementations that attached to the RequestContext of the Durable Object, which could contain a reference to the DurableObjectState. These could then be reset in the Durable Object plugin's beforeReload hook.

mrbbot avatar Mar 04 '22 20:03 mrbbot

This issue should be fixed in Miniflare 3, which uses workerd. 👍

mrbbot avatar Oct 17 '22 16:10 mrbbot