Skip to content
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

Merged
merged 2 commits into from
Feb 26, 2024

Conversation

jd-alexander
Copy link
Contributor

@jd-alexander jd-alexander commented Feb 23, 2024

Closes: #10901

Description

In addressing the ConcurrentModificationException within the ProductImageMap class, we opted to refine our approach by removing the previously implemented CopyOnWriteArrayList 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 of WeakReference objects, this solution effectively addresses the root cause of the exception, ensuring the integrity of the observer list without incurring the overhead associated with introducing CopyOnWriteArrayList.

Testing instructions

  1. Switch from one store to the next.
  2. Open the Orders tab.
  3. Notice no crash takes place.

Images/gif

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Implements a two-step process for safely removing cleared WeakReferences from the observer list in ProductImageMap, preventing ConcurrentModificationException during iteration.
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Feb 23, 2024

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
FlavorJalapeno
Build TypeDebug
Commit49a6057
Direct Downloadwoocommerce-prototype-build-pr10910-49a6057.apk

Copy link
Contributor

@malinajirka malinajirka left a 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.

@jd-alexander
Copy link
Contributor Author

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.

Totally fine with merging after the code freeze.

@jd-alexander jd-alexander added the status: do not merge Dependent on another PR, ready for review but not ready for merge. label Feb 23, 2024
@codecov-commenter
Copy link

codecov-commenter commented Feb 23, 2024

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 41.27%. Comparing base (672695b) to head (49a6057).
Report is 56 commits behind head on trunk.

Files Patch % Lines
...n/com/woocommerce/android/tools/ProductImageMap.kt 0.00% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@malinajirka malinajirka removed the status: do not merge Dependent on another PR, ready for review but not ready for merge. label Feb 26, 2024
@malinajirka malinajirka merged commit 9522db3 into trunk Feb 26, 2024
14 of 15 checks passed
@malinajirka malinajirka deleted the fix/weakref-concurrent-modification branch February 26, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Tablet Support M2] Crash when switching from one store to the next
4 participants