From 9b714aa048609dcd7b23717df8578e1e02f1e354 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Thu, 22 May 2025 15:01:58 +0200 Subject: [PATCH] clan-cli: Fix ctrl+c cancelling tasks --- pkgs/clan-app/clan-app.code-workspace | 3 --- pkgs/clan-cli/clan_lib/async_run/__init__.py | 11 ++++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkgs/clan-app/clan-app.code-workspace b/pkgs/clan-app/clan-app.code-workspace index 6261bf7ed..d2696c0e6 100644 --- a/pkgs/clan-app/clan-app.code-workspace +++ b/pkgs/clan-app/clan-app.code-workspace @@ -23,9 +23,6 @@ }, { "path": "../clan-cli/clan_lib" - }, - { - "path": "../webview-ui" } ], "settings": { diff --git a/pkgs/clan-cli/clan_lib/async_run/__init__.py b/pkgs/clan-cli/clan_lib/async_run/__init__.py index 50ed86ab0..94f5db70c 100644 --- a/pkgs/clan-cli/clan_lib/async_run/__init__.py +++ b/pkgs/clan-cli/clan_lib/async_run/__init__.py @@ -133,6 +133,7 @@ class AsyncThread(threading.Thread, Generic[P, R]): self, async_opts: AsyncOpts, condition: threading.Condition, + stop_event: threading.Event, function: Callable[P, R], *args: P.args, **kwargs: P.kwargs, @@ -148,13 +149,14 @@ class AsyncThread(threading.Thread, Generic[P, R]): self.finished = False # Set to True after the thread finishes execution self.condition = condition # Shared condition variable self.async_opts = async_opts + self.stop_event = stop_event # Event to signal cancellation def run(self) -> None: """ Run the function in a separate thread. """ try: - set_async_ctx(self.async_opts.async_ctx) + set_should_cancel(lambda: self.stop_event.is_set()) # Arguments for ParamSpec "P@AsyncThread" are missing self.result = AsyncResult(_result=self.function(*self.args, **self.kwargs)) except Exception as ex: @@ -246,8 +248,11 @@ class AsyncRuntime: msg = f"A task with the name '{opts.tid}' is already running." raise ClanError(msg) + stop_event = threading.Event() # Create and start the new AsyncThread - thread = AsyncThread(opts, self.condition, function, *args, **kwargs) + thread = AsyncThread( + opts, self.condition, stop_event, function, *args, **kwargs + ) self.tasks[opts.tid] = thread thread.start() return AsyncFuture(opts.tid, self) @@ -323,7 +328,7 @@ class AsyncRuntime: """ for name, task in self.tasks.items(): if not task.finished: - set_should_cancel(lambda: True) + task.stop_event.set() log.debug(f"Canceling task {name}")