Skip to content
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

FIX: Embedding rpc server shutdown #1088

Merged
merged 7 commits into from
Feb 10, 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
1 change: 1 addition & 0 deletions doc/changelog.d/1088.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Emedding rpc server shutdown
2 changes: 1 addition & 1 deletion src/ansys/mechanical/core/embedding/rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@ def is_alive(self):
def exit(self):
"""Shuts down the Mechanical instance."""
print("Requesting server shutdown ...")
self.root.service_exit()
self.service_exit()
self.connection.close()
print("Disconnected from server")
27 changes: 27 additions & 0 deletions src/ansys/mechanical/core/embedding/rpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import fnmatch
import os
import threading
import time
import typing

import rpyc
Expand Down Expand Up @@ -211,8 +213,11 @@

def exposed_service_exit(self):
"""Exit the server."""
print("Shutting down server ...")
self._backgroundapp.stop()
self._backgroundapp = None
self._server.stop_async()
print("Server stopped")


class MechanicalEmbeddedServer:
Expand All @@ -231,6 +236,7 @@
self._background_app = BackgroundApp(version=version)
self._service = service
self._methods = methods
self._exit_thread: threading.Thread = None
print("Initializing Mechanical ...")

self._port = self.get_free_port(port)
Expand All @@ -241,6 +247,7 @@

my_service = self._service(self._background_app, self._methods, self._impl)
self._server = ThreadedServer(my_service, port=self._port)
my_service._server = self

@staticmethod
def get_free_port(port=None):
Expand Down Expand Up @@ -270,8 +277,28 @@
print("User interrupt!")
finally:
conn.close()"""
print("Server exited!")
self._wait_exit()
self._exited = True

def _wait_exit(self) -> None:
if self._exit_thread is None:
return

Check warning on line 286 in src/ansys/mechanical/core/embedding/rpc/server.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mechanical/core/embedding/rpc/server.py#L286

Added line #L286 was not covered by tests
self._exit_thread.join()

def stop_async(self):
"""Return immediately but will stop the server."""

def stop_f(): # wait for all connections to close
while len(self._server.clients) > 0:
time.sleep(0.005)
self._background_app.stop()
self._server.close()
self._exited = True

self._exit_thread = threading.Thread(target=stop_f)
self._exit_thread.start()

def stop(self) -> None:
"""Stop the server."""
print("Stopping the server...")
Expand Down
24 changes: 13 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import shutil
import subprocess
import sys
import time

import ansys.tools.path as atp
import grpc
Expand Down Expand Up @@ -305,15 +306,6 @@ def connect_rpc_embedded_server(port: int):
return client


def stop_embeddedd_server():
# TODO: not use a terminate.
global embedded_server
if embedded_server is not None:
embedded_server.terminate()
embedded_server.wait()
embedded_server = None


@pytest.fixture(scope="session")
def mechanical(pytestconfig, rootdir):
print("current working directory: ", os.getcwd())
Expand All @@ -338,9 +330,19 @@ def mechanical(pytestconfig, rootdir):
print(mechanical)
yield mechanical
if is_embedded_server:
print("stopping embedded server")
print("\n Stopping embedded server")
global embedded_server
mechanical.exit()
dipinknair marked this conversation as resolved.
Show resolved Hide resolved
stop_embeddedd_server()
start_time = time.time()
while embedded_server.poll() is None:
if time.time() - start_time > 10:
try:
embedded_server.terminate()
embedded_server.wait()
except subprocess.TimeoutExpired:
embedded_server.kill()
break
time.sleep(0.5)
else:
assert "Ansys Mechanical" in str(mechanical)

Expand Down
Loading