Fixed race condition in PollingDeviceDiscovery. (#145506)
There are two issues in the previous implementation: 1. `_populateDevices` will return the devices from `deviceNotifier` if it had been initialized, assuming that once it's initialized, it has been properly populated. That assumption is not true because calling getters like `onAdded` would initialize `deviceNotifier` without populating it. 2. `deviceNotifier` instance might be replaced in some cases, causing `onAdded` subscribers to lose any future updates. To fix (1), this commit added the `isPopulated` field in `deviceNotifier` as a more accurate flag to determine if we need to populate it. To fix (2), this commit made `deviceNotifier` a final member in `PolingDeviceDiscovery`.
This commit is contained in:
@@ -97,9 +97,9 @@ String getSizeAsMB(int bytesLength) {
|
||||
/// removed, and calculate a diff of changes when a new list of items is
|
||||
/// available.
|
||||
class ItemListNotifier<T> {
|
||||
ItemListNotifier(): _items = <T>{};
|
||||
ItemListNotifier(): _items = <T>{}, _isPopulated = false;
|
||||
|
||||
ItemListNotifier.from(List<T> items) : _items = Set<T>.of(items);
|
||||
ItemListNotifier.from(List<T> items) : _items = Set<T>.of(items), _isPopulated = true;
|
||||
|
||||
Set<T> _items;
|
||||
|
||||
@@ -111,6 +111,11 @@ class ItemListNotifier<T> {
|
||||
|
||||
List<T> get items => _items.toList();
|
||||
|
||||
bool _isPopulated;
|
||||
|
||||
/// Returns whether the list has been populated.
|
||||
bool get isPopulated => _isPopulated;
|
||||
|
||||
void updateWithNewList(List<T> updatedList) {
|
||||
final Set<T> updatedSet = Set<T>.of(updatedList);
|
||||
|
||||
@@ -118,9 +123,10 @@ class ItemListNotifier<T> {
|
||||
final Set<T> removedItems = _items.difference(updatedSet);
|
||||
|
||||
_items = updatedSet;
|
||||
_isPopulated = true;
|
||||
|
||||
addedItems.forEach(_addedController.add);
|
||||
removedItems.forEach(_removedController.add);
|
||||
addedItems.forEach(_addedController.add);
|
||||
}
|
||||
|
||||
void removeItem(T item) {
|
||||
|
||||
@@ -480,18 +480,15 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
|
||||
|
||||
@protected
|
||||
@visibleForTesting
|
||||
ItemListNotifier<Device>? deviceNotifier;
|
||||
final ItemListNotifier<Device> deviceNotifier = ItemListNotifier<Device>();
|
||||
|
||||
Timer? _timer;
|
||||
|
||||
Future<List<Device>> pollingGetDevices({Duration? timeout});
|
||||
|
||||
void startPolling() {
|
||||
if (_timer == null) {
|
||||
deviceNotifier ??= ItemListNotifier<Device>();
|
||||
// Make initial population the default, fast polling timeout.
|
||||
_timer = _initTimer(null, initialCall: true);
|
||||
}
|
||||
// Make initial population the default, fast polling timeout.
|
||||
_timer ??= _initTimer(null, initialCall: true);
|
||||
}
|
||||
|
||||
Timer _initTimer(Duration? pollingTimeout, {bool initialCall = false}) {
|
||||
@@ -499,7 +496,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
|
||||
return Timer(initialCall ? Duration.zero : _pollingInterval, () async {
|
||||
try {
|
||||
final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout);
|
||||
deviceNotifier!.updateWithNewList(devices);
|
||||
deviceNotifier.updateWithNewList(devices);
|
||||
} on TimeoutException {
|
||||
// Do nothing on a timeout.
|
||||
}
|
||||
@@ -546,32 +543,28 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
|
||||
DeviceDiscoveryFilter? filter,
|
||||
bool resetCache = false,
|
||||
}) async {
|
||||
if (deviceNotifier == null || resetCache) {
|
||||
if (!deviceNotifier.isPopulated || resetCache) {
|
||||
final List<Device> devices = await pollingGetDevices(timeout: timeout);
|
||||
// If the cache was populated while the polling was ongoing, do not
|
||||
// overwrite the cache unless it's explicitly refreshing the cache.
|
||||
if (resetCache) {
|
||||
deviceNotifier = ItemListNotifier<Device>.from(devices);
|
||||
} else {
|
||||
deviceNotifier ??= ItemListNotifier<Device>.from(devices);
|
||||
if (!deviceNotifier.isPopulated || resetCache) {
|
||||
deviceNotifier.updateWithNewList(devices);
|
||||
}
|
||||
}
|
||||
|
||||
// If a filter is provided, filter cache to only return devices matching.
|
||||
if (filter != null) {
|
||||
return filter.filterDevices(deviceNotifier!.items);
|
||||
return filter.filterDevices(deviceNotifier.items);
|
||||
}
|
||||
return deviceNotifier!.items;
|
||||
return deviceNotifier.items;
|
||||
}
|
||||
|
||||
Stream<Device> get onAdded {
|
||||
deviceNotifier ??= ItemListNotifier<Device>();
|
||||
return deviceNotifier!.onAdded;
|
||||
return deviceNotifier.onAdded;
|
||||
}
|
||||
|
||||
Stream<Device> get onRemoved {
|
||||
deviceNotifier ??= ItemListNotifier<Device>();
|
||||
return deviceNotifier!.onRemoved;
|
||||
return deviceNotifier.onRemoved;
|
||||
}
|
||||
|
||||
void dispose() => stopPolling();
|
||||
|
||||
@@ -102,8 +102,6 @@ class IOSDevices extends PollingDeviceDiscovery {
|
||||
return;
|
||||
}
|
||||
|
||||
deviceNotifier ??= ItemListNotifier<Device>();
|
||||
|
||||
// Start by populating all currently attached devices.
|
||||
_updateCachedDevices(await pollingGetDevices());
|
||||
_updateNotifierFromCache();
|
||||
@@ -127,10 +125,8 @@ class IOSDevices extends PollingDeviceDiscovery {
|
||||
|
||||
@visibleForTesting
|
||||
Future<void> onDeviceEvent(XCDeviceEventNotification event) async {
|
||||
final ItemListNotifier<Device>? notifier = deviceNotifier;
|
||||
if (notifier == null) {
|
||||
return;
|
||||
}
|
||||
final ItemListNotifier<Device> notifier = deviceNotifier;
|
||||
|
||||
Device? knownDevice;
|
||||
for (final Device device in notifier.items) {
|
||||
if (device.id == event.deviceIdentifier) {
|
||||
@@ -186,10 +182,8 @@ class IOSDevices extends PollingDeviceDiscovery {
|
||||
/// Updates notifier with devices found in the cache that are determined
|
||||
/// to be connected.
|
||||
void _updateNotifierFromCache() {
|
||||
final ItemListNotifier<Device>? notifier = deviceNotifier;
|
||||
if (notifier == null) {
|
||||
return;
|
||||
}
|
||||
final ItemListNotifier<Device> notifier = deviceNotifier;
|
||||
|
||||
// Device is connected if it has either an observed usb or wifi connection
|
||||
// or it has not been observed but was found as connected in the cache.
|
||||
final List<Device> connectedDevices = _cachedPolledDevices.values.where((Device device) {
|
||||
|
||||
Reference in New Issue
Block a user