textual-dev icon indicating copy to clipboard operation
textual-dev copied to clipboard

Unclosed client session error printed after exiting Textual app

Open tconbeer opened this issue 2 months ago • 2 comments

On Textual 6.4 and textual-dev==1.8.0, running Harlequin with:

uv run textual run --dev -c harlequin

Upon exit, this error is printed:

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7efee5140cd0>

Adding the following code before starting the Textual app lets us see the source of the unclosed session:

    import asyncio
    import warnings
    import tracemalloc
    tracemalloc.start()

    asyncio.get_event_loop().set_debug(True)
    warnings.simplefilter("always", ResourceWarning)

That points to the client module in this package:

/home/tco/open/harlequin/.venv/lib/python3.10/site-packages/aiohttp/client.py:453: ResourceWarning: Unclosed client session <aiohttp.client.ClientSession object at 0x7fbe51bbc2b0>
  _warnings.warn(
Object allocated at (most recent call last):
  File "/home/tco/open/harlequin/.venv/lib/python3.10/site-packages/textual_dev/client.py", lineno 114
    self.session = aiohttp.ClientSession()
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fbe51bbc2b0>

https://github.com/Textualize/textual-dev/blob/e563f96f32d582b7cf22622a401f537c40349adc/src/textual_dev/client.py#L114

changing that assignment to a context manager should do the trick. PR incoming.

tconbeer avatar Oct 23 '25 20:10 tconbeer

Actually I think the issue is somewhere in App._shutdown in Textual, but I can't figure it out. This is the relevant call to devtools.disconnect()

https://github.com/Textualize/textual/blob/3427dbaed666a9af926c711c0eed552260738cd2/src/textual/app.py#L3606

tconbeer avatar Oct 23 '25 20:10 tconbeer

I figured it out - it only happens when the server isn't running, and the websocket fails to connect. A session still gets created, but it never gets killed.

tconbeer avatar Oct 23 '25 22:10 tconbeer