From 3677695f63f0c4925fe75688277db27367a34dd1 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 20 Nov 2024 14:49:48 -0800 Subject: [PATCH] PYTHON-4982 Fix spurious network errors when using asyncio --- pymongo/network_layer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pymongo/network_layer.py b/pymongo/network_layer.py index aa16e85a07..0ca5d96206 100644 --- a/pymongo/network_layer.py +++ b/pymongo/network_layer.py @@ -71,6 +71,7 @@ async def async_sendall(sock: Union[socket.socket, _sslConn], buf: bytes) -> None: timeout = sock.gettimeout() sock.settimeout(0.0) + sock.setsockopt(socket.SOL_SOCKET, socket.SOCK_NONBLOCK, 1) loop = asyncio.get_event_loop() try: if _HAVE_SSL and isinstance(sock, (SSLSocket, _sslConn)): @@ -258,6 +259,7 @@ async def async_receive_data( timeout = sock_timeout sock.settimeout(0.0) + sock.setsockopt(socket.SOL_SOCKET, socket.SOCK_NONBLOCK, 1) loop = asyncio.get_event_loop() cancellation_task = asyncio.create_task(_poll_cancellation(conn)) try: @@ -288,6 +290,7 @@ async def async_receive_data_socket( timeout = sock_timeout sock.settimeout(0.0) + sock.setsockopt(socket.SOL_SOCKET, socket.SOCK_NONBLOCK, 1) loop = asyncio.get_event_loop() try: if _HAVE_SSL and isinstance(sock, (SSLSocket, _sslConn)):