sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Windows Directory.watch SocketException on directory delete

Open davidmorgan opened this issue 3 days ago • 0 comments

When the watched directory is deleted, a SocketException might be thrown.

I think it would be better just to close it, there isn't really any error and the docs say the stream closes if "The FileSystemEntity being watched is deleted."

Separately to all this, on 3.10 stable it's thrown outside the event stream, on 3.11 beta it arrives on the event stream which is already an improvement :)

import 'dart:io';

Future<void> main() async {
  final temp = Directory.systemTemp.createTempSync();

  final dir1 = Directory('${temp.path}\\dir1')..createSync();
  dir1.watch(recursive: true).listen(
    (e) => print('1. data $e'),
    onError: (e, s) => print('1. onError: $e $s'),
    onDone: () => print('1. onDone'));
  await Future.delayed(Duration(milliseconds: 200));
  dir1.deleteSync();
  await Future.delayed(Duration(seconds: 1));
}

prints (on 3.11.0-17)

onError: FileSystemException: Directory watcher failed due to: SocketException: Access is denied.
 (OS Error: Access is denied.
, errno = 5), port = 0, path = 'C:\Users\davidmorgan\AppData\Local\Temp\2043dd2b\dir'
onDone

davidmorgan avatar Dec 08 '25 08:12 davidmorgan