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

fix: Cache errors when rapidly switching sources #68

Merged
merged 9 commits into from
Oct 31, 2024

Conversation

daytime-em
Copy link
Collaborator

@daytime-em daytime-em commented Oct 31, 2024

Root cause: calling SQLiteDatabase.close() (via use) is not thread-safe, even though it appears to be. This is a problem because media3's loader is multithreaded. The SQLiteDatabase and SQLiteCursor all have guards around their init and closing logic, but the underlying SQLiteConnectionPool isn't guarded adequately in this case. Since a single CacheDatastore can be used by multiple loader threads (as-designed in media3), we can't rely on SQLiteDatabase.close().

Because of all of that, rapidly switching media sources many times (like when fling-scrolling a feed) would occasionally result in playback errors

Solution: Explicitly keep an open SQLiteDatabase object around for as long as a Player needs a CacheDatastore. This is fine to do, and was the general intent of my last attempt at fixing this. Before, I was using acquireReference to hopefully keep the SQLiteDatabase's underlying connection open even if close() was called. This wasn't effective thanks to media3's multithreaded loading behavior. Now, we just keep the DB object (and therefore connection) open until we are closed (when our MuxPlayer is released)

@daytime-em daytime-em requested a review from a team as a code owner October 31, 2024 19:49
Copy link

@cjpillsbury cjpillsbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@daytime-em daytime-em merged commit 96fa5a6 into releases/v1.2.1 Oct 31, 2024
1 check passed
@daytime-em daytime-em deleted the fix/cache-errors-db branch October 31, 2024 21:05
@github-actions github-actions bot mentioned this pull request Oct 31, 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

Successfully merging this pull request may close these issues.

None yet

3 participants