Fixed
Status Update
Comments
su...@google.com <su...@google.com>
ap...@google.com <ap...@google.com> #2
Project: platform/frameworks/support
Branch: androidx-master-dev
commit e36c65f67e49ddbfa0080542295f4a4850ae17da
Author: Sumir Kataria <sumir@google.com>
Date: Tue Aug 07 13:04:03 2018
Enforce ConstraintTracker#setState is on the main thread.
- setState now asserts that we are on the main thread
- Moved assert*Thread methods into their own utils
- Annotated several tests manually calling onBroadcastReceive
as @UiThreadTest as our broadcast receivers only operate on
the main thread (they use the default registerReceiver
method)
- NetworkCallbacks are invoked on non-main threads by default;
in this case, we re-post them to the main thread
This is part of the solution to a concurrency exception in
ConstraintTracker.
Bug: 112272753
Test: Updated and ran tests
Change-Id: I7011e5993b814f4fcafe861e283a827cd1edee12
M work/workmanager/build.gradle
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/BatteryChargingTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/BatteryNotLowTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/ConstraintTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/StorageNotLowTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/workers/ConstraintTrackingWorkerTest.java
M work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java
M work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java
M work/workmanager/src/main/java/androidx/work/impl/constraints/trackers/ConstraintTracker.java
M work/workmanager/src/main/java/androidx/work/impl/constraints/trackers/NetworkStateTracker.java
A work/workmanager/src/main/java/androidx/work/impl/utils/ThreadUtils.java
https://android-review.googlesource.com/727429
https://goto.google.com/android-sha1/e36c65f67e49ddbfa0080542295f4a4850ae17da
Branch: androidx-master-dev
commit e36c65f67e49ddbfa0080542295f4a4850ae17da
Author: Sumir Kataria <sumir@google.com>
Date: Tue Aug 07 13:04:03 2018
Enforce ConstraintTracker#setState is on the main thread.
- setState now asserts that we are on the main thread
- Moved assert*Thread methods into their own utils
- Annotated several tests manually calling onBroadcastReceive
as @UiThreadTest as our broadcast receivers only operate on
the main thread (they use the default registerReceiver
method)
- NetworkCallbacks are invoked on non-main threads by default;
in this case, we re-post them to the main thread
This is part of the solution to a concurrency exception in
ConstraintTracker.
Bug: 112272753
Test: Updated and ran tests
Change-Id: I7011e5993b814f4fcafe861e283a827cd1edee12
M work/workmanager/build.gradle
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/BatteryChargingTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/BatteryNotLowTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/ConstraintTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/constraints/trackers/StorageNotLowTrackerTest.java
M work/workmanager/src/androidTest/java/androidx/work/impl/workers/ConstraintTrackingWorkerTest.java
M work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java
M work/workmanager/src/main/java/androidx/work/impl/background/systemalarm/SystemAlarmDispatcher.java
M work/workmanager/src/main/java/androidx/work/impl/constraints/trackers/ConstraintTracker.java
M work/workmanager/src/main/java/androidx/work/impl/constraints/trackers/NetworkStateTracker.java
A work/workmanager/src/main/java/androidx/work/impl/utils/ThreadUtils.java
Description
Version used: 1.0.0-alpha06
Devices/Android versions reproduced on: Many
We can see a large number of ConcurrentModificationExceptions in WorkManager:
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 bqHint=4 (has extras) } in vu@2406837b
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:988)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:346)
at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:366)
at androidx.work.impl.constraints.trackers.ConstraintTracker.setState(OperaSrc:89)
at androidx.work.impl.constraints.trackers.NetworkStateTracker$NetworkStateBroadcastReceiver.onReceive(OperaSrc:157)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:978)
It looks like the mListeners set is being modified during the loop in ConstraintTracker.setState(). I see two potential issues with this method:
1. Maybe a listener is added or removed in onConstraintChanged(). I haven't seen this happen, but it might be good to make a copy of mListeners and iterate over the copy.
2. The NetworkStateTracker.mNetworkCallback is called on a different thread than the main thread. In consequence setState() is called on a different thread so there is a possibility that the main thread adds or removes a listener. This cannot be the (only) reason because the error happens in response to the CONNECTIVITY_CHANGE.
This may be fixed with synchronization or by registering mNetworkCallback to run on the main thread.