Skip to content

Commit

Permalink
[FIX] don't wait for canceled task
Browse files Browse the repository at this point in the history
  • Loading branch information
arc-hugo committed Dec 18, 2024
1 parent b9abd06 commit 1f0befa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/seahorse/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023-present U.N. Owen <[email protected]>
#
# SPDX-License-Identifier: MIT
__version__ = "1.1.6b0"
__version__ = "1.1.6b1"
29 changes: 16 additions & 13 deletions src/seahorse/game/io_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import asyncio
import functools
import json
import time
import re
import socketio
import time
from collections import deque
from collections.abc import Coroutine
from typing import TYPE_CHECKING, Any, Callable

from typing import TYPE_CHECKING, Any, Callable, Coroutine
import socketio
from aiohttp import web
from loguru import logger
from collections import deque

from seahorse.game.action import Action
from seahorse.game.heavy_action import HeavyAction
Expand All @@ -33,7 +34,8 @@ def activate(self,
Args:
identifier (str | None, optional): Must be a unique identifier. Defaults to None.
wrapped_id (int | None, optional): If the eventSlave is bound to an instance a python native id might be associated.
wrapped_id (int | None, optional): If the eventSlave is bound to an instance,
a python native id might be associated.
Defaults to None.
"""
self.sio = socketio.AsyncClient()
Expand Down Expand Up @@ -123,7 +125,7 @@ def get_instance(game_state:type=Serializable,port: int = 8080,hostname: str="lo
"""Gets the instance object
Args:
n_clients (int, optional): the number of clients the instance is supposed to be listening,
n_clients (int, optional): the number of clients the instance is supposed to be listening,
*ignored* if already initialized. Defaults to 1.
game_state : class of a game state
port (int, optional): the port to use. Defaults to 8080.
Expand All @@ -137,7 +139,8 @@ def get_instance(game_state:type=Serializable,port: int = 8080,hostname: str="lo

def __init__(self,game_state,port,hostname):
if EventMaster.__instance is not None:
msg = "Trying to initialize multiple instances of EventMaster, this is forbidden to avoid side-effects.\n Call EventMaster.get_instance() instead."
msg = "Trying to initialize multiple instances of EventMaster, \
this is forbidden to avoid side-effects.\n Call EventMaster.get_instance() instead."
raise NotImplementedError(msg)
else:
# Initializing attributes
Expand All @@ -153,7 +156,7 @@ def __init__(self,game_state,port,hostname):
self.hostname = hostname

# Standard python-socketio server
self.sio = socketio.AsyncServer(async_mode="aiohttp", async_handlers=True,
self.sio = socketio.AsyncServer(async_mode="aiohttp", async_handlers=True,
cors_allowed_origins="*", ping_timeout=1e6)
self.app = web.Application()

Expand Down Expand Up @@ -181,7 +184,8 @@ def connect(sid, *_):
"""
self.__open_sessions.add(sid)
self.__n_clients_connected += 1
logger.info(f"Waiting for listeners {self.__n_clients_connected} out of {self.expected_clients} are connected.")
logger.info(f"Waiting for listeners {self.__n_clients_connected} \
out of {self.expected_clients} are connected.")

@self.sio.event
def disconnect(sid):
Expand Down Expand Up @@ -342,11 +346,10 @@ async def stop(task):

# Explicitly cancel any remaining tasks related to disconnected clients
# logger.info("Canceling pending tasks related to disconnected clients.")
tasks = [task for task in asyncio.all_tasks() if task is not asyncio.current_task()]
for task in tasks:
task.cancel()
all_pending_tasks = [task for task in asyncio.all_tasks() if task is not asyncio.current_task()]
for pending_task in all_pending_tasks:
try:
await task
pending_task.cancel()
except asyncio.CancelledError:
pass

Expand Down

0 comments on commit 1f0befa

Please sign in to comment.