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..cff210006 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -1581,10 +1581,26 @@ 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" + ) + 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 + -@pytest.mark.skipif( - sys.platform == "win32", reason="UNIX sockets are not available on Windows" -) class TestUNIXListener: @pytest.fixture( params=[