From e89cf66dbd1746f3373ccb14cd2f2c2d2c8c071f Mon Sep 17 00:00:00 2001 From: Mukller Date: Tue, 25 Aug 2026 14:42:52 +0300 Subject: [PATCH 1/2] fix: add checkpoint to UNIXSocketStream.aclose and _TrioSocketMixin.aclose (fixes #1288) --- src/anyio/_backends/_asyncio.py | 1 + src/anyio/_backends/_trio.py | 1 + tests/test_sockets.py | 21 ++++++++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/anyio/_backends/_asyncio.py b/src/anyio/_backends/_asyncio.py index 35b0cf371..070ac8785 100644 --- a/src/anyio/_backends/_asyncio.py +++ b/src/anyio/_backends/_asyncio.py @@ -1452,6 +1452,7 @@ async def aclose(self) -> None: self._receive_future.set_result(None) if self._send_future and not self._send_future.done(): self._send_future.set_result(None) + await AsyncIOBackend.checkpoint() class UNIXSocketStream(_RawSocketMixin, abc.UNIXSocketStream): diff --git a/src/anyio/_backends/_trio.py b/src/anyio/_backends/_trio.py index 2e67786e8..695b36480 100644 --- a/src/anyio/_backends/_trio.py +++ b/src/anyio/_backends/_trio.py @@ -466,6 +466,7 @@ async def aclose(self) -> None: if self._trio_socket.fileno() >= 0: self._closed = True self._trio_socket.close() + await trio.lowlevel.checkpoint() def _convert_socket_error(self, exc: BaseException) -> NoReturn: if isinstance(exc, trio.ClosedResourceError): diff --git a/tests/test_sockets.py b/tests/test_sockets.py index cc661b79a..83d651ede 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -1582,9 +1582,24 @@ async def test_from_socket_not_connected( await UNIXSocketStream.from_socket(sock_or_fd) -@pytest.mark.skipif( - sys.platform == "win32", reason="UNIX sockets are not available on Windows" -) + @pytest.mark.skipif( + sys.platform == "win32", reason="UNIX sockets are not available on Windows" + ) + async def test_aclose_in_cancelled_scope_raises_cancelled_exc( + self, server_sock: socket.socket, socket_path: Path + ) -> None: + exc = None + stream = await connect_unix(socket_path) + + with CancelScope() as scope: + scope.cancel() + try: + await stream.aclose() + except get_cancelled_exc_class() as e: + exc = e + raise + + assert exc is not None class TestUNIXListener: @pytest.fixture( params=[ From b2abaa71f0abb9489b3b1e3d692f4b09a40be0f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Aug 2026 11:43:57 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_sockets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_sockets.py b/tests/test_sockets.py index 83d651ede..cff210006 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -1581,7 +1581,6 @@ async def test_from_socket_not_connected( with pytest.raises(ValueError, match="the socket must be connected"): await UNIXSocketStream.from_socket(sock_or_fd) - @pytest.mark.skipif( sys.platform == "win32", reason="UNIX sockets are not available on Windows" ) @@ -1600,6 +1599,8 @@ async def test_aclose_in_cancelled_scope_raises_cancelled_exc( raise assert exc is not None + + class TestUNIXListener: @pytest.fixture( params=[