fix: 服务器停止时优雅等待连接关闭
问题: stop() 只调用 server.close() 后立即停止事件循环 导致活跃连接收到 GeneratorExit,产生大量 Task destroyed 警告 修复: 使用 run_coroutine_threadsafe 在子线程事件循环中执行 server.wait_closed(),最多等待5秒优雅关闭
This commit is contained in:
+11
-2
@@ -96,8 +96,17 @@ class Socks5Server:
|
|||||||
if not self.running:
|
if not self.running:
|
||||||
return
|
return
|
||||||
self.running = False
|
self.running = False
|
||||||
if self.server:
|
if self.server and self.loop:
|
||||||
self.server.close()
|
# 在子线程事件循环中优雅关闭服务器
|
||||||
|
import concurrent.futures
|
||||||
|
async def _shutdown():
|
||||||
|
self.server.close()
|
||||||
|
await self.server.wait_closed()
|
||||||
|
try:
|
||||||
|
future = asyncio.run_coroutine_threadsafe(_shutdown(), self.loop)
|
||||||
|
future.result(timeout=5) # 最多等待5秒
|
||||||
|
except (concurrent.futures.TimeoutError, Exception):
|
||||||
|
pass
|
||||||
if self.loop:
|
if self.loop:
|
||||||
try:
|
try:
|
||||||
self.loop.call_soon_threadsafe(self.loop.stop)
|
self.loop.call_soon_threadsafe(self.loop.stop)
|
||||||
|
|||||||
Reference in New Issue
Block a user