Skip to content
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

Marking an FFI function as leaf call blocks other Isolates #53030

Closed
simc opened this issue Jul 24, 2023 · 4 comments
Closed

Marking an FFI function as leaf call blocks other Isolates #53030

simc opened this issue Jul 24, 2023 · 4 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@simc
Copy link

simc commented Jul 24, 2023

Under certain conditions calling a leaf function can deadlock the app block other Isolates even though it does not call back into the Dart VM. Changing it to a non-leaf call works as expected.

Steps to reproduce:

  • create a blocking native function using sleep() for example
  • mark it as leaf function in Dart
  • call it from multiple Isolates simultaneously

I tested this on macOS M1 but it does not seem to be platform dependent.

Dart SDK version: 3.0.6 (stable) (Tue Jul 11 18:49:07 2023 +0000) on "macos_arm64"

@keertip keertip added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi labels Jul 25, 2023
@mraleph
Copy link
Member

mraleph commented Jul 25, 2023

Could you provide a full repro?

I would suspect that this is a duplicate of #51254 and #51261 - though leaf vs non-leaf should not matter in this particular case AFAIK.

Note that you should not mark potentially blocking functions as leaf (we should probably update documentation to mention that @dcharkes). VM can't do GCs while one of the threads are in the leaf call - so any attempt to trigger GC would block until blocking function returns.

@simc
Copy link
Author

simc commented Jul 25, 2023

It seems to be a separate issue from the 16 isolate maximum. It even happens with two isolates. I'll create a full example.

If leaf calls are not allowed to block the docs definitely need to be updated. They just say

isLeaf specifies whether the function is a leaf function. A leaf function must not run Dart code or call back into the Dart VM. Leaf calls are faster than non-leaf calls.

@mraleph
Copy link
Member

mraleph commented Jul 26, 2023

It seems to be a separate issue from the 16 isolate maximum. It even happens with two isolates. I'll create a full example.

Thanks. FWIW I don't think it should dead lock with something like sleep() but if you are calling something that is waiting for another isolate to do some work and be notified on completion - then this will in fact deadlock on anything that needs to bring all threads to a safepoint (e.g. GC). With something like sleep() the worst that can happen should be a temporary freeze in all isolates until sleep is done.

We will update the documentation - in general you are supposed to make leaf only those functions which are very small and short running where the overhead of full blown transition (with safepoint state updates) is too high compared to the overhead of the function itself. Don't mark anything else as leaf.

@simc
Copy link
Author

simc commented Jul 26, 2023

With something like sleep() the worst that can happen should be a temporary freeze in all isolates until sleep is done.

Oh yes you are right that is exactly what happens. I tried it again.

@simc simc changed the title Marking an FFI function as leaf call causes deadlocks Marking an FFI function as leaf call blocks other Isolates Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

3 participants