fix(di): defer database provisioning off the Hilt inject path #137

Merged
JMR-dev merged 4 commits from fix-providedatabase-anr into main 2026-07-02 16:23:15 +00:00
4 Commits
Author SHA1 Message Date
Jason Ross d7594c0b39 Merge branch 'main' into fix-providedatabase-anr 2026-07-02 11:11:42 -05:00
Jason Ross 3b210059db Merge branch 'main' into fix-providedatabase-anr 2026-07-02 10:33:15 -05:00
Jason Ross a6e213490a Merge branch 'main' into fix-providedatabase-anr 2026-07-02 10:14:28 -05:00
JMR-devandClaude Fable 5 ed9b9e2742 fix(di): defer database provisioning off the Hilt inject path
DatabaseModule.provideDatabase ran the whole startup sequence with
runBlocking while Hilt constructed the singleton database — a DataStore
read, a Keystore op, a possible SQLCipher re-key conversion, and (since
#111) the cross-database AccountDataMigrator — synchronously on whichever
thread first injected it, which can be the main thread (jank / ANR).

Move that work behind DatabaseProvisioner.prepareCache(): a memoized,
mutex-guarded suspend that runs the same sequence, in the same order, on
the IO dispatcher. Both databases' Room builders now open through a
DeferredOpenHelperFactory whose delegate — and therefore the gate — is
materialised only when Room first OPENS the database, on its background
query executor, never at inject time. AccountDatabase's open gates on the
same prepareCache(), preserving the #111 migrate-before-open ordering that
the old construction-time dependency on LibreMailDatabase enforced.

Behaviour, ordering, and crash-safety are unchanged — only where and when
the work runs moved off the (possibly main) inject thread.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 10:01:46 -05:00