How to detect if solara server has been aborted? #929
-
Hi Guys,
It looks great when I run it then close the page in the browser. The component gets destroyed and the client is closed properly.
However when I abort solara server before the page is closed it gets stuck, in that case I have to find python process in task manager and kill it:
I have been playing around with solara.use_task() before and examined this exact same behavior. If I have a function/coro that runs an infinite loop there is no way to detect that the server had been aborted. I tried checking task.is_current, task.cancelled, asyncio.get_running_loop, all of them show that the task and the loop is still fine and running. No signs of any cancellation. I would also expect my component to get destroyed (and use_effect cleanup to run) when server is aborted, but that's not the case either. So, is there a way to listen for server shutdown and cleanup my open connections? Any help is appreciated! Thanks a lot in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
One more thing I discovered earlier. If I use solara.use_thread() to run my function threaded, then it gets terminated on server abort. So that could be a workaround to have this whole stuff wrapped in use_thread. |
Beta Was this translation helpful? Give feedback.
-
Some further trial and error... Try registering a function with atexit module
The function only gets called when solara server can abort normally (when the component is already destroyed and binance client is closed). Does not help in my case, but at least, good to know. Studying the threads and trying to run code on main thread So next experiment is:
Hm, AnyIO. So I just need to find a way to run code synchronized from an AnyIO WorkerThread on the main thread. Should be easy, lol. And it turns out it actually is freakin' easy!
Result:
Next up... |
Beta Was this translation helpful? Give feedback.
-
This is where I finally landed It works now, but I'm not sure if this is the best way. I think it would be good to be able to register custom callbacks to Uvicorn's SIGINT handler. Maybe it is possible, I'll have to look into Uvicorn for that. Full code:
Result:
|
Beta Was this translation helpful? Give feedback.
-
And now that I know what I am looking for I found this: It all makes sense now and I like the solution in the above answer a lot more than what I came up with :) |
Beta Was this translation helpful? Give feedback.
-
The problem happens because they start a thread:
If you managed to override/monkeypatch to set the daemon flag before the thread starts, you will not have this problem. However, we can see if we can more gracefully shut down. I'll explore that a bit based on your info above. |
Beta Was this translation helpful? Give feedback.
-
Should be fixed by #930 |
Beta Was this translation helpful? Give feedback.
This one. Threads cannot be stopped.
We might do a release tomorrow, or very soon, I hope we can get this in.