Repository navigation
Stop the JNI warning about dropping a GlobalRef in a detached thread on Android - #193
Merged
Merged
Conversation
…on Android The Android backend attached the calling thread to the JVM only for the scope of each JNI call and detached it when the scope ended. A GlobalRef dropped after that point, such as a field of a struct whose Drop impl attaches the thread or an argument dropped after the locals of a function, was deleted on a detached thread. The jni crate then attached the thread temporarily and logged a warning, which appeared every frame while recording. Threads are now attached permanently and detached by the jni crate when they terminate. Since detaching no longer releases the local references created on a thread, attach_current_thread pushes a local reference frame and the returned guard pops it on drop.
ruccho
marked this pull request as ready for review
October 21, 2026 06:13
hkmt-mmy
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Android, the following warning was logged every frame while recording.
attach_current_thread()returned a scopedAttachGuard, which detaches a thread that was not attached before (e.g. a tokio worker) when the guard is dropped. EveryGlobalRefdropped after the guard was therefore deleted on a detached thread, and the jni crate attached the thread again only to delete it.impl Drop for ImageWriterImage { fn drop(&mut self) { if let Ok(mut env) = attach_current_thread() { // attach Image.close(self.image) - } // detach - } // drop self.image (GlobalRef) -> warning + } // pop local frame (thread stays attached) + } // drop self.image (GlobalRef) -> OK }The same pattern exists in
queue_input_image(the image argument is dropped after the locals), the Bgra32 input path, the audio input path andpull_encoded_data_with_codec.attach_current_thread()now attaches the thread permanently; the jni crate detaches it when the thread terminates. Since detaching no longer releases the local references created on the thread, the function returns aLocalFrameEnvguard that pushes a local reference frame and pops it on drop. The guard dereferences toJNIEnv, so the call sites are unchanged.Local references must not outlive the guard. This was already required, as they became invalid when the thread was detached.
Evidence
Measured on a real device (samsung SCG13, Android 16), counting the warning in logcat.
scripts/android-device-test.sh --release(Bgra32 input), warningsAttached threaddebug logs from the jni crateThe device test passes before and after the change. In the Unity build, MediaCodec ran normally after the change and no JNI error or crash was logged.