Close unclosed resources in the whole project (#267)

* Close resources

* Close all resources

* Update pyproject.toml

* Close all resources

* Close all resources

* try now...

* try to ignore this

* try again

* try adding one more..

* try now

* try now

* revert ci changes
This commit is contained in:
Marcelo Trylesinski 2025-03-13 11:59:45 +01:00 • committed by GitHub
commit 94d326dbf1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,5 @@
import logging
from contextlib import AsyncExitStack
from datetime import timedelta
from typing import Any, Callable, Generic, TypeVar
@ -180,6 +181,7 @@ class BaseSession(
self._read_timeout_seconds = read_timeout_seconds
self._in_flight = {}
self._exit_stack = AsyncExitStack()
self._incoming_message_stream_writer, self._incoming_message_stream_reader = (
anyio.create_memory_object_stream[
RequestResponder[ReceiveRequestT, SendResultT]
@ -187,6 +189,12 @@ class BaseSession(
| Exception
]()
)
self._exit_stack.push_async_callback(
lambda: self._incoming_message_stream_reader.aclose()
)
self._exit_stack.push_async_callback(
lambda: self._incoming_message_stream_writer.aclose()
)
async def __aenter__(self) -> Self:
self._task_group = anyio.create_task_group()
@ -195,6 +203,7 @@ class BaseSession(
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._exit_stack.aclose()
# Using BaseSession as a context manager should not block on exit (this
# would be very surprising behavior), so make sure to cancel the tasks
# in the task group.
@ -222,6 +231,9 @@ class BaseSession(
](1)
self._response_streams[request_id] = response_stream
self._exit_stack.push_async_callback(lambda: response_stream.aclose())
self._exit_stack.push_async_callback(lambda: response_stream_reader.aclose())
jsonrpc_request = JSONRPCRequest(
jsonrpc="2.0",
id=request_id,