diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index 7ed71ae5..4ad57855 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -12,22 +12,26 @@ TAG = __name__ logger = setup_logging() -async def wait_for_exit(): - """Windows 和 Linux 兼容的退出监听""" +async def wait_for_exit() -> None: + """ + 阻塞直到收到 Ctrl‑C / SIGTERM。 + - Unix: 使用 add_signal_handler + - Windows: 依赖 KeyboardInterrupt + """ loop = asyncio.get_running_loop() stop_event = asyncio.Event() - if sys.platform == "win32": - # Windows: 用 sys.stdin.read() 监听 Ctrl + C - await loop.run_in_executor(None, sys.stdin.read) - else: - # Linux/macOS: 用 signal 监听 Ctrl + C - def stop(): - stop_event.set() - - loop.add_signal_handler(signal.SIGINT, stop) - loop.add_signal_handler(signal.SIGTERM, stop) # 支持 kill 进程 + if sys.platform != "win32": # Unix / macOS + for sig in (signal.SIGINT, signal.SIGTERM): + loop.add_signal_handler(sig, stop_event.set) await stop_event.wait() + else: + # Windows:await一个永远pending的fut, + # 让 KeyboardInterrupt 冒泡到 asyncio.run,以此消除遗留普通线程导致进程退出阻塞的问题 + try: + await asyncio.Future() + except KeyboardInterrupt: # Ctrl‑C + pass async def main():