-
-
Notifications
You must be signed in to change notification settings - Fork 49
QA-8627 Don't Write Connect Data After Signing Out Of PersonalID (2/2: make Connect storage safe to reach around sign-out) #3860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
OrangeAndGreen
wants to merge
1
commit into
master
Choose a base branch
from
QA-8627-stale-connect-db-write-after-signout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/src/org/commcare/connect/database/ConnectDatabaseUnavailableException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package org.commcare.connect.database | ||
|
|
||
| /** | ||
| * Thrown when Connect storage is accessed with no passphrase to open it, which is the expected state | ||
| * once the user has signed out of PersonalId. | ||
| * | ||
| * Deliberately not a [org.commcare.connect.network.LoginInvalidatedException]: that one is | ||
| * reserved for a DB that can't be opened despite having a passphrase (corruption or bad | ||
| * encryption), and reaching the uncaught handler with it wipes the account and restarts the | ||
| * process. A missing passphrase just means the caller raced with sign-out and its work is no | ||
| * longer wanted. | ||
| * | ||
| * @author dviggiano | ||
| */ | ||
| class ConnectDatabaseUnavailableException( | ||
| message: String, | ||
| ) : RuntimeException(message) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/unit-tests/src/org/commcare/connect/database/ConnectDatabaseHelperTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package org.commcare.connect.database | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import org.commcare.CommCareTestApplication | ||
| import org.commcare.android.database.connect.models.ConnectUserRecord | ||
| import org.commcare.connect.network.LoginInvalidatedException | ||
| import org.junit.After | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.annotation.Config | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.atomic.AtomicReference | ||
|
|
||
| /** | ||
| * Covers Connect storage being reached around sign-out, which an in-flight request can still do | ||
| * because the sign-in check and the storage access aren't atomic. | ||
| * | ||
| * The invariant under test is that losing that race stays survivable: it must never flag the DB as | ||
| * broken or raise [LoginInvalidatedException], because reaching the uncaught handler with that wipes | ||
| * the account and restarts the process. | ||
| */ | ||
| @Config(application = CommCareTestApplication::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| class ConnectDatabaseHelperTest { | ||
| private val context: Context = CommCareTestApplication.instance() | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| ConnectUserDatabaseUtil.forgetUser() | ||
| } | ||
|
|
||
| private fun readUserStorage() = | ||
| ConnectDatabaseHelper | ||
| .getConnectStorage(context, ConnectUserRecord::class.java) | ||
| .getRecordsForValues(emptyArray(), emptyArray()) | ||
|
|
||
| @Test | ||
| fun testForgetUserLeavesNoPassphraseAndNoBrokenFlag() { | ||
| ConnectUserDatabaseUtil.forgetUser() | ||
|
|
||
| // the passphrase going away is what tells the storage layer there's no account to open a | ||
| // DB for, and it has to land together with the deletion rather than partway through it | ||
| assertNull(ConnectDatabaseUtils.getKeyRecord()) | ||
| assertFalse(ConnectDatabaseHelper.dbExists()) | ||
| assertFalse(ConnectDatabaseHelper.isDbBroken()) | ||
| } | ||
|
|
||
| @Test | ||
| fun testGetUserAfterForgetUserReturnsNullWithoutFlaggingDb() { | ||
| ConnectUserDatabaseUtil.forgetUser() | ||
|
|
||
| // the ordinary "do I have an account" check treats an absent DB as no account, so it stays | ||
| // cheap and side-effect free | ||
| assertNull(ConnectUserDatabaseUtil.getUser(context)) | ||
| assertFalse(ConnectDatabaseHelper.isDbBroken()) | ||
| } | ||
|
|
||
| @Test | ||
| fun testStorageReadRacingSignOutNeverInvalidatesLogin() { | ||
| val readerReady = CountDownLatch(1) | ||
| val signOutDone = CountDownLatch(1) | ||
| val fatal = AtomicReference<Throwable?>(null) | ||
|
|
||
| val reader = | ||
| Thread { | ||
| readerReady.countDown() | ||
| // keep hammering storage across the sign-out so at least one read lands on either | ||
| // side of it, and ideally one lands mid-teardown | ||
| repeat(200) { | ||
| try { | ||
| readUserStorage() | ||
| } catch (expected: ConnectDatabaseUnavailableException) { | ||
| // the read can't succeed once the account is gone, it just has to fail | ||
| // survivably | ||
| } catch (t: Throwable) { | ||
| if (t is LoginInvalidatedException) { | ||
| fatal.compareAndSet(null, t) | ||
| } | ||
| } | ||
| } | ||
| signOutDone.countDown() | ||
| } | ||
|
|
||
| reader.start() | ||
| assertTrue(readerReady.await(5, TimeUnit.SECONDS)) | ||
| ConnectUserDatabaseUtil.forgetUser() | ||
| reader.join(TimeUnit.SECONDS.toMillis(30)) | ||
|
|
||
| assertNull("A read racing sign-out invalidated the login", fatal.get()) | ||
| assertFalse("A read racing sign-out flagged the Connect DB as broken", ConnectDatabaseHelper.isDbBroken()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 50379
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 50379
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 50380
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 26957
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 24805
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 10157
Handle the sign-out race in Connect parsers and workers.
ConnectDatabaseHelperis the only catch site, but parsers andMessagingChannelsKeySyncWorkercan still propagateConnectDatabaseUnavailableException. TheisloggedIn()check inConnectReleaseTogglesParserdoes not prevent a concurrent sign-out. Return the expected no-op result when this exception occurs.🤖 Prompt for AI Agents