Closed alsemenov closed 7 years ago
Your ping
helper method is async
. That means that its body isn't executed immediately, but is instead scheduled for a later microtask. Right after calling ping
, you call isolate.kill
, so the kill
message is actually sent before the ping
message. That's also why it works if the ping
function is inlined - then the ping
is sent before the kill
. Since the function doesn't need to be async
, I'll wager that if you remove the async
marker, the test will succeed.
I'll take this as another vote for #22909.
You are right, the root cause is async
marker for method test. I have removed it and test passes. So this is my mistake, I forgot that async methods are executed later.
However, some error message, that ping is sent to already killed isolate would be helpful in understanding what is wrong.
The Isolate
object could maintain state to know that kill
has been sent, and throw if you try to do something after a kill
. It's not that simple, though - since a kill operation might fail if you don't have the correct capability, and one way to test if it failed is to request a ping after the kill and see if you get a response.
All in all, the Isolate
class is a very low-level API, really just the raw way to send control messages to another isolate. I don't think it's a good idea to try to make it much fancier, because it's still going to be low-level, now just with a few odd higher-level features bolted on. Instead we should make sure that users have better, probably more specialized, higher level APIs build on top of the Isolate
class, like the IsolateRunner
from package:isolate
.
The description for method Isolate.kill() reads:
The following sample code demonstrates, that ping event, which is send before
kill()
, is discarded.Output:
Expected output:
Unfortunately, the sample code is not stable enough. For example if function
ping
is inlined test passes. Also it passes withisolate.kill(priority:Isolate.BEFORE_NEXT_EVENT);
but more complex scenarios withBEFORE_NEXT_EVENT
fail.Dart VM version: 1.21.0 (Wed Dec 07 06:52:21 2016) on "windows_x64"