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

Added mechanism to determine if server did not start cleanly #1539

Merged
merged 2 commits into from
Jun 4, 2023
Merged
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
10 changes: 10 additions & 0 deletions pymodbus/server/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ def __init__(

# asyncio future that will be done once server has started
self.serving = asyncio.Future()
self.serving_done = asyncio.Future()
# constructors cannot be declared async, so we have to
# defer the initialization of the server
self.server = None
Expand All @@ -552,13 +553,15 @@ async def serve_forever(self):
Log.info("Server(Unix) listening.")
await self.server.serve_forever()
except asyncio.exceptions.CancelledError:
self.serving_done.set_result(True)
raise
except Exception as exc: # pylint: disable=broad-except
Log.error("Server unexpected exception {}", exc)
else:
raise RuntimeError(
"Can't call serve_forever on an already running server object"
)
self.serving_done.set_result(True)
Log.info("Server graceful shutdown.")

async def shutdown(self):
Expand Down Expand Up @@ -641,6 +644,7 @@ def __init__(

# asyncio future that will be done once server has started
self.serving = asyncio.Future()
self.serving_done = asyncio.Future()
# constructors cannot be declared async, so we have to
# defer the initialization of the server
self.server = None
Expand All @@ -663,13 +667,15 @@ async def serve_forever(self):
try:
await self.server.serve_forever()
except asyncio.exceptions.CancelledError:
self.serving_done.set_result(False)
raise
except Exception as exc: # pylint: disable=broad-except
Log.error("Server unexpected exception {}", exc)
else:
raise RuntimeError(
"Can't call serve_forever on an already running server object"
)
self.serving_done.set_result(True)
Log.info("Server graceful shutdown.")

async def shutdown(self):
Expand Down Expand Up @@ -821,6 +827,7 @@ def __init__(
self.stop_serving = self.loop.create_future()
# asyncio future that will be done once server has started
self.serving = asyncio.Future()
self.serving_done = asyncio.Future()
self.factory_parms = {
"local_addr": self.address,
"allow_broadcast": True,
Expand All @@ -836,9 +843,11 @@ async def serve_forever(self):
**self.factory_parms,
)
except asyncio.exceptions.CancelledError:
self.serving_done.set_result(False)
raise
except Exception as exc:
Log.error("Server unexpected exception {}", exc)
self.serving_done.set_result(False)
raise RuntimeError(exc) from exc
Log.info("Server(UDP) listening.")
self.serving.set_result(True)
Expand All @@ -847,6 +856,7 @@ async def serve_forever(self):
raise RuntimeError(
"Can't call serve_forever on an already running server object"
)
self.serving_done.set_result(True)

async def shutdown(self):
"""Shutdown server."""
Expand Down