Repository navigation
Is there a hook before worker cancellation during shutdown? #6760
|
Hi! I'm using Textual 8.2.8 and ran into a shutdown lifecycle question. My app runs background calculations that can continue in a thread after their asyncio worker has been cancelled. I need to revoke their permission to publish results before Textual starts cancelling workers and shutting down. From looking at the source, Is there a supported way to run a synchronous cleanup hook before worker cancellation, including when the message-loop task is cancelled directly ( I realize a hook during cancellation would run after the actual I'm not trying to stop running threads, just prevent them from publishing stale results. If there's no existing hook, would adding one be reasonable? I've seen #4510 about cooperative thread cancellation, but my question is specifically about shutdown ordering and a synchronous pre-teardown hook. Thanks! |
Replies: 2 comments
|
There's no public hook that runs before
One ordering difference I found: under Do the final check on the event loop thread rather than in your thread. import asyncio
from textual.app import App
from textual.worker import Worker, get_current_worker
class MyApp(App):
def on_mount(self) -> None:
self.run_worker(self.calc())
async def calc(self) -> None:
worker = get_current_worker()
await asyncio.to_thread(self.heavy, worker) # the thread outlives a cancelled worker
def heavy(self, worker: Worker) -> None:
result = ... # long calculation
self.publish(worker, result)
def publish(self, worker: Worker, result) -> None:
def apply() -> None:
# Runs on the event loop thread, same as cancel_all() and _shutdown().
if worker.is_cancelled or not self.is_running:
return # stale result, drop it
... # update widgets
try:
self.call_from_thread(apply)
except RuntimeError:
pass # app already stoppedI ran this on 8.2.8 with a thread that finishes 0.3 s after shutdown starts:
On adding a hook: since |
|
Thanks for the quick and detailed response! Really appreciate you taking the time to test all those shutdown scenarios. Your explanation makes sense. Moving the final publication check to the event-loop thread looks like a much simpler approach than introducing another shutdown hook. I still have some request ownership and executor lifecycle details to work through on my side, but this gives me a much clearer direction. Thanks again for your help! |
There's no public hook that runs before
cancel_all()in 8.2.8, but what Textual already guarantees is enough to gate publishing:self.workers.cancel_all()is in thefinally:ofApp._process_messages, so it runs however the loop ends:exit(), an exception, or the message-loop task being cancelled directly.Worker.cancel()is synchronous. It setsis_cancelled(and the threadingcancelled_event) in the same call that doestask.cancel(), so on the event loop the flag is set the moment cancellation is requested.One ordering difference I found: under
run_test()the context manager callsapp._shutdown()itself on exit, soon_unmountruns beforecancel_all()there, while underrun()/run_async()