Skip to content

Commit

Permalink
[monodroid] Fix thread state transition in get_function_pointer (#9343
Browse files Browse the repository at this point in the history
)

Fixes: #8253

`get_function_pointer` can be called from any thread. In particular,
it can be called from Android thread pool thread when method
marshaling is used. We need to ensure those threads stay in the GC
safe mode from the runtime perspective once we stop executing user
code on those threads. `mono_thread_attach` switches the thread to GC
unsafe mode and it stays there after we stop executing user code. The
next GC will try to suspend the thread which cannot succeed since
there's no cooperative runtime code running on that thread anymore.

The fix is to use `mono_jit_thread_attach` which mimics what the
iOS/macOS interop does. It creates the necessary MonoVM thread
structures for JIT to work but doesn't transition the thread state.
  • Loading branch information
filipnavara authored Sep 30, 2024
1 parent 230ebb0 commit 27b5d2e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/native/monodroid/xamarin-android-app-context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
}

// We need to do that, as Mono APIs cannot be invoked from threads that aren't attached to the runtime.
mono_thread_attach (mono_get_root_domain ());
mono_jit_thread_attach (mono_get_root_domain ());

MonoImage *image = MonoImageLoader::get_from_index (mono_image_index);
MarshalMethodsManagedClass &klass = marshal_methods_class_cache[class_index];
Expand Down

0 comments on commit 27b5d2e

Please sign in to comment.