Description
The RawReceivePort.handler
is always run in the root zone.
(Or rather, it doesn't set the Zone._current
, so it runs in whatever zone was set when control reaches the event loop. That should be the root zone, because every setting of another zone should be paired with a finally
setting it back to the previous value. It probably works.)
Running things in the root zone is dangerous.
Users may not be prepared for their code running in a different zone, possibly a different error zone.
I'll readily admit that I have written lots of RawReceivePort
code and never remembered that it was running in a different zone. (I also don't make mistakes, so I won't have uncaught errors in the root zone.)
It also leaks the root zone. I want to make zones hermetically closed, so the only zone code has direct access to is Zone.current
(#61060). Being able to leave a zone makes it much harder to use zones for encapsulating code.
Being able to access the root zone by doing RawReceivePort..handler = (_) { code to run }..sendPort.send(null);
makes it pointless to hide Zone.root
.
Currently, RawReceivePort
is Zone
-agnostic. It doesn't use or touch zones. If it starts binding its callback in the current zone (with the dart:isolate
and/or dart:async
libraries maybe having ways to bypass that if necessary), then using RawReceivePort
in "non-isolate excecution" won't work. I think it wouldn't anyway, you won't get callbacks if you have no event loop, but it is a change.
It may also confuse any code that assumes callbacks will run in the root zone, but more likely callbacks are just zone agnostic, unless they throw, in which case they'd be throwing in the root zone of the VM, which means crashing the program. So probably won't happen.