Skip to content

Commit 5fe30dc

Browse files
fix: threaded task is_current() will return False after cancelling.
This was the case for async tasks, not yet for threaded tasks.
1 parent 86fc2ad commit 5fe30dc

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

solara/tasks.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ def __init__(self, function: Callable[P, R], key: str):
304304
self.__qualname__ = function.__qualname__
305305
self.function = function
306306
self.lock = threading.Lock()
307+
self._local = threading.local()
307308

308309
def cancel(self) -> None:
309310
if self._cancel:
@@ -343,12 +344,16 @@ def cancel():
343344
current_thread.start()
344345

345346
def is_current(self):
347+
cancel_event = getattr(self._local, "cancel_event", None)
348+
if cancel_event is not None and cancel_event.is_set():
349+
return False
346350
return self._current_thread == threading.current_thread()
347351

348352
def _run(self, _last_finished_event, previous_thread: Optional[threading.Thread], cancel_event, args, kwargs) -> None:
349353
# use_thread has this as default, which can make code run 10x slower
350354
intrusive_cancel = False
351355
wait_on_previous = False
356+
self._local.cancel_event = cancel_event
352357

353358
def runner():
354359
if wait_on_previous:
@@ -405,7 +410,7 @@ def runner():
405410
# this means this thread is cancelled not be request, but because
406411
# a new thread is running, we can ignore this
407412
finally:
408-
if self.is_current():
413+
if self._current_thread == threading.current_thread():
409414
self.running_thread = None
410415
logger.info("thread done!")
411416
if cancel_event.is_set():

0 commit comments

Comments
 (0)