Skip to content

Commit

Permalink
Low-level API: endpoints should always clear the receiver buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Nov 23, 2024
1 parent e139463 commit e69a892
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/easynetwork/lowlevel/api_async/endpoints/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ async def aclose(self) -> None:
"""
Closes the endpoint.
"""
await self.__transport.aclose()
self.__receiver.clear()
try:
await self.__transport.aclose()
finally:
self.__receiver.clear()

async def recv_packet(self) -> _T_ReceivedPacket:
"""
Expand Down Expand Up @@ -269,8 +271,10 @@ async def aclose(self) -> None:
Closes the endpoint.
"""
with self.__send_guard:
await self.__transport.aclose()
self.__receiver.clear()
try:
await self.__transport.aclose()
finally:
self.__receiver.clear()

async def send_packet(self, packet: _T_SentPacket) -> None:
"""
Expand Down
2 changes: 2 additions & 0 deletions src/easynetwork/lowlevel/api_async/servers/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ async def __client_coroutine(
case _: # pragma: no cover
assert_never(self.__protocol)

# NOTE: It is safe to clear the consumer before the transport here.
# There is no task reading the transport at this point.
task_exit_stack.callback(consumer.clear)

request_handler_generator = client_connected_cb(
Expand Down
12 changes: 8 additions & 4 deletions src/easynetwork/lowlevel/api_sync/endpoints/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def close(self) -> None:
"""
Closes the endpoint.
"""
self.__transport.close()
self.__receiver.clear()
try:
self.__transport.close()
finally:
self.__receiver.clear()

def recv_packet(self, *, timeout: float | None = None) -> _T_ReceivedPacket:
"""
Expand Down Expand Up @@ -270,8 +272,10 @@ def close(self) -> None:
"""
Closes the endpoint.
"""
self.__transport.close()
self.__receiver.clear()
try:
self.__transport.close()
finally:
self.__receiver.clear()

def send_packet(self, packet: _T_SentPacket, *, timeout: float | None = None) -> None:
"""
Expand Down

0 comments on commit e69a892

Please sign in to comment.