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.
This commit is contained in:
zarzet committed 2026-10-29 00:55:58 +07:00
1 parent a940b07911
commit ccbd891eee
7 files changed
+305 -8

No files matched your search

@@ -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 = <String>[];
server.listen((request) async {
methods.add(request.method);
expect(
request.headers.value('authorization'),
'Basic ${base64Encode(utf8.encode('fixture:secret'))}',
);
await request.drain<void>();
if (request.method == 'PROPFIND') {
request.response.statusCode = 207;
request.response.write(
'<d:multistatus xmlns:d="DAV:">'
'<d:response><d:href>/music/Song.flac</d:href><d:propstat><d:prop>'
'<d:getcontentlength>4</d:getcontentlength></d:prop>'
'<d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>'
'</d:multistatus>',
);
} 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']);
},
);
}
+50
View File
@@ -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<LocalLibraryState> {
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<LocalLibraryState> {
bool _scanInProgress = false;
Future<void>? _scanCompletion;
NetworkLibraryScanner? _networkScanner;
String? _activeScanPath;
String? _activeScanSourceId;
String? _nativeLibraryRequestId;
final Set<String> _pendingReconnectScans = {};
bool _drainingReconnectScans = false;
@@ -204,7 +211,11 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
@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<LocalLibraryState> {
.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<LocalLibraryState> {
Future<void> removeSource(String sourceId) async {
_sourceVersion++;
_pendingReconnectScans.remove(sourceId);
await _cancelSourceScan(sourceId);
await _db.removeSource(sourceId);
await _setScanFailures(sourceId, null);
await _refreshSummaryFromStorage();
}
Future<void> _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<void> _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<void> refreshSourceAvailability({bool scanReconnected = false}) {
_availabilityRequestVersion++;
_scanReconnectedOnRefresh |= scanReconnected;
@@ -491,6 +537,8 @@ class LocalLibraryNotifier extends Notifier<LocalLibraryState> {
final completion = Completer<void>();
_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<LocalLibraryState> {
);
}
_scanInProgress = false;
_activeScanPath = null;
_activeScanSourceId = null;
_scanCompletion = null;
_nativeLibraryRequestId = null;
completion.complete();
@@ -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,
+33 -4
View File
@@ -52,14 +52,44 @@ class NetworkLibraryScanner {
SubsonicClient? _activeSubsonic;
bool _running = false;
bool _cancelled = false;
Completer<List<NetworkEntry>>? _pendingListing;
/// Stop scan-owned native reads even while scan() is awaiting their result.
Future<void> 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<NetworkEntry>> _list(
NetworkConnection connection,
String path,
) async {
if (_cancelled) throw StateError('Network library scan cancelled');
final listing = _pendingListing = Completer<List<NetworkEntry>>();
// 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<void>(
(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<LibraryScanNDJSONFile> 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<NetworkEntry>? first = await _storage.list(connection, root);
List<NetworkEntry>? first = await _list(connection, root);
await checkpoint();
List<String>? preserveMissingPaths = [];
void preserve(String path) {
@@ -204,13 +234,12 @@ class NetworkLibraryScanner {
}
List<NetworkEntry>? 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);
+9 -4
View File
@@ -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<String?> Function() _read;
final Future<void> Function(String) _write;
final Future<void> 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<NetworkConnection> 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);
}
}
+93
View File
@@ -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<void> ensureLoaded() async {}
}
class _ListingStorage extends NetworkStorageService {
_ListingStorage() : super(read: () async => null, write: (_) async {});
final started = Completer<void>();
final listing = Completer<List<NetworkEntry>>();
@override
Future<List<NetworkEntry>> 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<String> calls;
late List<MethodCall> 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<void>.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 {
+40
View File
@@ -38,6 +38,23 @@ class _Storage extends NetworkStorageService {
}
}
class _BlockedFolderStorage extends _Storage {
_BlockedFolderStorage()
: super({
'': [const NetworkEntry('Album/', 'Album', directory: true)],
});
final started = Completer<void>();
final listing = Completer<List<NetworkEntry>>();
@override
Future<List<NetworkEntry>> 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<NetworkVirtualSong> 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 {