Skip to content

Make "rust-async" feature optional #2723

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
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

alexlapa
Copy link
Contributor

Fixes #2253

Changes

So I've got pretty much the same issue as described in #2253 - I only use #[frb(sync)], so I want to turn off rust-async and threadpool so there wont be 2*num_cpus threads just hangin there doing nothing. Disabling threadpool works fine, but when I try to disable rust-async I get complation errors:

cannot find function `rust_auto_opaque_encode` in module `flutter_rust_bridge::for_generated`

cannot find type `RustAutoOpaqueInner` in module `flutter_rust_bridge::for_generated`

no method named `dart_fn_handle_output` found for struct `api_bridge_generated::FLUTTER_RUST_BRIDGE_HANDLER` in the current scope

cannot find function `lockable_compute_decode_order` in module `flutter_rust_bridge::for_generated`

unresolved import `flutter_rust_bridge::for_generated::Lockable`

Currently I fix this by having rust-async enbled and declaring a custom FLUTTER_RUST_BRIDGE_HANDLER that has no async executor. Thats fine, but I believe that this should be fixed here.

So this PR tries to make rust-async feature optional and decouple it from dart-opaque.

Procedure and Checklist

In order to quickly iterate and avoid slowing down development pace by making CI pass, only the following simplified steps are needed, and I (fzyzcjy) will handle the rest of CI / moving the tests currently (will have more automation in the future).

  • Implement the feature / fix the bug.
  • Add tests in frb_example/dart_minimal.
  • Make dart_minimal's CI green.

Utility commands

  • Run codegen: cargo run --manifest-path /path/to/flutter_rust_bridge/frb_codegen/Cargo.toml -- generate
  • Run tests: ./frb_internal test-dart-native --package frb_example/dart_minimal

@alexlapa
Copy link
Contributor Author

So the reason why CI fails right now is basicaly this:

diff --git a/frb_rust/src/dart_api/dart_api.h b/frb_rust/src/dart_api/dart_api.h
index 1d83c90..7394525 100644
--- a/frb_rust/src/dart_api/dart_api.h
+++ b/frb_rust/src/dart_api/dart_api.h
@@ -837,7 +837,7 @@ typedef Dart_Handle (*Dart_GetVMServiceAssetsArchive)(void);
  * The current version of the Dart_InitializeFlags. Should be incremented every
  * time Dart_InitializeFlags changes in a binary incompatible way.
  */
-#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000008)
+#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000009)

...

diff --git a/frb_rust/src/dart_api/dart_version.h b/frb_rust/src/dart_api/dart_version.h
index 5ca0b68..ddfaf3a 100644
--- a/frb_rust/src/dart_api/dart_version.h
+++ b/frb_rust/src/dart_api/dart_version.h
@@ -11,6 +11,6 @@
 // On backwards compatible changes the minor version is increased.
 // The versioning covers the symbols exposed in dart_api_dl.h
 #define DART_API_DL_MAJOR_VERSION 2
-#define DART_API_DL_MINOR_VERSION 5
+#define DART_API_DL_MINOR_VERSION 6

I sure can generate and push updated bindings but this would probably be wrong. I guess that the protocol for this should be something like: regenerated dart-sys, update dart-sys in Cargo.toml, upgrade FRB_MAIN_DART_VERSION and FRB_MAIN_FLUTTER_VERSION in github workflow, upgrade environment.sdk constraint in frb_dart/pubspec.yaml. So it seems to be out of scope fo this PR.

@alexlapa alexlapa marked this pull request as ready for review May 16, 2025 07:29
Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Only some small suggestions

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be great to add some tests e.g. following #2681

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well its not removing rust-async from default, but makes it optional, so everything except exporting async functions should work as before.

I've added frb_example/no_rust_async which just tests regualr stuff but with rust-async featrue disabled.

CI will probably fail, Il fix it later, just want to be sure that this apporach is ok. Let me know what you think about this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looks like there is no need to add a new example, but directly use approach in #2681 and use existing examples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so I guess what you mean is ae8c875 which changes getRustFeaturesOfPackage to return a list of feature configurations and testDartNative will iterate through all configuration running tests multiple times.

But the issue is that I cant run all tests in frb_example two times (with rust-async and without) cause this PR does not affect code generation. Meaning that if there is an async function exported from Rust its FFI glue will be generated, but it will fail to compile later. And this is the intened behaviour.

So to run all tests two times it will aslo need to hide exported Rust async functions behind a #[cfg] and then run codegen between test runs. And that would also probalby require some major github workflow or frb_internal rework.

I've tried to implement this approach and added tests to frb_example/dart_minimal. It runst two times with and without rust-async but it only works because there are no async functions in Rust. So tell me what you think about this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fzyzcjy ping

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops I did not receive any pings :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is because your new commits will make this pr in my github noti inbox, and then I come here and see there are the "New changes since you last viewed" hints and thus I leave :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I originally meant to mimic that commit and test multi times. Now I see dart_minimal by has async functions, and thus we cannot trivially mimic that approach. I am ok for whichever approach!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, and I've missed you reply somehow :) Only received notification when you've merged master here. I'll take a look at it this week.

Copy link
Owner

@fzyzcjy fzyzcjy Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take your time! I will check every notification here; but if I somehow again missed your reply, just ping me on issues (since that one will only be triggered when there are messages, and no commits will trigger).

@fzyzcjy
Copy link
Owner

fzyzcjy commented May 16, 2025

No worries about that CI failure caused by dart sdk bump, I will handle it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs/examples/fixes to make rust-async, threadpool optional
2 participants