This commit is contained in:
zerone 2025-02-24 20:50:59 +08:00
parent 6a978c4275
commit 9eff208e8e

View File

@ -434,8 +434,9 @@ def handle_sigint():
def configure_asyncio_event_loop():
"""Configure the appropriate event loop based on the platform"""
if platform.system() == 'Windows':
# Windows specific configuration
loop = asyncio.ProactorEventLoop()
# Windows specific configuration for aiodns compatibility
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
else:
# Unix-like systems configuration
@ -465,7 +466,16 @@ def run_server():
except KeyboardInterrupt:
handle_sigint()
finally:
loop.close()
try:
# Clean up pending tasks
pending = asyncio.all_tasks(loop)
for task in pending:
task.cancel()
# Give tasks a chance to clean up
if pending:
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
finally:
loop.close()
if __name__ == "__main__":