Skip to content

Commit 85682cb

Browse files
committed
Fix possible race in user creation
This issue showed up a lot in CI where things are a lot slower. The createprofile fragment spawns a coroutine that will create a user and returns before it's done, then the contactlist fragment will try to load the user, leading to a crash if the user-creation coroutine hasn't done its thing yet. With this change, we wait for the user-creation coroutine to complete before moving to the contactlist fragment. To reproduce the issue locally, I added a delay(1000) in UserManager::create.
1 parent bcae2ef commit 85682cb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

atox/src/main/kotlin/ui/createprofile/CreateProfileFragment.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// SPDX-FileCopyrightText: 2020-2021 aTox contributors
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <[email protected]>
2+
// SPDX-FileCopyrightText: 2022 aTox contributors
23
//
34
// SPDX-License-Identifier: GPL-3.0-only
45

@@ -19,6 +20,7 @@ import androidx.core.view.WindowInsetsCompat
1920
import androidx.core.view.updatePadding
2021
import androidx.fragment.app.viewModels
2122
import androidx.navigation.fragment.findNavController
23+
import kotlinx.coroutines.runBlocking
2224
import ltd.evilcorp.atox.R
2325
import ltd.evilcorp.atox.databinding.FragmentProfileBinding
2426
import ltd.evilcorp.atox.ui.BaseFragment
@@ -95,7 +97,10 @@ class CreateProfileFragment : BaseFragment<FragmentProfileBinding>(FragmentProfi
9597
name = if (username.text.isNotEmpty()) username.text.toString() else getString(R.string.name_default),
9698
statusMessage = getString(R.string.status_message_default),
9799
)
98-
viewModel.create(user)
100+
101+
runBlocking {
102+
viewModel.create(user).join()
103+
}
99104

100105
findNavController().popBackStack()
101106
}

0 commit comments

Comments
 (0)