Skip to content

Fix: Prevent freezes when AirPods connection hangs#579

Merged
d4rken merged 1 commit into
mainfrom
fix/aap-connect-anr
May 3, 2026
Merged

Fix: Prevent freezes when AirPods connection hangs#579
d4rken merged 1 commit into
mainfrom
fix/aap-connect-anr

Conversation

@d4rken

@d4rken d4rken commented May 3, 2026

Copy link
Copy Markdown
Member

What changed

Fixed app freezes (ANRs) that could happen when an AirPods connection attempt got stuck instead of completing or failing in time.

Technical Context

  • Root cause: BluetoothSocket.connect() is a blocking JNI call that doesn't respect coroutine cancellation. The previous withTimeout only cancelled the suspending wrapper while the native thread stayed pinned, so hung connect attempts piled up and could surface as ANRs
  • Fix moves the timeout inside the AAP connection layer and runs the blocking connect on a daemon thread; on timeout the caller closes the socket, which is the documented Android way to unblock an in-flight L2CAP connect
  • Also cancels the app coroutine scope before delegating uncaught exceptions so coroutines get a best-effort window to release resources before the system handler kills the process

BluetoothSocket.connect() is a blocking JNI call that ignores coroutine cancellation, so the previous withTimeout in AapAutoConnect only cancelled the suspending wrapper while the native thread stayed pinned. Hung threads accumulated and could trigger ANRs.

Move the timeout inside AapConnection and run the blocking connect on a daemon thread; on timeout, close the socket from the caller thread to unblock the native call (the documented Android pattern for cancelling in-flight L2CAP connects).

Also cancel appScope before delegating uncaught exceptions so coroutines have a best-effort window to release resources before the system handler terminates the process.
@d4rken d4rken added bug Something isn't working coms/AAP Uses Apples AirPod Protocol. Requires Android ROM with fixed L2CAP support on the Bluetooth sockets. labels May 3, 2026
@d4rken d4rken merged commit 07c72dc into main May 3, 2026
11 checks passed
@d4rken d4rken deleted the fix/aap-connect-anr branch May 3, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working coms/AAP Uses Apples AirPod Protocol. Requires Android ROM with fixed L2CAP support on the Bluetooth sockets.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant