miniflare icon indicating copy to clipboard operation
miniflare copied to clipboard

"Some functionality can only be performed while handling a request" whenever running in a debugger

Open JosXa opened this issue 2 years ago • 3 comments

My project runs fine using a regular start/dev command. However, whenever a debugger from VSCode or WebStorm attaches itself to the process, all requests subequently fail with

GET /myurl 500 Internal Server Error (22891.94ms)
[mf:err] GET /myurl: Error: Some functionality, such as asynchronous I/O (fetch, Cache API, KV), timeouts (setTimeout, 
setInterval), and generating random values (crypto.getRandomValues, crypto.subtle.generateKey), can only be performed wh
ile handling a request.

I am 100% certain that my code is not doing any i/o outside of its main request handler. How can we help to reproduce this, as in #207 the same issue occurs?

JosXa avatar Jun 20 '22 18:06 JosXa

From the v2.0 changelog

To match the behaviour of the Workers runtime, some functionality, such as asynchronous I/O (fetch, Cache API, KV), timeouts (setTimeout, setInterval), and generating cryptographically-secure random values (crypto.getRandomValues, crypto.subtle.generateKey), can now only be performed while handling a request.

This behaviour can be disabled by setting the --global-async-io/globalAsyncIO, --global-timers/globalTimers and --global-random/globalRandom options respectively, which may be useful for tests or libraries that need async I/O for setup during local development. Note the Miniflare Jest environment automatically enables these options.

Maybe the debugger mode does something magic that requires you to disable this feature.

dan-lee avatar Jun 24 '22 10:06 dan-lee

That's also my suspicion. Disabling the warning does the trick as a workaround.

JosXa avatar Jun 26 '22 01:06 JosXa

Hey! 👋 Thanks for reporting this. I'm able to reproduce it and will investigate further. Will aim to fix in the next release.

mrbbot avatar Jul 02 '22 12:07 mrbbot

I’ve suddenly started running into this a lot and not sure what the cause is. Occasionally restarting the debugger in VS Code works fine, but usually it breaks it.

For anyone else running into it, remember to enable global_async_io, global_timers and global_random in all of your mounted workers.

huw avatar Sep 03 '22 09:09 huw

Hey everyone! 👋 Looks like this is because of an issue in Node: https://github.com/nodejs/node/issues/43148. A fix has been merged (https://github.com/nodejs/node/pull/44423) which should be released soon. 👍 It seems this issue only affects Node 18 though, so in the meantime, you may want to downgrade to Node 16.

mrbbot avatar Sep 03 '22 19:09 mrbbot

Hey again! Looks like this fix was released as part of Node 18.9.0. Upgrading should resolve this issue! 🎉

mrbbot avatar Sep 13 '22 19:09 mrbbot