Ways to detect session stopped or is stopping

I have a couple of async coros in my streamlit app that consist baisically of a while True: loop. They handle events, communicate with each other and manipulate the streamlit session.

This works nicely but I can no longer stop my streamlit session. The problem is, that the coros continue to run in the while True: loop preventing the session from exiting.

Is there some possibility to detect if a session is stopping. I was hoping for something in the session_state object which would allow me to do

async def some_coro():
    while st.session_state.server_state not in {'STOPPED', 'STOPPING'}:
        ... do something

Any ideas?