-
Notifications
You must be signed in to change notification settings - Fork 29
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
Calling a method on a class for the first time on a secondary thread fails. #190
Comments
I started looking at this and I've discovered that this is inconsistent behavior between Android and vanilla java. Curiously, I can write a test that only fails on Android but the identical test fails with vanilla Java. Updates to follow (and if folks have seen this outside of Android I'm keen to know). |
I believe this relates to the problem: In fact, there are a couple threads posted which seem to be this specific Android issue. I have a couple ideas I can try. I think if I cache the loader during the |
So, I've invested a non-zero amount of time trying to resolve this, and I think this cannot be resolved without breaking or confusing behaviour for end users . The issue is that any JNI call is effectively bound to the loader of the class that initiated the call. For Android, this means the application class loader, but when new threads are spun up, they do not have the same classloader, and fail to find the user defined class. The only workaround is to call I'll try add a syntax like so to workaround, but at the end of the day I think there's nothing I can do unfortunately. JvmRef jvm_ref {vm};
jvm_ref.Warm<kClassUsedOnOtherThread>() |
So, I continued to plug away at this, and it turns out the mistake was partly a cacheing mistake. TL;DR: I managed to get a fallback class loader to work for classes that fail lookup. The unfortunate part is that it requires some extra setup from the caller, however, it's fairly minor. Basically the caller just has to provide a single instance of the host activity so that it can cache the application cache loader (you won't have to spell out each class type which would be easy to forget). |
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
On Android, secondary threads do not have access to the application loader and there is no way to recover it. Callers can now prime a loader (i.e. whatever loader is used by the `Activity` or `Application`). This loader can be used as a fallback when looking for classes. This CL should resolve #190. PiperOrigin-RevId: 594851933
FYI I uploaded the Android test that I used to test this fix here. Sadly, I haven't managed to get these tests running under Bazel, but it at least shows the correct flow. |
This seems to be an issue with caching not occurring properly, despite a
ThreadGuard
being held.To workaround, call any method on a class from the main thread and all subsequent calls will work.
The text was updated successfully, but these errors were encountered: