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

Deadlock with android_content_provider #1063

Open
puff opened this issue Feb 27, 2024 · 2 comments
Open

Deadlock with android_content_provider #1063

puff opened this issue Feb 27, 2024 · 2 comments

Comments

@puff
Copy link

puff commented Feb 27, 2024

Documented behaviour

Loading artwork through an Android ContentProvider with the android_content_provider package.

Actual behaviour

Sometimes playing a new media item with an artUri using android_content_provider can freeze the app. It seems like a race condition causing a deadlock.

It hangs on this code:

synchronized (this) {
if (artBitmap != null)
builder.setLargeIcon(artBitmap);
}

What I believe is happening:

  • User plays item 1.
  • audio_service calls setMediaItem for item 1.
    • This blocks the background thread and AudioService class in java because it's synchronized.
    • This completes normally.
  • User plays item 2.
  • audio_service calls setMediaItem for item 2.
    • This again blocks the background thread and AudioService.
  • During this, audio_service receives a callback from the OS for building the notification (buildNotification) for item 1.
    • This blocks the main thread for audio_service as it has to wait for the setMediaItem call to complete.
  • setMediaItem never completes because the android_content_provider hangs while waiting for a response from our content provider.
    • I think that audio_service is blocking the flutter thread while waiting for a response from setMediaItem, causing a deadlock where android_content_provider can't send back a response to the java method.

Here's an export of the threads in the hanged state: threads_report.txt

Removing synchronized from the code above fixes the issue, but I'm not sure of the consequences of doing this.
Because of this, I think the issue lies within audio_service, but please let me know what you think so I can try and get this fixed!

Minimal reproduction project

https://github.com/puff/audio_service/tree/deadlock_content_provider

Reproduction steps

  1. Click play
  2. Spam next button - you may have to click fast to trigger the bug

Output of flutter doctor

[√] Flutter (Channel stable, 3.16.4, on Microsoft Windows [Version 10.0.19045.3803], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3)
[!] Android Studio (not installed)
[√] IntelliJ IDEA Ultimate Edition (version 2023.3)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 3 categories.

Devices exhibiting the bug

Pixel 8 - Android 14

@Chaphasilor
Copy link

Hey @ryanheise, would you mind taking a look? If the solution really is as simple as removing the synchronized, that would allow us to finish up Android Auto support for the next release :)
I can also reproduce this bug (Zenfone 10, tested with Head Unit and actual car).

If you need any additional info, please let us know!

@puff
Copy link
Author

puff commented Mar 23, 2024

Hi, I just wanted to note that we were able to work around the issue by implementing our content provider in Kotlin/Java instead of Flutter. However, I want to leave this issue open because eventually it may become essential to access data from our Flutter app in our content provider, and this issue prevents that.

redbaty added a commit to redbaty/audio_service that referenced this issue May 10, 2024
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

No branches or pull requests

2 participants