dart-lang / watcher

A file system watcher library for Dart.
https://pub.dev/packages/watcher
BSD 3-Clause "New" or "Revised" License
138 stars 35 forks source link

Watcher prevents child processes from being killed #137

Open Ehesp opened 1 year ago

Ehesp commented 1 year ago

When using watcher (and typically the hotreloader package), processes never seem to be killed whilst watch events are being listened too. A minimal setup of this looks like:

Process process = await Process.start('some-long-lived-process');

final watcher = DirectoryWatcher(p.join(Directory.current.path, 'lib'));

watcher.events.listen((event) async {
  final result = process.exit();
  print(result); // true
});

Looking at activity monitor, the process spawned is never killed. I've tried a combination of signals, but upon investigation the process simply never exists whilst the watcher is active. This also includes trying to exist outside of the listener.

My current workaround is to kill the watcher on an event, kill the process and restart the flow. Something like:

while (true) {
  Process process = await Process.start('some-long-lived-process');

  final watcher = DirectoryWatcher(p.join(Directory.current.path, 'lib'));
  final completer = Completer();
  StreamSubscription? watcherSubscription;

  watcherSubscription = watcher.events.listen((event) async {
    if (!completer.isCompleted) completer.complete();
  });

  await completer.future; // wait for an event
  await watcherSubscription.cancel(); // stop the watcher
  process.kill(); // kill the process

  // while loop starts it again
}

Probably worth noting I'm on MacOS here.

Solido commented 1 year ago

Maybe check that sdt out and error buffers are flushed?

Ehesp commented 1 year ago

Fairly sure I tried that, but will double check again and report back.