From b5d33cb36e4967f670360b934def645139db28e8 Mon Sep 17 00:00:00 2001
From: linznin <linznin@gmail.com>
Date: Thu, 23 Jan 2025 21:51:41 +0800
Subject: [PATCH] fix: improve service shutdown handling in for cross-platform
 compatibility (#5124)

---
 .../samples/core_grpc_worker_runtime/run_host.py   | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/python/samples/core_grpc_worker_runtime/run_host.py b/python/samples/core_grpc_worker_runtime/run_host.py
index 3f899987c6a7..5a1170ec0a5d 100644
--- a/python/samples/core_grpc_worker_runtime/run_host.py
+++ b/python/samples/core_grpc_worker_runtime/run_host.py
@@ -1,4 +1,5 @@
 import asyncio
+import os
 
 from autogen_ext.runtimes.grpc import GrpcWorkerAgentRuntimeHost
 
@@ -6,7 +7,18 @@
 async def main() -> None:
     service = GrpcWorkerAgentRuntimeHost(address="localhost:50051")
     service.start()
-    await service.stop_when_signal()
+
+    try:
+        # Wait for the service to stop
+        if os.system() == "Windows":
+            # On Windows, the signal is not available, so we wait for a new event
+            await asyncio.Event().wait()
+        else:
+            await service.stop_when_signal()
+    except KeyboardInterrupt:
+        print("Stopping service...")
+    finally:
+        await service.stop()
 
 
 if __name__ == "__main__":