Skip to content

Fixed cache load startup race condition - #3968

Open
choppeh wants to merge 9 commits into
mihonapp:mainfrom
choppeh:bf/cache
Open

choppeh wants to merge 9 commits into
mihonapp:mainfrom
choppeh:bf/cache

Conversation

@choppeh

@choppeh choppeh commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Closes #3526
Closes #2808
Closes #1429 (This will be fixed because the cache was no longer being pre-deleted; the current issue arises because the cache can be deleted, and reindexing halted, before the new cache is saved)

This PR fixes issues where the cache was lost on every app restart.

Previously, calling invalidateCache() deleted diskCacheFile immediately, leaving the cache cleared if renewal was cancelled or delayed, which caused downloaded chapters to incorrectly appear missing in the UI. Additionally, an uncoordinated startup sequence allowed UI queries to trigger full filesystem scans before the disk cache finished reading.

To resolve this, diskCacheFile now uses AtomicFile to prevent file corruption upon abrupt process termination, while initial disk loading is synchronized via initJob to ensure UI queries do not trigger premature re-indexing. Invalidation now marks RootDirectory as expired through its embedded creation timestamp and TTL rather than deleting the file upfront. Furthermore, disk writes are deferred until indexing successfully completes, and ProtoBuf serialization is made thread-safe

Additionally, a new preference has been added under Advanced Settings allowing users to customize the download cache recheck interval.

Images

Image 1 Image 2

Real device

Whenever the app was restarted, reindexing started from scratch. If there were many files to check, this could take a long time, becoming a problem when users opened and closed the app multiple times a day because the previous cache was lost.

Now, the cache is reused based on its lifespan.

commit 449c4e0 Fixed
issue.webm
fix.webm
speed 2.2x speed 1x

@choppeh
choppeh marked this pull request as draft September 16, 2026 07:36
@choppeh choppeh changed the title Fixes the temporary absence of downloaded chapters Fix download index cache persistence and startup race condition Sep 16, 2026
@choppeh
choppeh force-pushed the bf/cache branch 2 times, most recently from 8afaaf0 to f4217b7 Compare September 17, 2026 20:19
@choppeh
choppeh marked this pull request as ready for review September 17, 2026 20:19
@AntsyLich

Copy link
Copy Markdown
Member

Run ./gradlew spotlessApply

@AntsyLich

Copy link
Copy Markdown
Member

Imo the PR doesn’t solve #2030. Can you remove that from changelog and PR body

- Use AtomicFile to ensure atomic reads/writes and prevent corrupted cache files on sudden termination
- Synchronize initialization with initJob to prevent UI calls from triggering premature re-indexing
- Embed createdAt and ttl in RootDirectory with isExpired check
- Avoid premature disk writes before filesystem indexing completes
- Guard UniFile serialization against nulls and Injekt errors
Allow users to configure the download cache time-to-live (TTL) in Advanced Settings, with a minimum threshold of 30 minutes.
@choppeh choppeh changed the title Fix download index cache persistence and startup race condition Fixed cache load startup race condition Sep 22, 2026
@choppeh

choppeh commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Imo the PR doesn’t solve #2030. Can you remove that from changelog and PR body

Updated the changelog with the startup race condition fix

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.

Reindex glitch Slower loading when the app opens ( beta version) Downloaded chapters not showing

2 participants