-
Notifications
You must be signed in to change notification settings - Fork 122
fix(storage): Prevent duplicate observer registration in TransferWorkerObserver to mitigate OOM issues #2988
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(storage): Prevent duplicate observer registration in TransferWorkerObserver to mitigate OOM issues #2988
Conversation
…server to mitigate OOM (aws-amplify#2840)
f599641
to
9b11da8
Compare
Thank you for the PR, someone on our team will take a look and get back to you if we have any questions! |
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.
Hi @onseok. The idea of the change looks good to me but you missed adding the tag to the set in attachObserver
. We can also take advantage of the return values of add
/remove
.
aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferWorkerObserver.kt
Outdated
Show resolved
Hide resolved
aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferWorkerObserver.kt
Outdated
Show resolved
Hide resolved
aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferWorkerObserver.kt
Outdated
Show resolved
Hide resolved
aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferWorkerObserver.kt
Outdated
Show resolved
Hide resolved
Once the above updates are made we can run tests and get this merged. Thanks for the contribution! 🚀 |
Hi @mattcreaser, thank you for the feedback! I've updated the PR to use the return values of |
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.
Looks good to me. I've started running tests and once we get passes we can merge this to include in our next release.
Hi @onseok the PR is failing lint check. Please run this command and ensure it passes: |
Hi @mattcreaser, I’ve fixed it by reordering the imports. It should pass |
@onseok this PR has been merged and will be included in our next release. Thanks again! |
Issue #, if available:
Issue #2840
Description of changes:
The error appears on devices with limited heap (for example, some tablets or set-top boxes running Android 7.1.2/OS 14) when many pending transfers trigger multiple LiveData observer registrations in the TransferWorkerObserver. These duplicate observer registrations may lead to excessive internal allocations (via Room’s InvalidationTracker, MapBuilder, and SetBuilder), eventually causing OOM.
To address this, the PR introduces a thread-safe set (observedTags) that tracks which transfer tags have already been observed. In the attachObserver() method, we now check if the tag is already present in observedTags and return early if so; otherwise, the observer is registered and the tag is added to the set. Similarly, in removeObserver(), we first verify that the tag exists in observedTags before unregistering the observer and then remove the tag. This change prevents the same transfer from being observed multiple times, reducing memory pressure and mitigating OOM issues without impacting the core functionality of transfer monitoring.
How did you test these changes?
Due to the internal and private visibility of several classes and methods in this module, direct testing of these functions is challenging.
Documentation update required?
General Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.