Skip to content

[native_assets] Hot restart native libs #55850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #50565
dcharkes opened this issue May 27, 2024 · 2 comments
Open
Tracked by #50565

[native_assets] Hot restart native libs #55850

dcharkes opened this issue May 27, 2024 · 2 comments
Labels
area-native-interop Used for native interop related issues, including FFI. library-ffi type-enhancement A request for a change that isn't a bug

Comments

@dcharkes
Copy link
Contributor

We should consider trying to support "hot restart" for native code.

The native code would need to be bundled with dylibs (not statically linked).

We could unload all dylibs loaded by @Natives and purging caches for resolved natives.

For Dart in JIT mode this should be enough, as the dylibs will just be on disk. (Windows might be problematic if as dylibs cannot be deleted while in use.)

For Flutter in JIT mode we'd need some Flutter specific support for copying the dylibs to the right place (and possibly signing them).

Hot reload doesn't make sense for dylibs, because the memory of the dylib cannot be migrated (the native languages don't have the same support as Dart).
Hot restarting native libs when doing a Dart hot reload seems to be a bad idea, due to the Dart and native state getting out of sync.

We'd need to decide whether we'd always want hot restart native libs on hot restarting Dart, or whether it should be a different command. I think hot restarting both together would be better than hot restarting one. Hot restarting only native code means things like native finalizers in the Dart heap would still exist. (Due to native finalizers being run on isolate shutdown, we should probably shut down Dart isolates first, then unload the dylibs and then start a Dart isolate again.)

Thanks for the suggestion @fzyzcjy! 🙏

Related issues:

@fzyzcjy
Copy link
Contributor

fzyzcjy commented May 27, 2024

You are welcome and thanks for the issue!

Hot reload doesn't make sense for dylibs, because the memory of the dylib cannot be migrated (the native languages don't have the same support as Dart).

For Rust, there are some (very hacky) approaches: https://github.com/rksm/hot-lib-reloader-rs. This is super hacky, but if the user just modifies some logic without changing the state, hopefully that will work.

@dcharkes
Copy link
Contributor Author

If we do it for native assets with the build and link hooks. We also need to think about when to rerun the native builds. That actually advocates for a new letter in flutter run:

  • r Dart hot reload (quick)
  • R Dart hot restart (fairly quick)
  • T [TBD what letter] native+Dart hot restart (slower, because it will rerun build / link hooks, which could be fast but could also be fairly slow)

Open to alternative suggestions on what the user experience should be.

@mit-mit mit-mit added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label May 28, 2024
@a-siva a-siva added the type-enhancement A request for a change that isn't a bug label May 29, 2024
@a-siva a-siva added area-native-interop Used for native interop related issues, including FFI. and removed area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. labels Jun 7, 2024
@dcharkes dcharkes added this to the Native Assets v1.x milestone Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-native-interop Used for native interop related issues, including FFI. library-ffi type-enhancement A request for a change that isn't a bug
Projects
Status: No status
Development

No branches or pull requests

4 participants