Skip to content

Fix session-scoped injection and database migration issues#6230

Merged
SeniorZhai merged 2 commits intofeature/session-scope-injection-migrationfrom
claude/sub-pr-6190
Mar 18, 2026
Merged

Fix session-scoped injection and database migration issues#6230
SeniorZhai merged 2 commits intofeature/session-scope-injection-migrationfrom
claude/sub-pr-6190

Conversation

@Claude
Copy link
Contributor

@Claude Claude AI commented Mar 17, 2026

Addresses critical session-scoped dependency injection and database migration ordering issues that cause crashes on startup/logout and potential data loss.

Session-scoped dependency injection

Problem: Components requesting session-scoped databases (MixinDatabase, WalletDatabase, PendingDatabase, FtsDatabase) crash when Session.getAccount() is null (first launch, logout, background start).

Solution:

  • BaseDbModule now uses Provider<CurrentUserScopeManager> to defer database resolution until actually needed
  • Injector init block checks for active session before attempting injection, falls back to ensureSessionInjection() when session is available
// Before: Crashes immediately if session is null
fun provideDb(scopeManager: CurrentUserScopeManager) = scopeManager.getMixinDatabase()

// After: Resolves lazily when injected dependency is used
fun provideDb(scopeManagerProvider: Provider<CurrentUserScopeManager>) = 
    scopeManagerProvider.get().getMixinDatabase()

Database migration ordering

Problem: AccountSessionInitializer called clearJobsAndRawTransaction() before CurrentUserScopeManager.enter(), which created empty scoped DB files before legacy-to-scoped migration ran. Migration then skipped moving legacy data (target exists), orphaning the real database.

Solution: Reordered to enter scope (triggers migration) before any database operations:

// Store session → Enter scope & migrate → Clear jobs → Clear settings
Session.storeAccount(account)
resolveCurrentUserScopeManager(context).enter(account)
clearJobsAndRawTransaction(context, account.identityNumber)

Removed premature MixinDatabase.getDatabase() call in clearJobsAndRawTransaction() that could create empty DB files.

Resource leaks

  • SafeBoxStoreManager: DataStore instances accumulated indefinitely with uncancelled coroutine scopes. Added clearCurrent()/clearAll() methods to cancel scopes on logout/account switch.
  • PendingDatabaseImp: Static supportSQLiteDatabase reference retained closed database connections across scope changes. Now cleared when closing or switching identities.

Same-user re-login

AccountSessionInitializer now checks if logging into the same account and preserves SharedPreferences/CryptoWallet settings on re-login, only clearing on account switch.

Address critical issues identified in PR review:
- Fix SafeBoxStoreManager memory leak by tracking and cleaning up coroutine scopes
- Fix PendingDatabaseImp to properly clear supportSQLiteDatabase on scope changes
- Fix AccountSessionInitializer to migrate databases BEFORE clearing jobs/raw transactions
- Fix BaseDbModule to use Provider for session-scoped DB bindings to defer resolution
- Fix Injector to defer session-scoped injection until session is available
- Fix AccountSessionInitializer to preserve settings on same-user re-login
- Fix DatabaseUtil to avoid creating empty scoped DB files prematurely
- Remove unused import and errorMessage observation in SetPinLoadingPage

Co-authored-by: crossle <1675131+crossle@users.noreply.github.com>
@Claude
Copy link
Contributor Author

Claude AI commented Mar 17, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED -XX:&#43;UseParallelGC -Xmx10g -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Claude Claude AI changed the title [WIP] Migrate app storage to session/user-scoped database model Fix session-scoped injection and database migration issues Mar 17, 2026
@Claude Claude AI requested a review from crossle March 17, 2026 13:01
@SeniorZhai SeniorZhai marked this pull request as ready for review March 18, 2026 01:54
@SeniorZhai SeniorZhai merged commit 4abbebc into feature/session-scope-injection-migration Mar 18, 2026
@SeniorZhai SeniorZhai deleted the claude/sub-pr-6190 branch March 18, 2026 01:54
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.

3 participants