Clean up after spawned isolate when process receives SIGTERM or similar signal

Hi,
In my program I have several spawned sub isolates which are handling web socket connections to external services – and I want to be able to properly close these connections in case of receiving SIGINT, SIGTERM or similar signal.

Now I’m unable to do that because at the time main process is receiving signal of termination sub isolates are killed without any chance for me to properly close connections and clean up everything on sub isolates.

Dart version: Dart VM version: 2.9.0-18.0.dev (dev) (Mon Jun 22 16:07:30 2020 +0200) on "linux_x64"

1 possible answer(s) on “Clean up after spawned isolate when process receives SIGTERM or similar signal

  1. The Dart VM generally terminates when the root isolate ends. It’s the job of the root isolate code to notify the other isolates it has started, and wait for them to be done.

    You can choose to watch the signals, so they won’t terminate the process immediately, then send shutdown messages to your isolates.

      ProcessSignal.sigint.watch.forEach(() {
         if (!_isShuttingDown) _startShutdown()
      });

    Other than that, you can hope that the VM adds some way to have isolates surviving the main isolate shutdown.