[Crashlytics] Launch crashes from Keystore / encrypted-preferences recovery (shared master key) and possible local DB deletion — 208 events

XMLWordPrintable

    • FLW Sprint 56
    • FLW Mobile App
    • Prod

      Summary

      App crashes at launch in DatabaseKeyManager / PreferenceManager (KeyStoreException -22 / -30, KeyPermanentlyInvalidatedException). The recovery logic also risks deleting the local database.

      Background / Context

      Source: Sakhi/Niramay Crashlytics RCA (project utprerona-niramay-ii, app org.piramalswasthya.sakhi.niramay, Oct 12 – Nov 10 2026, latest release 2.13.0 (70)). RCA ref RCA-01, status: Probable Root Cause (not reproduced; stack traces, device and Android-version breakdown not opened). Code inspected: PSMRI/FLW-Mobile-App, branch main, commit f90f07c3 (repo version file reads 2.12.5, so main may differ from shipped 2.13.0). Priority: P0. Workbook: Sakhi_Niramay_Crashlytics_RCA.xlsx.

      Impact: 208 events (32% of all captured crashes) across 3 open issues.

      Problem Statement

      Crashes occur at app start when encrypted preferences cannot be opened or created. The top issue shows ~13 events per user (169 events / 13 users), consistent with repeated launch crashes.

      Current Behaviour (Actual)

      Crashlytics issues (events / users):

      • DatabaseKeyManager.createEncryptedPreferences (DatabaseKeyManager.kt:38) — KeyStoreException -22 (key generation failed), 3 variants — 169 / 13 — 2.8.7 → 2.13.0 — early + repetitive
      • PreferenceManager.createEncryptedPreferences (PreferenceManager.kt:39) — KeyPermanentlyInvalidatedException — 22 / 22 — new in 2.13.0
      • PreferenceManager.createEncryptedPreferences (PreferenceManager.kt:39) — KeyStoreException -30 (signature/MAC verification failed), 2 variants — 17 / 15 — 2.12.1 → 2.13.0

      Code findings (current main):

      • DatabaseKeyManager (room_db_encryption_pref) and PreferenceManager (app PREF_NAME file) both use MasterKeys.AES256_GCM_SPEC, i.e. the same Keystore alias androidx_security_master_key.
      • Each recovery block deletes that shared alias plus only its own prefs file. Recovering one store therefore invalidates the other's key, which can trigger -30 / invalidated errors and another recovery.
      • The retry after recovery (createEncryptedPreferences(context)) has no try/catch (PreferenceManager.kt:29, DatabaseKeyManager.kt:19), so a second failure crashes the app.
      • After DatabaseKeyManager recovery a NEW DB passphrase is generated; RoomDbEncryptionHelper.encryptIfNeeded (lines 54-56) then cannot open the existing SQLCipher file and deletes it ("Deleting for fresh start") — unsynced local data can be lost.
      • Auto-backup restore hypothesis rejected on config (allowBackup=false).

      Expected Behaviour / Requirement

      • Recovering one encrypted store must never invalidate the other.
      • A repeated Keystore failure must not crash the app at launch; the user gets a recoverable path and a non-fatal is logged.
      • An unreadable database must not be deleted silently while it may hold unsynced data.

      User Story

      As a field user, I want the app to open reliably after a Keystore problem, so that I can keep working without losing unsynced beneficiary data.

      Acceptance Criteria

      • Given DatabaseKeyManager and PreferenceManager, When either store recovers, Then the other store's key remains intact (separate aliases).
      • Given a Keystore that fails twice, When the app starts, Then the app does not crash and shows a recoverable screen / guidance, and a non-fatal is reported.
      • Given an existing encrypted DB that cannot be opened with the current key, When RoomDbEncryptionHelper runs, Then the file is not deleted without the agreed safeguard [TBD/CLARIFICATION REQUIRED].
      • Given the master key is deleted on a test device, When the app restarts, Then both stores recover without a launch crash.
      • Crashlytics custom keys (failing stage, device model, Android version) are available for these issues after release.

      Business Rules / Validations

      • What must happen to unsynced local data when the DB key is lost: [TBD/CLARIFICATION REQUIRED].

      Dependencies

      • Possible interplay with data-sync flows (unsynced data export/sync before reset) [TBD].

      Impact / Affected Areas

      DatabaseKeyManager.kt, PreferenceManager.kt, RoomDbEncryptionHelper.kt, InAppDb.kt (lines 3610-3612), app startup path; all users on 2.8.7 – 2.13.0.

      Out of Scope

      Fixing OEM Keystore defects outside the app's control (error -22 on some devices); this ticket only makes the app handle them safely.

      QA / Testing Considerations

      Delete/invalidate the Keystore alias on a test device (both stores); simulate repeated failure; wrong-key DB test with unsynced rows; verify no new KeyStore events in Crashlytics after release; test on low-end and OEM devices seen in Crashlytics.

      Open Questions / Clarifications

      • Which devices / Android versions produce -22 and -30 (open the issue pages)?
      • Required behaviour for unsynced data when the DB cannot be decrypted.
      • Is raising the minimum supported version for older builds in scope?

      Source: Sakhi_Niramay_Crashlytics_RCA.xlsx (RCA-01, CAPA-01 to CAPA-05, CAPA-21). RCA is a draft pending human confirmation.

              Assignee:
              Ishwar Chouhan
              Reporter:
              Shivani Garg
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated: