Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Immediate Worker/Isolate Shutdown and Call Cancellation #53

Open
a-h-mzd opened this issue Dec 12, 2024 · 4 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@a-h-mzd
Copy link

a-h-mzd commented Dec 12, 2024

I would like to request a new feature that allows us to immediately stop workers/isolates and cancel any in-progress calls. Currently, when we attempt to shut down a worker, it completes its ongoing tasks before actually exiting. This creates situations where we cannot quickly recover or free up resources, particularly in scenarios where graceful shutdowns are not desirable or feasible.

Use Case:

  • Handling error states or timeouts where hanging processes need to be forcibly terminated.
  • When a system or service needs to be quickly restarted or reconfigured.
  • Improving system responsiveness and reliability by ensuring that stuck or long-running operations don’t hold up critical resources.

Proposed Behavior:
Introduce a method (e.g., forceStopWorkers() or shutdownImmediately()) that:

  1. Terminates worker/isolates without waiting for ongoing calls to complete.
  2. Cancels any currently executing calls, ensuring that no callbacks or promise resolutions occur after termination.
  3. Returns a status or error code indicating that the termination was forced.
@a-h-mzd
Copy link
Author

a-h-mzd commented Dec 12, 2024

This functionality can easily be reproduced.

Steps to reproduce:

  1. Create an app with hello world example from the package.

  2. Add a 4 second wait time before returning 'Hello, $name!'

    hello_world.dart
      @SquadronService(baseUrl: '~/workers', targetPlatform: TargetPlatform.all)
      base class HelloWorld {
        @SquadronMethod()
        FutureOr<String> hello([String? name]) async {
          await Future.delayed(const Duration(seconds: 4));
          name = name?.trim() ?? 'World';
          return 'Hello, $name!';
        }
      }
  3. Create a worker and call the hello function printing the result but stopping the working mid call.

    code
      final worker = HelloWorldWorker();
      Future.delayed(const Duration(seconds: 2), worker.stop);
      final message = await worker.hello();
      print(message);
  4. Observe that worker.hello() succeeds even though the worker was stopped.

@d-markey d-markey self-assigned this Dec 12, 2024
@d-markey d-markey added the enhancement New feature or request label Dec 12, 2024
@d-markey
Copy link
Owner

Hello,

This is something that can already be done via the PlatformThreadHook callback. With this callback, you can capture the thread (Isolate or Web Worker) so you can decide to kill it (Isolate.kill() or Worker.terminate()). You'll need a bit of conditional imports if you need both platforms but you can already manage it. You should stop the worker beforehand, to make sure Squadron knows the worker cannot be used anymore.

But it's an interesting feature request, I'll keep that in mind for a future release :-)

@d-markey
Copy link
Owner

Just one remark however, in the case of your example: I'm not quite sure what would happen... final message = await worker.hello(); might never complete. So yes, probably better to wait for a proper implementation in Squadron.

@d-markey
Copy link
Owner

You can upgrade to 6.1.4 and try this:

  final worker = HelloWorldWorker();
  Future.delayed(const Duration(seconds: 2), worker.terminate);
  final message = await worker.hello();
  print(message);

terminate() is provided by IWorker, so it's also available on worker pools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants