-
Notifications
You must be signed in to change notification settings - Fork 137
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
Fix ProductImageMap ConcurrentModificationException by Safely Managing WeakReferences #10910
Conversation
Implements a two-step process for safely removing cleared WeakReferences from the observer list in ProductImageMap, preventing ConcurrentModificationException during iteration.
📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.
|
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.
I've reviewed and re-tested the code and LGTM. I've left one minor suggestions.
I think it might be better to merge this after the code-freeze as it's Friday evening, having said that, it's up to you.
P.S. We could potentially also use an iterator which allows us to remove elements during iteration
val observersIterator = observers.iterator()
while (observersIterator.hasNext()) {
// notify the observer or collect it for removal if it's been garbage collected
observersIterator.next().get()?.onProductFetched(remoteProductId)
?: observersIterator.remove()
}
Having said that, I'm not convinced it's more readable with the iterator.
WooCommerce/src/main/kotlin/com/woocommerce/android/tools/ProductImageMap.kt
Outdated
Show resolved
Hide resolved
Totally fine with merging after the code freeze. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #10910 +/- ##
=========================================
Coverage 41.27% 41.27%
Complexity 5084 5084
=========================================
Files 1034 1034
Lines 59556 59556
Branches 7989 7989
=========================================
+ Hits 24582 24583 +1
+ Misses 32788 32787 -1
Partials 2186 2186 ☔ View full report in Codecov by Sentry. |
Closes: #10901
Description
In addressing the
ConcurrentModificationException
within theProductImageMap
class, we opted to refine our approach by removing the previously implementedCopyOnWriteArrayList
and instead directly tackling the issue with WeakReference objects in the observer list. Thanks to @malinajirka for this exploration.The revised solution is an improvement to the PR #10902 It involves a two-step modification process: initially, the code iterates over the observer list, identifying any
WeakReference
objects that have been cleared due to garbage collection and adding them to a temporary list. This method prevents the direct modification of the observer list during iteration, which was the primary cause of the exception.Following this, the code iterates over the temporary list to safely remove the identified references from the original observer list. By reverting the
CopyOnWriteArrayList
implementation and focusing on the specific management ofWeakReference
objects, this solution effectively addresses the root cause of the exception, ensuring the integrity of the observer list without incurring the overhead associated with introducingCopyOnWriteArrayList
.Testing instructions
Images/gif
RELEASE-NOTES.txt
if necessary. Use the "[Internal]" label for non-user-facing changes.