workers-sdk: ๐Ÿ› BUG: `wrangler dev --experimental-local` slows down over time and leaks memory

What version of Wrangler are you using?

2.6.2

What operating system are you using?

Mac

Describe the Bug

If I test my simple worker with wrangler dev --experimental-local and monitor the browser devtools network tab I see that after the initial (cold) requests then next requests return in 2ms.

If I keep on refreshing the browser, I see that time slowly starts to creep up, 4ms, 6ms, 8ms, and if I keep on refreshing the browser tab a lot, it slows down to 50-70ms and keeps on slowing down further with additional http requests.

At the same time I observe that the memory usage increases from 17MB to 34MB, 50MB, 75MB, all the way to over 1GB.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 27 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Iโ€™d like to double down on this issue, as itโ€™s currently blocking our effort to use wrangler dev in a local development / integration testing environment.

3.1.0 MacOS

For measurement I used

hey -c 1 -n 10000 http://127.0.0.1:8787/

Bare minimum worker, no deps, no bindings

export default {
  async fetch(request: Request, env: any, executionCtx: ExecutionContext) {
    return new Response("Hello", {status: 200})
  },
}

Observed Very progressive slowdown, and in the end crash

[mf:inf] GET / 200 OK (18ms)
[mf:inf] GET / 200 OK (20ms)
[mf:inf] GET / 200 OK (20ms)
[mf:inf] GET / 200 OK (20ms)
*** Received signal #10: Bus error: 10
stack: 
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ [b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit                                                                                                               โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Did a control measurement with Service Worker format vs Module worker

addEventListener('fetch', (event) => {
  event.respondWith(new Response("Hello", {status: 200}))
})

The worker performs a lot better with wrangler dev when in the Service Worker format, but crashes all the same with the same error, just later (after way more requests)

Iโ€™ll perform an experiment with emulated amd64 arch a bit later

Can report this is still the case in wrangler 3.0.1 on MacOS.

Note that this does not require the wrangler server to be used at all. It will leak just by starting it.

Command:

wrangler dev --port 2222

I also get this error after some time; Iโ€™m not sure if itโ€™s related to the leak.

Failed to get worker definitions TypeError: fetch failed

      at fetch (/Users/a/webaverse/node_modules/wrangler/wrangler-dist/cli.js:16584:17)
      at async getRegisteredWorkers
  (/Users/a/webaverse/node_modules/wrangler/wrangler-dist/cli.js:124280:22)
      at async getBoundRegisteredWorkers
  (/Users/a/webaverse/node_modules/wrangler/wrangler-dist/cli.js:124299:29) {
    cause: HeadersTimeoutError: Headers Timeout Error
        at Timeout.onParserTimeout [as callback]
  (/Users/a/webaverse/node_modules/wrangler/wrangler-dist/cli.js:8431:33)
        at Timeout.onTimeout [as _onTimeout]
  (/Users/a/webaverse/node_modules/wrangler/wrangler-dist/cli.js:681:17)
        at listOnTimeout (node:internal/timers:573:17)
        at process.processTimers (node:internal/timers:514:7) {
      code: 'UND_ERR_HEADERS_TIMEOUT'
    }
  }

Getting a similar issue on the latest wrangler. Donโ€™t know if it โ€œslows downโ€ but memory usage quickly goes to 10->20->30gb and it doesnโ€™t come back down unless killed.

I was not able to verify/test because I got blocked on https://github.com/cloudflare/workers-sdk/issues/3007 which seems to suggest that --experimental-local is currently broken in the 2.14.0 release for some if not for all users.