Skip to content

Do not publish a half-initialized app instance on cancellation. - #49

Open
TheBlackBit wants to merge 1 commit into
openwallet-foundation:mainfrom
TheBlackBit:fix/app-init-not-cancellable
Open

TheBlackBit wants to merge 1 commit into
openwallet-foundation:mainfrom
TheBlackBit:fix/app-init-not-cancellable

Conversation

@TheBlackBit

@TheBlackBit TheBlackBit commented Nov 19, 2026 •

Copy link
Copy Markdown

App.getInstance() stores app before initialize() has finished. If the initialization is cancelled, the next caller gets that half-built instance back and the wallet crashes.

What happens

Cold start of the wallet by a Digital Credentials presentment request:

  1. Force-stop the wallet: adb shell am force-stop org.multipaz.wallet.android.dev.
  2. On the same phone, open a web page in Chrome that requests a credential through the Digital Credentials API, and press its button.
  3. The wallet process dies:
kotlin.UninitializedPropertyAccessException: lateinit property presentmentSource has not been initialized
  at App$Companion.getPresentmentSource(App.kt:974)
  at WalletCredentialManagerPresentmentActivity.getSettings(WalletCredentialManagerPresentmentActivity.kt:8)
  at CredentialManagerPresentmentActivity$onCreate$1$1$1$1.invokeSuspend(CredentialManagerPresentmentActivity.kt:112)

A retry after a partial initialization can fail differently, with KeyExistsStorageException: Record with key='AndroidKeystoreSecureArea' (table '_SCHEMA') already exists: the storage kept tables it had created on disk and forgot them in memory. I saw this one on 2026-11-09 and the UninitializedPropertyAccessException on 2026-11-18, on the same phone.

The change

Run the initialization in NonCancellable and publish app only after it has completed. One file, App.kt.

This covers cancellation only. If initialize() throws, app is no longer published, but tables it already created on disk remain and a retry could still fail with KeyExistsStorageException; I did not test that path and it is not addressed here. A caller that is cancelled now still waits for the initialization to finish. There is no automated test: the App singleton makes one hard to write, so the only protection is the manual run below.

Checked

  • Same steps, same build otherwise (head of Show AP2 spending permissions in the consent prompt. #48): without this change the wallet dies as above; with it, no crash. One run each, on a Samsung SM-S928B with Android 17.
  • On that build, ./gradlew :androidApp:testDebugUnitTest and ./gradlew :androidApp:lintDebug pass.

@TheBlackBit
TheBlackBit requested a review from davidz25 November 19, 2026 04:23
@TheBlackBit
TheBlackBit force-pushed the fix/app-init-not-cancellable branch from 1e74f77 to edd2579 Compare November 20, 2026 01:17
`App.getInstance()` stored `app` before `initialize()` had finished. If the
initialization was cancelled, the next caller got that half-built instance
back and failed with `kotlin.UninitializedPropertyAccessException` on
`presentmentSource`. A retry after a partial initialization failed with
`KeyExistsStorageException`, because the storage kept tables it had created
on disk and forgot them in memory.

Run the initialization in `NonCancellable` and publish `app` only after it
has completed. This covers cancellation only: a retry after `initialize()`
throws is not addressed.

Test: On a Samsung SM-S928B (Android 17) I force-stopped the wallet and
requested a credential from Chrome through the Digital Credentials API.
Without this change the wallet process died with the exception above; with
it there was no crash. I ran this on top of the head of pull request 48,
because `main` does not build against the Multipaz SDK in my local Maven
repository. On that build `./gradlew :androidApp:testDebugUnitTest` and
`./gradlew :androidApp:lintDebug` both pass.

Signed-off-by: Ever Morales <ever.morales@koombea.com>
@TheBlackBit
TheBlackBit force-pushed the fix/app-init-not-cancellable branch from edd2579 to 258141e Compare November 20, 2026 01:20
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.

1 participant