Skip to content

Enforce timeout in connect_to_server #2137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ansys/dpf/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def connect_to_server(
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
as_global=True,
timeout=5,
timeout=10.0,
config=None,
context=None,
):
Expand Down Expand Up @@ -367,6 +367,7 @@ def connect():
as_global=as_global,
launch_server=False,
context=context,
timeout=timeout,
)
else:
server = server_type(as_global=as_global, context=context)
Expand Down
17 changes: 9 additions & 8 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,6 @@
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
num_connection_tryouts: int = 3,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
Expand All @@ -786,6 +785,7 @@
address = f"{ip}:{port}"

self._remote_instance = None
start_time = time.time()
if launch_server:
if (
is_pypim_configured()
Expand Down Expand Up @@ -818,7 +818,7 @@
self._input_port = port
self.live = True
self._create_shutdown_funcs()
self._check_first_call(num_connection_tryouts)
self._check_first_call(timeout=timeout - (time.time() - start_time)) # Pass remaining time
if context:
if context == core.AvailableServerContexts.no_context:
self._base_service.initialize()
Expand All @@ -831,15 +831,16 @@
pass
self.set_as_global(as_global=as_global)

def _check_first_call(self, num_connection_tryouts):
for i in range(num_connection_tryouts):
def _check_first_call(self, timeout: float):
start_time = time.time()
while time.time() - start_time < timeout:
try:
self.version
_ = self.version
break
except errors.DPFServerException as e:
if ("GOAWAY" not in str(e.args) and "unavailable" not in str(e.args)) or i == (
num_connection_tryouts - 1
):
if "GOAWAY" in str(e.args) or "unavailable" in str(e.args):
time.sleep(0.5)

Check warning on line 842 in src/ansys/dpf/core/server_types.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/server_types.py#L841-L842

Added lines #L841 - L842 were not covered by tests
else:
raise e

@property
Expand Down
Loading