Merge pull request 'clan-cli: Fix ctrl+c cancelling tasks' (#3746) from Qubasa/clan-core:fix_ctrl_c into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3746
This commit is contained in:
Luis Hebendanz
2025-05-22 13:14:30 +00:00
2 changed files with 8 additions and 6 deletions

View File

@@ -23,9 +23,6 @@
}, },
{ {
"path": "../clan-cli/clan_lib" "path": "../clan-cli/clan_lib"
},
{
"path": "../webview-ui"
} }
], ],
"settings": { "settings": {

View File

@@ -133,6 +133,7 @@ class AsyncThread(threading.Thread, Generic[P, R]):
self, self,
async_opts: AsyncOpts, async_opts: AsyncOpts,
condition: threading.Condition, condition: threading.Condition,
stop_event: threading.Event,
function: Callable[P, R], function: Callable[P, R],
*args: P.args, *args: P.args,
**kwargs: P.kwargs, **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.finished = False # Set to True after the thread finishes execution
self.condition = condition # Shared condition variable self.condition = condition # Shared condition variable
self.async_opts = async_opts self.async_opts = async_opts
self.stop_event = stop_event # Event to signal cancellation
def run(self) -> None: def run(self) -> None:
""" """
Run the function in a separate thread. Run the function in a separate thread.
""" """
try: 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 # Arguments for ParamSpec "P@AsyncThread" are missing
self.result = AsyncResult(_result=self.function(*self.args, **self.kwargs)) self.result = AsyncResult(_result=self.function(*self.args, **self.kwargs))
except Exception as ex: except Exception as ex:
@@ -246,8 +248,11 @@ class AsyncRuntime:
msg = f"A task with the name '{opts.tid}' is already running." msg = f"A task with the name '{opts.tid}' is already running."
raise ClanError(msg) raise ClanError(msg)
stop_event = threading.Event()
# Create and start the new AsyncThread # 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 self.tasks[opts.tid] = thread
thread.start() thread.start()
return AsyncFuture(opts.tid, self) return AsyncFuture(opts.tid, self)
@@ -323,7 +328,7 @@ class AsyncRuntime:
""" """
for name, task in self.tasks.items(): for name, task in self.tasks.items():
if not task.finished: if not task.finished:
set_should_cancel(lambda: True) task.stop_event.set()
log.debug(f"Canceling task {name}") log.debug(f"Canceling task {name}")