From ccbd891eeefebe6e8bc8de643b5996e2fff0fe1f Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:55:58 +0700 Subject: [PATCH] fix(library): stop scans when NAS connections are removed Cancel scans when their source is removed or disabled, and retain offline library indexes when a NAS connection is deleted. Stop waiting on cancelled directory listings and consume late results without publishing them. Validation: 95 related tests pass, with 2 SMB fixture tests skipped. WebDAV HTTP listing and streaming pass on Android 17 through a non-loopback address. Targeted analyzer and formatting checks pass. --- .../network_webdav_http_test.dart | 78 ++++++++++++++++ lib/providers/local_library_provider.dart | 50 ++++++++++ .../local_library_provider_scan.dart | 2 + lib/services/network_library_scanner.dart | 37 +++++++- lib/services/network_storage_service.dart | 13 ++- test/library_scan_scheduling_test.dart | 93 +++++++++++++++++++ test/network_library_scanner_test.dart | 40 ++++++++ 7 files changed, 305 insertions(+), 8 deletions(-) create mode 100644 integration_test/network_webdav_http_test.dart diff --git a/integration_test/network_webdav_http_test.dart b/integration_test/network_webdav_http_test.dart new file mode 100644 index 000000000..a47e00678 --- /dev/null +++ b/integration_test/network_webdav_http_test.dart @@ -0,0 +1,78 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:mornye/services/network_storage_service.dart'; + +// Android 17 requires ACCESS_LOCAL_NETWORK to be granted before this transport +// test. It deliberately exercises HTTP independently of the permission prompt. +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + testWidgets( + 'WebDAV HTTP lists and reads through a non-loopback Android address', + (tester) async { + final interfaces = await NetworkInterface.list( + type: InternetAddressType.IPv4, + ); + final address = interfaces + .expand((value) => value.addresses) + .firstWhere((value) => !value.isLoopback && !value.isLinkLocal); + final server = await HttpServer.bind(InternetAddress.anyIPv4, 0); + addTearDown(() => server.close(force: true)); + final methods = []; + server.listen((request) async { + methods.add(request.method); + expect( + request.headers.value('authorization'), + 'Basic ${base64Encode(utf8.encode('fixture:secret'))}', + ); + await request.drain(); + if (request.method == 'PROPFIND') { + request.response.statusCode = 207; + request.response.write( + '' + '/music/Song.flac' + '4' + 'HTTP/1.1 200 OK' + '', + ); + } else { + request.response.statusCode = 206; + request.response.headers.set('Content-Range', 'bytes 0-3/4'); + request.response.contentLength = 4; + request.response.add([1, 2, 3, 4]); + } + await request.response.close(); + }); + final storage = NetworkStorageService( + read: () async => null, + write: (_) async {}, + // Isolate HTTP policy from the separate runtime LAN permission gate. + ensureAccess: (_) async {}, + ); + addTearDown(storage.dispose); + final connection = NetworkConnection( + id: 'fixture', + name: 'HTTP NAS', + protocol: NetworkProtocol.webdav, + address: 'http://${address.address}:${server.port}/music/', + username: 'fixture', + password: 'secret', + ); + final entries = await storage.list(connection); + expect(entries.single.name, 'Song.flac'); + await storage.save(connection); + final url = await storage.resolve('network://fixture/Song.flac'); + final client = HttpClient(); + addTearDown(() => client.close(force: true)); + final request = await client.getUrl(Uri.parse(url)); + request.headers.set('Range', 'bytes=0-3'); + final response = await request.close(); + expect(response.statusCode, 206); + expect(await response.expand((value) => value).toList(), [1, 2, 3, 4]); + expect(methods, ['PROPFIND', 'GET']); + }, + ); +} diff --git a/lib/providers/local_library_provider.dart b/lib/providers/local_library_provider.dart index f96f1a1fd..b7672c45d 100644 --- a/lib/providers/local_library_provider.dart +++ b/lib/providers/local_library_provider.dart @@ -8,6 +8,7 @@ import 'package:mornye/providers/settings_provider.dart'; import 'package:mornye/services/library_cleanup.dart'; import 'package:mornye/services/library_database.dart'; import 'package:mornye/services/network_library_scanner.dart'; +import 'package:mornye/services/network_storage_service.dart'; import 'package:mornye/services/notification_service.dart'; import 'package:mornye/services/platform_bridge.dart'; import 'package:mornye/utils/logger.dart'; @@ -150,10 +151,14 @@ class LocalLibraryNotifier extends Notifier { LocalLibraryNotifier({ LibraryDatabase? database, LocalLibraryAvailabilityProbe? availabilityProbe, + NetworkStorageService? networkStorage, }) : _db = database ?? LibraryDatabase.instance, + _networkStorage = networkStorage ?? NetworkStorageService.instance, _availabilityProbe = availabilityProbe; final LibraryDatabase _db; + final NetworkStorageService _networkStorage; + void Function()? _unsubscribeNetworkRemoval; final LocalLibraryAvailabilityProbe? _availabilityProbe; final NotificationService _notificationService = NotificationService(); static const _progressPollingInterval = Duration(milliseconds: 350); @@ -184,6 +189,8 @@ class LocalLibraryNotifier extends Notifier { bool _scanInProgress = false; Future? _scanCompletion; NetworkLibraryScanner? _networkScanner; + String? _activeScanPath; + String? _activeScanSourceId; String? _nativeLibraryRequestId; final Set _pendingReconnectScans = {}; bool _drainingReconnectScans = false; @@ -204,7 +211,11 @@ class LocalLibraryNotifier extends Notifier { @override LocalLibraryState build() { + _unsubscribeNetworkRemoval = _networkStorage.connectionRemovals.subscribe( + _onNetworkConnectionRemoved, + ); ref.onDispose(() { + _unsubscribeNetworkRemoval?.call(); _scanCancelRequested = true; _scanPauseRequested = false; _pendingReconnectScans.clear(); @@ -411,6 +422,7 @@ class LocalLibraryNotifier extends Notifier { .where((entry) => entry.id == sourceId) .firstOrNull; _sourceVersion++; + if (!enabled) await _cancelSourceScan(sourceId); await _db.updateSourceState(sourceId, enabled: enabled); await _refreshSummaryFromStorage(); if (enabled && @@ -423,11 +435,45 @@ class LocalLibraryNotifier extends Notifier { Future removeSource(String sourceId) async { _sourceVersion++; + _pendingReconnectScans.remove(sourceId); + await _cancelSourceScan(sourceId); await _db.removeSource(sourceId); await _setScanFailures(sourceId, null); await _refreshSummaryFromStorage(); } + Future _cancelSourceScan(String sourceId) async { + if (!_scanInProgress || _activeScanSourceId != sourceId) return; + final completion = _scanCompletion; + final waitForScan = state.isScanning; + await cancelScan(); + // Preflight can still be awaiting an availability probe. Its source + // version checks discard the result after this source change. + if (waitForScan && completion != null) await completion; + } + + Future _onNetworkConnectionRemoved(String connectionId) async { + if (!ref.mounted) return; + bool matches(String path) { + final uri = Uri.tryParse(path); + return uri?.scheme == 'network' && uri?.host == connectionId; + } + + _sourceVersion++; + if (matches(_activeScanPath ?? '') && _activeScanSourceId != null) { + await _cancelSourceScan(_activeScanSourceId!); + } + if (!ref.mounted) return; + final sources = await _db.getSources(); + if (!ref.mounted) return; + for (final source in sources.where((source) => matches(source.path))) { + _pendingReconnectScans.remove(source.id); + await _db.updateSourceState(source.id, available: false); + if (!ref.mounted) return; + } + await _refreshSummaryFromStorage(); + } + Future refreshSourceAvailability({bool scanReconnected = false}) { _availabilityRequestVersion++; _scanReconnectedOnRefresh |= scanReconnected; @@ -491,6 +537,8 @@ class LocalLibraryNotifier extends Notifier { final completion = Completer(); _scanCompletion = completion.future; _scanInProgress = true; + _activeScanPath = folderPath; + _activeScanSourceId = sourceId; if (sourceId != null) _pendingReconnectScans.remove(sourceId); _scanCancelRequested = false; _scanPauseRequested = false; @@ -514,6 +562,8 @@ class LocalLibraryNotifier extends Notifier { ); } _scanInProgress = false; + _activeScanPath = null; + _activeScanSourceId = null; _scanCompletion = null; _nativeLibraryRequestId = null; completion.complete(); diff --git a/lib/providers/local_library_provider_scan.dart b/lib/providers/local_library_provider_scan.dart index 01df0d479..712718bb0 100644 --- a/lib/providers/local_library_provider_scan.dart +++ b/lib/providers/local_library_provider_scan.dart @@ -56,6 +56,7 @@ extension _LocalLibraryScan on LocalLibraryNotifier { required void Function(int processed, int errors, String name) onProgress, }) async { final scanner = NetworkLibraryScanner( + storage: _networkStorage, readCachedRows: (paths) => _db.readNetworkScanRows(sourceId, paths), ); _networkScanner = scanner; @@ -181,6 +182,7 @@ extension _LocalLibraryScan on LocalLibraryNotifier { } } + _activeScanSourceId = activeSourceId; final available = await _isPathAvailable( folderPath, bookmark: iosBookmark, diff --git a/lib/services/network_library_scanner.dart b/lib/services/network_library_scanner.dart index 66796a671..9c4ab3668 100644 --- a/lib/services/network_library_scanner.dart +++ b/lib/services/network_library_scanner.dart @@ -52,14 +52,44 @@ class NetworkLibraryScanner { SubsonicClient? _activeSubsonic; bool _running = false; bool _cancelled = false; + Completer>? _pendingListing; /// Stop scan-owned native reads even while scan() is awaiting their result. Future cancelPending() async { _cancelled = true; + final listing = _pendingListing; + if (listing != null && !listing.isCompleted) { + listing.completeError(StateError('Network library scan cancelled')); + } _activeSubsonic?.close(); await _activeMetadata?.cancelPending(); } + Future> _list( + NetworkConnection connection, + String path, + ) async { + if (_cancelled) throw StateError('Network library scan cancelled'); + final listing = _pendingListing = Completer>(); + // A directory read has its own transport timeout. Stop waiting on it + // immediately when cancelled, and consume any late result or error. + unawaited( + Future.sync(() => _storage.list(connection, path)).then( + (entries) { + if (!listing.isCompleted) listing.complete(entries); + }, + onError: (Object error, StackTrace stack) { + if (!listing.isCompleted) listing.completeError(error, stack); + }, + ), + ); + try { + return await listing.future; + } finally { + if (identical(_pendingListing, listing)) _pendingListing = null; + } + } + Future scan( String source, { bool forceFullScan = false, @@ -134,7 +164,7 @@ class NetworkLibraryScanner { } await checkpoint(); // Fail before creating scan output if the root cannot be listed. - List? first = await _storage.list(connection, root); + List? first = await _list(connection, root); await checkpoint(); List? preserveMissingPaths = []; void preserve(String path) { @@ -204,13 +234,12 @@ class NetworkLibraryScanner { } List? entries; try { - entries = folder == root - ? first! - : await _storage.list(connection, folder); + entries = folder == root ? first! : await _list(connection, folder); // The first listing can be very large. Release it before descending // into children instead of retaining it for the entire scan. first = null; } catch (error) { + await checkpoint(); errors++; preserve(folder); _log.e('Network folder listing failed [$folder]', error); diff --git a/lib/services/network_storage_service.dart b/lib/services/network_storage_service.dart index 1a9e10447..df004aa6a 100644 --- a/lib/services/network_storage_service.dart +++ b/lib/services/network_storage_service.dart @@ -16,6 +16,7 @@ import 'package:mornye/services/network_certificate.dart'; import 'package:mornye/services/network_smb_client.dart'; import 'package:mornye/services/platform_bridge.dart'; import 'package:mornye/services/secure_storage_options.dart'; +import 'package:mornye/services/source_deletion_events.dart'; import 'package:xml/xml.dart'; part 'network_storage_upload.dart'; @@ -285,6 +286,9 @@ class NetworkStorageService { final Future Function() _read; final Future Function(String) _write; final Future Function(Uri) _ensureAccess; + + /// Owners stop scans before removing the connection finishes. + final connectionRemovals = SourceDeletionEvents(); static const timeout = Duration(seconds: 20); static const _probeTimeout = Duration(seconds: 3); static const _probeReuse = Duration(seconds: 10); @@ -363,7 +367,8 @@ class NetworkStorageService { await _write(jsonEncode(next.map((c) => c.toJson()).toList())); _connections = List.unmodifiable(next); _routes.removeWhere((_, route) => route.id == id); - _closeReadClients(id); + _closeReadClients(id, force: true); + await connectionRemovals.publish(id); }); Future connection(String id) async => @@ -563,10 +568,10 @@ class NetworkStorageService { return client; } - void _closeReadClients(String id) { + void _closeReadClients(String id, {bool force = false}) { for (final key in _readClients.keys.where((key) => key.id == id).toList()) { - // Let in-flight reads finish; new requests use the updated configuration. - _readClients.remove(key)!.close(); + // Edits let existing reads finish; removal aborts the old connection. + _readClients.remove(key)!.close(force: force); } } diff --git a/test/library_scan_scheduling_test.dart b/test/library_scan_scheduling_test.dart index d97d13cd5..8395a75fe 100644 --- a/test/library_scan_scheduling_test.dart +++ b/test/library_scan_scheduling_test.dart @@ -14,6 +14,7 @@ import 'package:mornye/providers/settings_provider.dart'; import 'package:mornye/services/library_database.dart'; import 'package:mornye/services/platform_bridge.dart'; import 'package:mornye/services/notification_service.dart'; +import 'package:mornye/services/network_storage_service.dart'; import 'package:mornye/utils/local_library_scan_prefs.dart'; class _Settings extends SettingsNotifier { @@ -24,6 +25,22 @@ class _Settings extends SettingsNotifier { Future ensureLoaded() async {} } +class _ListingStorage extends NetworkStorageService { + _ListingStorage() : super(read: () async => null, write: (_) async {}); + + final started = Completer(); + final listing = Completer>(); + + @override + Future> list( + NetworkConnection connection, [ + String path = '', + ]) { + if (!started.isCompleted) started.complete(); + return listing.future; + } +} + class _Database implements LibraryDatabase { _Database(this.sources); @@ -135,6 +152,7 @@ void main() { late List calls; late List notificationCalls; var completeOnCancel = false; + late _ListingStorage storage; setUp(() async { debugDefaultTargetPlatformOverride = TargetPlatform.android; @@ -158,6 +176,7 @@ void main() { calls = []; notificationCalls = []; completeOnCancel = false; + storage = _ListingStorage(); messenger.setMockMethodCallHandler(paths, (_) async => directory.path); messenger.setMockMethodCallHandler(notifications, (call) async { notificationCalls.add(call); @@ -183,6 +202,7 @@ void main() { localLibraryProvider.overrideWith( () => LocalLibraryNotifier( database: database, + networkStorage: storage, availabilityProbe: (path, {bookmark}) => probe(path, bookmark: bookmark), ), @@ -195,6 +215,10 @@ void main() { tearDown(() async { container.dispose(); + if (storage.started.isCompleted && !storage.listing.isCompleted) { + storage.listing.complete([]); + } + await storage.dispose(); for (final (_, response) in scans) { if (!response.isCompleted) response.complete('{"cancelled":true}'); } @@ -219,6 +243,7 @@ void main() { localLibraryProvider.overrideWith( () => LocalLibraryNotifier( database: database, + networkStorage: storage, availabilityProbe: probe, ), ), @@ -228,6 +253,74 @@ void main() { await _waitFor(() => !container.read(localLibraryProvider).isLoading); } + test( + 'removing a NAS cancels its blocked listing and retains its offline index', + () async { + const connection = NetworkConnection( + id: 'nas', + name: 'NAS', + protocol: NetworkProtocol.smb, + address: 'smb://nas.test/music/', + ); + await storage.save(connection); + const path = 'network://nas/'; + database.sources[0] = LocalLibrarySource( + id: 'active', + path: path, + displayName: 'NAS', + available: true, + ); + available[path] = true; + final scan = library.startSourceScan('active', forceFullScan: true); + await storage.started.future.timeout(const Duration(seconds: 5)); + expect(container.read(localLibraryProvider).isScanning, isTrue); + + available[path] = false; + await storage.remove('nas').timeout(const Duration(seconds: 1)); + await scan; + final state = container.read(localLibraryProvider); + expect(state.isScanning, isFalse); + expect(state.scanWasCancelled, isTrue); + expect( + state.sources.firstWhere((s) => s.id == 'active').available, + isFalse, + ); + expect( + storage.listing.isCompleted, + isFalse, + reason: 'Cancellation must not wait for the old NAS response', + ); + storage.listing.completeError( + const SocketException('Old connection closed'), + ); + await Future.delayed(Duration.zero); + }, + ); + + test( + 'removing the active library source waits for its scan to stop', + () async { + completeOnCancel = true; + final scan = library.startSourceScan('active'); + await _waitFor(() => scans.isNotEmpty); + await library.removeSource('active').timeout(const Duration(seconds: 1)); + await scan; + expect(calls, contains('cancelLibraryScan')); + expect(container.read(localLibraryProvider).isScanning, isFalse); + expect(database.sources.any((s) => s.id == 'active'), isFalse); + }, + ); + + test('removing another connection keeps the active scan running', () async { + final scan = library.startSourceScan('active'); + await _waitFor(() => scans.isNotEmpty); + await storage.remove('another-nas'); + expect(calls, isNot(contains('cancelLibraryScan'))); + expect(container.read(localLibraryProvider).isScanning, isTrue); + scans.single.$2.complete('{"cancelled":true}'); + await scan; + }); + test( 'Android crash details are recovered and retained without manual logs', () async { diff --git a/test/network_library_scanner_test.dart b/test/network_library_scanner_test.dart index 0916a3529..0e62d407d 100644 --- a/test/network_library_scanner_test.dart +++ b/test/network_library_scanner_test.dart @@ -38,6 +38,23 @@ class _Storage extends NetworkStorageService { } } +class _BlockedFolderStorage extends _Storage { + _BlockedFolderStorage() + : super({ + '': [const NetworkEntry('Album/', 'Album', directory: true)], + }); + + final started = Completer(); + final listing = Completer>(); + + @override + Future> list(NetworkConnection c, [String path = '']) { + if (path != 'Album/') return super.list(c, path); + started.complete(); + return listing.future; + } +} + /// A storage without saved connections whose `virtual` host lists songs. class _VirtualStorage extends NetworkStorageService { _VirtualStorage(List songs) { @@ -174,6 +191,29 @@ void main() { }, ); + test( + 'cancelling a blocked child listing discards staging and its late error', + () async { + final storage = _BlockedFolderStorage(); + addTearDown(storage.dispose); + final reader = scanner(storage, (_) async => {}); + final scan = reader.scan( + 'network://nas/', + checkpoint: () async {}, + onProgress: (_, _, _) {}, + ); + final stopped = expectLater(scan, throwsStateError); + await storage.started.future; + await reader.cancelPending(); + await stopped.timeout(const Duration(seconds: 1)); + await expectNoOutput(); + storage.listing.completeError( + const SocketException('Late listing failure'), + ); + await pumpEventQueue(); + }, + ); + test( 'directory-scoped deduplication rejects cross-folder listing entries', () async {