From ee3f0555d1a0642c30f13c456af94d681776d124 Mon Sep 17 00:00:00 2001 From: caixypromise Date: Sun, 4 May 2025 22:58:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(app):=20=E9=87=8D=E5=86=99app.py=E5=86=85?= =?UTF-8?q?=E7=9A=84wait=5Ffor=5Fexit()=EF=BC=8C=E4=BB=A5=E6=AD=A4?= =?UTF-8?q?=E8=A7=A3=E5=86=B3Windows=E7=8E=AF=E5=A2=83=E4=B8=8B=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E9=80=80=E5=87=BA=E6=97=B6=EF=BC=8C=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E9=98=BB=E5=A1=9E=E5=8D=A1=E6=AD=BB=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82=20=E5=BD=B1=E5=93=8D=20----=20-=20Ctrl=E2=80=91C/kill?= =?UTF-8?q?=E9=80=80=E5=87=BA=E6=97=B6=E4=B8=8D=E5=86=8D=E5=8D=A1=E4=BD=8F?= =?UTF-8?q?=EF=BC=8C=E8=B5=84=E6=BA=90=E5=AE=8C=E5=85=A8=E9=87=8A=E6=94=BE?= =?UTF-8?q?=EF=BC=8C=20-=20Windows=20=E4=B8=8E=20Unix=20=E8=A1=8C=E4=B8=BA?= =?UTF-8?q?=E4=B8=80=E8=87=B4=EF=BC=8C=E6=94=B9=E5=8A=A8=E4=B8=8D=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E6=AD=A3=E5=B8=B8=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/app.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) 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():