From e242f7738b5869ba1140907bd4e9faa919743c88 Mon Sep 17 00:00:00 2001 From: plan Date: Fri, 15 Nov 2024 19:57:55 +0800 Subject: [PATCH 1/2] FEAT: Add CONNECT_TIMEOUT option system default is too long. we add CONNECT_TIMEOUT default to 8 sec --- python/xoscar/backends/communication/socket.py | 10 ++++++++-- python/xoscar/constants.py | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/python/xoscar/backends/communication/socket.py b/python/xoscar/backends/communication/socket.py index 61742f7e..4f6718b5 100644 --- a/python/xoscar/backends/communication/socket.py +++ b/python/xoscar/backends/communication/socket.py @@ -29,7 +29,7 @@ from urllib.parse import urlparse from ..._utils import to_binary -from ...constants import XOSCAR_UNIX_SOCKET_DIR +from ...constants import XOSCAR_CONNECT_TIMEOUT, XOSCAR_UNIX_SOCKET_DIR from ...serialization import AioDeserializer, AioSerializer, deserialize from ...utils import classproperty, implements, is_py_312, is_v6_ip from .base import Channel, ChannelType, Client, Server @@ -291,7 +291,13 @@ async def connect( ) -> "Client": host, port_str = dest_address.rsplit(":", 1) port = int(port_str) - (reader, writer) = await asyncio.open_connection(host=host, port=port, **kwargs) + config = kwargs.get("config", {}) + connect_timeout = config.get("connect_timeout", XOSCAR_CONNECT_TIMEOUT) + fut = asyncio.open_connection(host=host, port=port) + try: + reader, writer = await asyncio.wait_for(fut, timeout=connect_timeout) + except asyncio.TimeoutError: + raise ConnectionError("connect timeout") channel = SocketChannel( reader, writer, local_address=local_address, dest_address=dest_address ) diff --git a/python/xoscar/constants.py b/python/xoscar/constants.py index b558a79d..5a940577 100644 --- a/python/xoscar/constants.py +++ b/python/xoscar/constants.py @@ -19,3 +19,5 @@ # unix socket. XOSCAR_UNIX_SOCKET_DIR = XOSCAR_TEMP_DIR / "socket" + +XOSCAR_CONNECT_TIMEOUT = 8 From b4521602b73d2cc741d87ea2d873bcb7a95c5b67 Mon Sep 17 00:00:00 2001 From: qinxuye Date: Mon, 25 Nov 2024 16:14:13 +0800 Subject: [PATCH 2/2] fix --- python/xoscar/backends/indigen/tests/test_pool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/xoscar/backends/indigen/tests/test_pool.py b/python/xoscar/backends/indigen/tests/test_pool.py index 6b60481d..fbf03241 100644 --- a/python/xoscar/backends/indigen/tests/test_pool.py +++ b/python/xoscar/backends/indigen/tests/test_pool.py @@ -886,7 +886,7 @@ async def test_server_closed(): # check if error raised normally when subprocess killed task = asyncio.create_task(actor_ref.sleep(10)) - await asyncio.sleep(0) + await asyncio.sleep(0.1) # kill subprocess 1 process = list(pool._sub_processes.values())[0]