Skip to content

Commit

Permalink
fixes warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Nov 30, 2022
1 parent 6bf9292 commit c938a04
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import logging
from typing import AsyncIterator

from aiohttp import web
from socketio import AsyncServer
Expand All @@ -13,6 +15,27 @@ def get_socket_server(app: web.Application) -> AsyncServer:
return app[APP_CLIENT_SOCKET_SERVER_KEY]


async def _socketio_server_cleanup_ctx(_app: web.Application) -> AsyncIterator[None]:
yield
# NOTE: this is ugly. It seems though that python-enginio does not
# cleanup its background tasks properly.
# https://github.com/miguelgrinberg/python-socketio/discussions/1092
current_tasks = asyncio.tasks.all_tasks()
cancelled_tasks = []
for task in current_tasks:
coro = task.get_coro()
if any(
coro_name in coro.__qualname__ # type: ignore
for coro_name in [
"AsyncServer._service_task",
"AsyncSocket.schedule_ping",
]
):
task.cancel()
cancelled_tasks.append(task)
await asyncio.gather(*cancelled_tasks, return_exceptions=True)


def setup_socketio_server(app: web.Application):
if app.get(APP_CLIENT_SOCKET_SERVER_KEY) is None:
# SEE https://github.com/miguelgrinberg/python-socketio/blob/v4.6.1/docs/server.rst#aiohttp
Expand All @@ -26,5 +49,6 @@ def setup_socketio_server(app: web.Application):
sio.attach(app)

app[APP_CLIENT_SOCKET_SERVER_KEY] = sio
app.cleanup_ctx.append(_socketio_server_cleanup_ctx)

return get_socket_server(app)
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,27 @@ async def test_websocket_multiple_connections(
)


@pytest.mark.skip(
reason="this test is here to show warnings when closing "
"the socketio server and could be useful as a proof"
"see https://github.com/miguelgrinberg/python-socketio/discussions/1092"
"and simcore_service_webserver.socketio.server _socketio_server_cleanup_ctx"
)
@pytest.mark.parametrize(
"user_role",
[
(UserRole.TESTER),
],
)
async def test_asyncio_task_pending_on_close(
client: TestClient,
logged_user: dict[str, Any],
socketio_client_factory: Callable,
):
sio = await socketio_client_factory()
# this test generates warnings on its own


@pytest.mark.parametrize(
"user_role,expected",
[
Expand Down

0 comments on commit c938a04

Please sign in to comment.