Session life

How do you actually run into such a situation? Is that from a running task?
3 Replies
Ben Epstein
Ben Epstein2w ago
I'm looking for a way to know if the user has left (closed the tab), or if there are no active sessions
MaartenBreddels
MaartenBreddelsOP2w ago
yes, but how does that function execute outside of the virtual kernel. Is this a thread running? I think I need a bit more context, because I usually don't see this situation
Ben Epstein
Ben Epstein2w ago
yea thats fair. So the situation is that i'm deploying this on modal. Modal shuts down containers after some amount of inactivity. But they define inactivity as no incoming requests. Since solara on the "client side" isnt making requests back to the server, the container shuts down. To resolve this, i implemented a slab.task keepalive that pings the solara /readyz endopint every 2 minutes, so long as there is an active user If I don't include that last part, since solara is just always running the task, the modal container never shuts down. So this solution works, its just a little funny
import asyncio

import aiohttp
import solara as sl
import solara.lab as slab

from app.constants import MODAL_LIVE


@slab.task
async def keepalive():
url = "modal-endpoint/readyz"
async with aiohttp.ClientSession() as session:
while True:
try:
sl.get_kernel_id()
async with session.get(url) as response:
response.raise_for_status()
print(f"Successful request to {url}")

except aiohttp.ClientError as e:
print(f"Request failed: {str(e)}")
except RuntimeError:
print("Kernel is no longer alive, allowing shutdown.")
except Exception as e:
print(f"Unexpected error: {str(e)}")

await asyncio.sleep(120)
import asyncio

import aiohttp
import solara as sl
import solara.lab as slab

from app.constants import MODAL_LIVE


@slab.task
async def keepalive():
url = "modal-endpoint/readyz"
async with aiohttp.ClientSession() as session:
while True:
try:
sl.get_kernel_id()
async with session.get(url) as response:
response.raise_for_status()
print(f"Successful request to {url}")

except aiohttp.ClientError as e:
print(f"Request failed: {str(e)}")
except RuntimeError:
print("Kernel is no longer alive, allowing shutdown.")
except Exception as e:
print(f"Unexpected error: {str(e)}")

await asyncio.sleep(120)

Did you find this page helpful?