fix: SDK-4504 ConcurrentModificationException in Model.initializeFromModel#2642
Merged
Conversation
…Model Iterating `model.data` (a `Collections.synchronizedMap` over a `LinkedHashMap`) without holding the wrapper's monitor races with concurrent `setOptAnyProperty` mutations on the source model and trips the iterator's fail-fast modCount check, throwing `ConcurrentModificationException`. Snapshot the source map under `synchronized(model.data)`, then build the local replacement map outside any lock, then atomically swap `this.data` under `synchronized(data)`. The two monitors are acquired sequentially (never simultaneously), preserving the deadlock fix from 536fc7e while closing the unsynchronized iteration window. Add a regression test in ModelingTests that mutates a source model in a tight loop on one thread while another thread calls `initializeFromModel` 500 times. The new test reproduces the exact production CME against the buggy code and passes against the fix, alongside the existing deadlock tests. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a production ConcurrentModificationException in Model.initializeFromModel by taking a thread-safe snapshot of the source model’s synchronized map before iterating, preventing fail-fast iterator crashes when the source is concurrently mutated (notably with sdk_background_threading enabled).
Changes:
- Snapshot
model.dataundersynchronized(model.data)and iterate the snapshot instead of iterating the live synchronized map. - Keep the destination swap (
this.data.clear()/putAll) atomic undersynchronized(data)without holding both monitors at once. - Add a regression test that mutates a source model concurrently while repeatedly calling
initializeFromModel.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/modeling/Model.kt |
Prevents CME by snapshotting the source map under its monitor before copying. |
OneSignalSDK/onesignal/core/src/test/java/com/onesignal/common/ModelingTests.kt |
Adds a concurrency regression test to reproduce and prevent the CME. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+33
to
+36
| var i = 0 | ||
| while (!stop.get()) { | ||
| sourceModel.setOptAnyProperty("k${i % 32}", "v$i") | ||
| i++ |
Comment on lines
+53
to
+60
| // When both threads run concurrently | ||
| mutator.start() | ||
| initializer.start() | ||
|
|
||
| initializer.join(10_000) | ||
| stop.set(true) | ||
| mutator.join(10_000) | ||
|
|
Contributor
📊 Diff Coverage ReportDiff Coverage Report (Changed Lines Only)Gate: aggregate coverage on changed executable lines must be ≥ 80% (JaCoCo line data for lines touched in the diff). Changed Files Coverage
Overall (aggregate gate)9/9 touched executable lines covered (100.0% — requires ≥ 80%) |
fadi-george
approved these changes
May 11, 2026
This was referenced May 13, 2026
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.
Summary
Fixes a
java.util.ConcurrentModificationExceptionthrown fromModel.initializeFromModelwhen the source model is mutated by another thread while being copied. Reproduced in production on a coroutine worker (DefaultDispatcher-worker-8) insidesuspendifyWithCompletionon SDK5.9.0with thesdk_background_threadingflag enabled.Linear: SDK-4504
Root cause
model.datais aCollections.synchronizedMap(LinkedHashMap). The wrapper only synchronizes individual method calls — iteration over the entry view requires the caller to hold the wrapper's monitor (per theCollections.synchronizedMapJavadoc). When another thread mutates the source viasetOptAnyProperty, the underlyingLinkedHashMap.modCountadvances mid-iteration and the iterator's fail-fast check throwsConcurrentModificationException.This regressed in 536fc7e (
Update initializeFromModel to build a local map to replace data), which intentionally moved the iteration out of the synchronized block to fix a deadlock but did not re-protect the iteration on the source map.Affected callers
SingletonModelStore.replace()— replaces the singleton ConfigModel / IdentityModel / PropertiesModel.ConfigModelStoreListener.fetchParams— applies server config over the local config.RebuildUserService.getRebuildOperationsIfCurrentUser()— copies identity / properties / subscription models to build rebuild operations.Any of these flows can race with concurrent property mutations once background threading is enabled.
Production stack trace
com.dvex.movp5.9.0(ossdk.sdk_base_version=050900)sdk_background_threadingFix
model.data.toMap()) undersynchronized(model.data).this.dataundersynchronized(data).The two monitors are acquired sequentially, never simultaneously, so the deadlock that 536fc7e originally fixed cannot return.
Test plan
ModelingTests > initializeFromModel does not throw ConcurrentModificationException when source model is mutated concurrentlyinitializeFromModel500 times.Expected null but actual was java.util.ConcurrentModificationException).ModelingTests > Deadlock related to Model.setOptAnyPropertyModelingTests > Deadlock related to ModelStore add() or remove()ModelingTests > Unsubscribing handler in change event may cause the concurrent modification exceptionModelingTests > ensure Model Store load pulls cached operations and doesn't duplicate models./gradlew :onesignal:core:testReleaseUnitTest --tests "com.onesignal.common.ModelingTests"— all 5 tests pass.Made with Cursor