-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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() | ||
|
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. We are essentially checking init state before and after waitForInit(). Should we let waitForInit() handle the throwing/proceeding based on the initState so we don't need to check for the state again in the usage? |
||
| // 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) | ||
| } | ||
|
Comment on lines
+332
to
+342
Member
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. Duplicate code, can we make a function for this? |
||
| } | ||
|
|
||
| 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.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Can isSDKAccessible() be changed to something like checkSDKAccessible() so that it throws proper error messages based on initState, and do nothing if allow proceeding?