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

ndk-glue: Don't return from onCreate until LOOPER is created #131

Merged
merged 1 commit into from
Mar 20, 2021

Conversation

MarijnS95
Copy link
Member

Android only performs additional initialization like providing an input queue through onInputQueueCreated after the main initialization function (ANativeActivity_onCreate) returns. In some cases it is possible that the main thread spawned here hasn't attached a looper to its thread and assigned it to the accompanying LOOPER static variable yet, resulting in a None unwrap when Android provides us with an input queue.

This race condition is simply solved by not returning from fn init() (ANativeActivity_onCreate) until the thread has finished its setup, through wrapping LOOPER in a Mutex and using a condition variable to signal when it is ready.

This condition is intentionally not exposed to on_input_queue_created may we ever have a cleanup procedure that sets LOOPER back to None; any call to onInputQueueCreated in that state would be an error and should not block indefinitely. The LOOPER should simply be ready by the time fn init() returns.

Fixes #116
CC @Gordon-F

Android only performs additional initialization like providing an input
queue through `onInputQueueCreated` after the main initialization
function (`ANativeActivity_onCreate`) returns.  In some cases it is
possible that the main thread spawned here hasn't attached a looper to
its thread and assigned it to the accompanying `LOOPER` static variable
yet, resulting in a `None` unwrap when Android provides us with an input
queue.

This race condition is simply solved by not returning from `fn init()`
(`ANativeActivity_onCreate`) until the thread has finished its setup,
through wrapping `LOOPER` in a `Mutex` and using a condition variable to
signal when it is ready.

This condition is intentionally not exposed to `on_input_queue_created`
may we ever have a cleanup procedure that sets `LOOPER` back to `None`;
any call to `onInputQueueCreated` in that state would be an error and
should not block indefinitely.  The `LOOPER` should simply be ready by
the time `fn init()` returns.

Fixes rust-mobile#116
@Gordon-F
Copy link
Contributor

@MarijnS95 Thank you! Tested this patch in my case. Works well.

@msiglreith msiglreith merged commit b430a5e into rust-mobile:master Mar 20, 2021
@MarijnS95 MarijnS95 deleted the glue-wait-for-thread branch March 20, 2021 19:50
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.

Sometimes running apps panicked on the Android Emulator
3 participants