-
Notifications
You must be signed in to change notification settings - Fork 376
bug: Handling race condition #2408
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
Open
abdulraqeeb33
wants to merge
6
commits into
main
Choose a base branch
from
bug/initWithContextRaceCondition
base: main
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.
+110
−60
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
580b0bc
Handling race conditions
4c8a9e7
fix test
7e71e06
updating flow to use debugUnitTest
b51b6b9
removed exceptions and just logging
fb729bb
Change unit test command to testReleaseUnitTest
abdulraqeeb33 6e2531e
moving the code a bit
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,7 +263,6 @@ internal class OneSignalImp( | |
| suspendifyOnIO { | ||
| internalInit(context, appId) | ||
| } | ||
| initState = InitState.SUCCESS | ||
| return true | ||
| } | ||
|
|
||
|
|
@@ -306,22 +305,48 @@ internal class OneSignalImp( | |
| ) { | ||
| Logging.log(LogLevel.DEBUG, "Calling deprecated login(externalId: $externalId, jwtBearerToken: $jwtBearerToken)") | ||
|
|
||
| if (!initState.isSDKAccessible()) { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'login'") | ||
| // Check state and provide appropriate error messages | ||
| when (initState) { | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| InitState.NOT_STARTED -> { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'login'") | ||
| } | ||
| InitState.IN_PROGRESS, InitState.SUCCESS -> { | ||
| // Continue - these states allow proceeding (will wait if needed) | ||
| } | ||
| } | ||
|
|
||
| waitForInit() | ||
jinliu9508 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| suspendifyOnIO { loginHelper.login(externalId, jwtBearerToken) } | ||
| } | ||
|
|
||
| override fun logout() { | ||
| Logging.log(LogLevel.DEBUG, "Calling deprecated logout()") | ||
|
|
||
| if (!initState.isSDKAccessible()) { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'logout'") | ||
| // Check state and provide appropriate error messages | ||
| when (initState) { | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| InitState.NOT_STARTED -> { | ||
| throw IllegalStateException("Must call 'initWithContext' before 'logout'") | ||
| } | ||
| InitState.IN_PROGRESS, InitState.SUCCESS -> { | ||
| // Continue - these states allow proceeding (will wait if needed) | ||
| } | ||
|
||
| } | ||
|
|
||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| suspendifyOnIO { logoutHelper.logout() } | ||
| } | ||
|
|
||
|
|
@@ -358,6 +383,10 @@ internal class OneSignalImp( | |
| withTimeout(MAX_TIMEOUT_TO_INIT) { | ||
| initAwaiter.awaitSuspend() | ||
| } | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } catch (e: TimeoutCancellationException) { | ||
| throw IllegalStateException("initWithContext was timed out after $MAX_TIMEOUT_TO_INIT ms") | ||
| } | ||
|
|
@@ -384,13 +413,21 @@ internal class OneSignalImp( | |
| InitState.IN_PROGRESS -> { | ||
| Logging.debug("Waiting for init to complete...") | ||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } | ||
| InitState.FAILED -> { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| else -> { | ||
| // SUCCESS | ||
| waitForInit() | ||
| // Re-check state after waiting - init might have failed during the wait | ||
| if (initState == InitState.FAILED) { | ||
| throw IllegalStateException("Initialization failed. Cannot proceed.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -150,8 +150,19 @@ class SDKInitTests : FunSpec({ | |
| accessorThread.join(500) | ||
|
|
||
| // Then | ||
| // should complete even SharedPreferences is unavailable | ||
| // should complete even SharedPreferences is unavailable (non-blocking) | ||
| accessorThread.isAlive shouldBe false | ||
|
|
||
| // Release the SharedPreferences lock so internalInit can complete | ||
| trigger.complete() | ||
|
|
||
| // Wait for initialization to complete (internalInit runs asynchronously) | ||
| var attempts = 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the wait mechanism for the initialization seems repetitive in the test. Can we clean this up? |
||
| while (!os.isInitialized && attempts < 50) { | ||
| Thread.sleep(20) | ||
| attempts++ | ||
| } | ||
|
|
||
| os.isInitialized shouldBe true | ||
| } | ||
|
|
||
|
|
@@ -224,12 +235,23 @@ class SDKInitTests : FunSpec({ | |
| accessorThread.start() | ||
| accessorThread.join(500) | ||
|
|
||
| os.isInitialized shouldBe true | ||
| // initWithContext should return immediately (non-blocking) | ||
| // but isInitialized won't be true until internalInit completes | ||
| // which requires SharedPreferences to be unblocked | ||
| accessorThread.isAlive shouldBe true | ||
|
|
||
| // release the lock on SharedPreferences | ||
| // release the lock on SharedPreferences so internalInit can complete | ||
| trigger.complete() | ||
|
|
||
| // Wait for initialization to complete (internalInit runs asynchronously) | ||
| var initAttempts = 0 | ||
| while (!os.isInitialized && initAttempts < 50) { | ||
| Thread.sleep(20) | ||
| initAttempts++ | ||
| } | ||
|
|
||
| os.isInitialized shouldBe true | ||
|
|
||
| accessorThread.join(500) | ||
| accessorThread.isAlive shouldBe false | ||
| os.user.externalId shouldBe externalId | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.