fix: Remove throwing "initWithContext was not called or timed out", introduced in 5.4.0#2408
Merged
abdulraqeeb33 merged 18 commits intomainfrom Dec 8, 2025
Merged
Conversation
jkasten2
requested changes
Nov 12, 2025
Comment on lines
+332
to
+342
| // 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) | ||
| } |
Member
There was a problem hiding this comment.
Duplicate code, can we make a function for this?
Contributor
Author
There was a problem hiding this comment.
good callout, i centralized all of this. including the code path for suspend and blocking.
jinliu9508
reviewed
Nov 24, 2025
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt
Outdated
Show resolved
Hide resolved
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt
Outdated
Show resolved
Hide resolved
jinliu9508
reviewed
Dec 2, 2025
| trigger.complete() | ||
|
|
||
| // Wait for initialization to complete (internalInit runs asynchronously) | ||
| var attempts = 0 |
Contributor
There was a problem hiding this comment.
nit: the wait mechanism for the initialization seems repetitive in the test. Can we clean this up?
jkasten2
requested changes
Dec 2, 2025
Member
jkasten2
left a comment
There was a problem hiding this comment.
I would rather not have waitUntilInitInternal return, but initState == InitState.IN_PROGRESS. It creates an extra inconsistent state and doesn't provide any benefit I can see.
I would rather we just wait here as long as it takes so code doesn't have to consider both states. PR #2412 does this, and only logs if it is taking longer than expected.
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt
Outdated
Show resolved
Hide resolved
jkasten2
requested changes
Dec 3, 2025
...ignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/OneSignalDispatchers.kt
Outdated
Show resolved
Hide resolved
...ignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/OneSignalDispatchers.kt
Outdated
Show resolved
Hide resolved
...ignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/OneSignalDispatchers.kt
Outdated
Show resolved
Hide resolved
jkasten2
approved these changes
Dec 3, 2025
added 2 commits
December 5, 2025 17:33
jkasten2
approved these changes
Dec 8, 2025
jinliu9508
pushed a commit
that referenced
this pull request
Jan 6, 2026
…ntroduced in 5.4.0 (#2408) Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
jinliu9508
pushed a commit
that referenced
this pull request
Jan 8, 2026
…ntroduced in 5.4.0 (#2408) Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
jinliu9508
pushed a commit
that referenced
this pull request
Jan 8, 2026
…ntroduced in 5.4.0 (#2408) Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
jinliu9508
pushed a commit
that referenced
this pull request
Jan 8, 2026
…ntroduced in 5.4.0 (#2408) Co-authored-by: AR Abdul Azeez <abdul@onesignal.com>
This was referenced Jan 8, 2026
This was referenced Jan 28, 2026
This was referenced Jan 30, 2026
Closed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
One Line Summary
Fix race condition in initWithContext preventing concurrent initialization and premature SDK access.
Details
This PR fixes a race condition that could occur when
initWithContext()was called multiple times concurrently, or when SDK accessors were accessed before initialization completed. The fix ensures thread-safe initialization state management and prevents inconsistent SDK state.The Problem
The race condition could manifest in two scenarios:
Concurrent Initialization: If multiple threads called
initWithContext()simultaneously, both could attempt to initialize the SDK, leading to:initStatevaluesPremature Access: If SDK accessors (like
OneSignal.User,OneSignal.Notifications, etc.) were accessed beforeinitWithContext()completed, they could:NullPointerExceptionThe Solution
The fix implements proper synchronization and state management:
synchronized(initLock)aroundinitStatechecks to prevent concurrent initialization attemptsinitStateis properly set toIN_PROGRESSbefore starting initialization andSUCCESS/FAILEDafter completionCompletableDeferredto ensure consistent stateRelated Issues
Motivation
To address the race condition where concurrent initialization attempts or premature SDK access could lead to crashes or inconsistent behavior.
Scope
OneSignalImp.initWithContext()synchronizedblocksTesting
Unit testing
InitStateenum transitionsManual testing
initWithContext()calls from multiple threadsinitWithContext()Affected code checklist
Checklist
Overview
Testing
Final pass
This change is