From a57be0971fa2b735de9c11622ad6a3865b3278d1 Mon Sep 17 00:00:00 2001 From: Flowseal Date: Wed, 4 Mar 2026 18:04:15 +0300 Subject: [PATCH] Tray exit lag fix --- tg_ws_proxy.py | 9 ++++++++- tg_ws_tray.py | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tg_ws_proxy.py b/tg_ws_proxy.py index b9d11b8..3fdd435 100644 --- a/tg_ws_proxy.py +++ b/tg_ws_proxy.py @@ -790,7 +790,14 @@ async def _run(port: int, dc_opt: Dict[int, Optional[str]], async def wait_stop(): await stop_event.wait() server.close() - await server.wait_closed() + me = asyncio.current_task() + for task in list(asyncio.all_tasks()): + if task is not me: + task.cancel() + try: + await server.wait_closed() + except asyncio.CancelledError: + pass asyncio.create_task(wait_stop()) async with server: diff --git a/tg_ws_tray.py b/tg_ws_tray.py index 020f04a..99a628b 100644 --- a/tg_ws_tray.py +++ b/tg_ws_tray.py @@ -51,6 +51,7 @@ _stop_event: Optional[threading.Event] = None _async_stop: Optional[object] = None _tray_icon: Optional[object] = None _config: dict = {} +_exiting: bool = False log = logging.getLogger("tg-ws-tray") @@ -198,7 +199,7 @@ def stop_proxy(): loop, stop_ev = _async_stop loop.call_soon_threadsafe(stop_ev.set) if _proxy_thread: - _proxy_thread.join(timeout=5) + _proxy_thread.join(timeout=2) _proxy_thread = None log.info("Proxy stopped") @@ -401,8 +402,18 @@ def _on_open_logs(icon=None, item=None): def _on_exit(icon=None, item=None): + global _exiting + if _exiting: + os._exit(0) + return + _exiting = True log.info("User requested exit") - stop_proxy() + + def _force_exit(): + time.sleep(3) + os._exit(0) + threading.Thread(target=_force_exit, daemon=True, name="force-exit").start() + if icon: icon.stop()