dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

[vm] Should native finalizers run on hot-restart #50016

Open dcharkes opened 2 years ago

dcharkes commented 2 years ago

We guarantee to run all native finalizers before VM-shutdown completes. We do not guarantee to run all native finalizers before an isolate shuts down.

Should hot-restart have the VM-shutdown semantics? E.g. all heaps get released and all native finalizers are run before hot-restart completes? Or, should it be thought of as simply shutting down all isolates and starting new ones but not stopping and starting the VM?

cc @mraleph

rmacnak-google commented 2 years ago

Isn't the finalizer guarantee scoped to the heap / isolate group instead of the VM? I would expect hot-restart to shut down a whole group and meet this guarentee, but maybe hot-restart hasn't been updated since the introduction of isolate groups and is still operating at the isolate level?

maks commented 2 years ago

I should add that in the example code I added to in flutter/flutter#75528 while I do have a secondary Isolate, I was trying to use the NativeFinalizer in the main Isolate only and it didn't seem to be working with a hot-restart.

mraleph commented 1 year ago

@maks if you have a secondary isolate that secondary isolate likely keeps the isolate group alive which prevents finalisers from running. Which would explain why native finalizers don't fire. We are probably leaking secondary isolates on restart?

This might be yet another argument for creating process tree like primitive for isolates similar to what Erlang has.

maks commented 1 year ago

@mraleph I thought you had spotted the issue, but I just tried adding a Isolate.exit() along with print("exiting Helper Isolate"); on line before it, to my helper Isolate (as I was only using the Isolate to invoke the potentially long running operation of initialising the audio library) and it doesn't seem to help in that I don't see the printf() I have put into my audio library shutdown function and the whole apps still crashes straight after the audio lib initialisation post hot-restart:

flutter run -d linux lib/main.dart 
Launching lib/main.dart on Linux in debug mode...
Building Linux application...                                           
Syncing files to device Linux...                                    57ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

💪 Running with sound null safety 💪

An Observatory debugger and profiler on Linux is available at: http://127.0.0.1:32975/fzHarI7Hwfs=/
flutter: exiting Helper Isolate
The Flutter DevTools debugger and profiler on Linux is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:32975/fzHarI7Hwfs=/

Performing hot restart...                                               
Restarted application in 214ms.
flutter: exiting Helper Isolate
Lost connection to device.

I'm in the middle of tidying up my plugin code for publication, so I'll post a link to the repo here as soon its done in case that would help.

Irrespective of the above, having a process tree for Isolates similar to Erlangs would be FANTASTIC! 👍🏻 💯 🙏🏻

a-siva commented 9 months ago

I'm in the middle of tidying up my plugin code for publication, so I'll post a link to the repo here as soon its done in case that would help.

@maks Can you post a link to the repo that you describe above.

maks commented 9 months ago

@a-siva sorry its been a while since I commented on this issue, I think the project I was working on was this one, but I haven't touched it in quite a while to be honest as I got a bit discouraged trying to get low latency audio working with Dart.