Skip to content

Commit b499d14

Browse files
authored
Enforce timeout in connect_to_server (#2137)
1 parent 4e6cb70 commit b499d14

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/ansys/dpf/core/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def connect_to_server(
302302
ip=LOCALHOST,
303303
port=DPF_DEFAULT_PORT,
304304
as_global=True,
305-
timeout=5,
305+
timeout=10.0,
306306
config=None,
307307
context=None,
308308
):
@@ -367,6 +367,7 @@ def connect():
367367
as_global=as_global,
368368
launch_server=False,
369369
context=context,
370+
timeout=timeout,
370371
)
371372
else:
372373
server = server_type(as_global=as_global, context=context)

src/ansys/dpf/core/server_types.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,6 @@ def __init__(
766766
launch_server: bool = True,
767767
docker_config: DockerConfig = RUNNING_DOCKER,
768768
use_pypim: bool = True,
769-
num_connection_tryouts: int = 3,
770769
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
771770
):
772771
# Load DPFClientAPI
@@ -786,6 +785,7 @@ def __init__(
786785
address = f"{ip}:{port}"
787786

788787
self._remote_instance = None
788+
start_time = time.time()
789789
if launch_server:
790790
if (
791791
is_pypim_configured()
@@ -818,7 +818,7 @@ def __init__(
818818
self._input_port = port
819819
self.live = True
820820
self._create_shutdown_funcs()
821-
self._check_first_call(num_connection_tryouts)
821+
self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time
822822
if context:
823823
if context == core.AvailableServerContexts.no_context:
824824
self._base_service.initialize()
@@ -831,15 +831,16 @@ def __init__(
831831
pass
832832
self.set_as_global(as_global=as_global)
833833

834-
def _check_first_call(self, num_connection_tryouts):
835-
for i in range(num_connection_tryouts):
834+
def _check_first_call(self, timeout: float):
835+
start_time = time.time()
836+
while time.time() - start_time < timeout:
836837
try:
837-
self.version
838+
_ = self.version
838839
break
839840
except errors.DPFServerException as e:
840-
if ("GOAWAY" not in str(e.args) and "unavailable" not in str(e.args)) or i == (
841-
num_connection_tryouts - 1
842-
):
841+
if "GOAWAY" in str(e.args) or "unavailable" in str(e.args):
842+
time.sleep(0.5)
843+
else:
843844
raise e
844845

845846
@property

0 commit comments

Comments
 (0)