Skip to content

Fix casting crashing CastBay on Android 6: detach native threads on exit - #194

Merged
easonxiang merged 1 commit into
mainfrom
jni-detach
Oct 5, 2026
Merged

easonxiang merged 1 commit into
mainfrom
jni-detach

Conversation

@easonxiang

Copy link
Copy Markdown
Contributor

Fixes casting crashes on Android 6. Report CB-4K36FJ shows three restarts in a row on a Xiaomi TV (Android 6.0.1). The TV's tombstones show the cause: ART aborts with Native thread exited without calling DetachCurrentThread.

currentEnv() attached UxPlay threads to the JVM and never detached them. Android 7+ only warns about this, but Android 6 aborts the process. The fix registers a pthread key whose destructor calls DetachCurrentThread, as ART's warning suggests.

Tests

  • New tools/sim jnithread: a native thread attaches and exits.
  • Xiaomi TV (Android 6), before the fix: the same abort and SIGSEGV as the report.
  • Xiaomi TV, after the fix: survives three runs, with only ART's warning.
  • Emulator (Android 16): unaffected.
  • Lint and unit tests pass.

🤖 Generated with Claude Code

UxPlay's threads attach to Java the first time they call back
(currentEnv) and ended without detaching. Android 6's ART aborts the
process when such a thread ends: "Native thread exited without calling
DetachCurrentThread". Casting from an iPhone to a Xiaomi TV (Android
6.0.1) crashed CastBay three times in a row (report CB-4K36FJ; the
TV's tombstones show the abort). Later Android versions only warn.

A pthread key whose destructor detaches the thread is now set when a
thread is attached, as ART's own warning suggests.

tools/sim jnithread starts a native thread that attaches and exits.
On the Xiaomi TV it reproduced the crash before this change, and the
app survives it after; the emulator (Android 16) is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@easonxiang
easonxiang merged commit 82eacb2 into main Oct 5, 2026
6 checks passed
@easonxiang
easonxiang deleted the jni-detach branch October 5, 2026 06:10
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.

1 participant