solara icon indicating copy to clipboard operation
solara copied to clipboard

interrupted coroutine tasks do not close threads

Open rileythai opened this issue 10 months ago • 0 comments

Related to #508, but I thought it should get its own issue.

If you interrupt tasks with prefer_threaded=True (so you spawn coroutine threads; also the default), they do not close. You wil eventually hit an OSError in uvicorn because there are too many open threads.

Min reproducible (best to run on your own machine to see thread count go up on the solara process):

import solara as sl
import numpy as np
import asyncio


@sl.component()
def Page():
    switch = sl.use_reactive(False)

    async def get_value():
        await asyncio.sleep(
            1
        )  # long enough, doesnt really matter just needs to be "cancelled"
        return np.random.randint(0, 9) # this is just so it does something; i dont know if its necessary

    data = sl.lab.use_task(get_value,
                           dependencies=[switch.value],
                           prefer_threaded=True) # if it's False ,it will not spawn threads

    sl.Switch(label="spam me", value=switch)


if __name__ == "__main__":
    Page()

and solara run [filename].py

On Ubuntu 24.04 LTS:

solara                    1.44.1
solara-server             1.44.1
solara-ui                 1.44.1

edit fixes wording

rileythai avatar Feb 28 '25 07:02 rileythai