Skip to content

Commit

Permalink
Revert "⬆️ Maintenance: Upgrade python-socketio + flakyness (ITISFoun…
Browse files Browse the repository at this point in the history
…dation#3622)"

This reverts commit db03a28.
  • Loading branch information
sanderegg committed Dec 1, 2022
1 parent db03a28 commit 6d48471
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 265 deletions.
2 changes: 1 addition & 1 deletion services/web/server/requirements/_base.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# From 5.0.0, https://github.com/miguelgrinberg/python-socketio/blob/main/CHANGES.md
# test_resource_manager.py::test_websocket_resource_management fails because
# socket_id saved in redis does not correspond to client's sio
python-socketio
python-socketio~=4.6.1



Expand Down
15 changes: 5 additions & 10 deletions services/web/server/requirements/_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
# pip-compile --output-file=requirements/_base.txt --strip-extras requirements/_base.in
#
aio-pika==8.2.4
# via
# -c requirements/../../../../packages/service-library/requirements/./_base.in
# -r requirements/../../../../packages/service-library/requirements/_base.in
# -r requirements/../../../../packages/simcore-sdk/requirements/../../../packages/service-library/requirements/_base.in
# -r requirements/_base.in
# via -r requirements/_base.in
aiocache==0.11.1
# via -r requirements/../../../../packages/simcore-sdk/requirements/_base.in
aiodebug==2.3.0
Expand Down Expand Up @@ -88,8 +84,6 @@ attrs==21.4.0
# aiohttp
# jsonschema
# openapi-core
bidict==0.22.0
# via python-socketio
certifi==2022.6.15
# via requests
cffi==1.15.0
Expand Down Expand Up @@ -204,7 +198,6 @@ openapi-spec-validator==0.4.0
# -c requirements/../../../../packages/service-library/requirements/././constraints.txt
# -c requirements/../../../../packages/service-library/requirements/./constraints.txt
# -c requirements/../../../../packages/simcore-sdk/requirements/../../../packages/service-library/requirements/./constraints.txt
# -c requirements/../../../../packages/simcore-sdk/requirements/./constraints.txt
# openapi-core
openpyxl==3.0.9
# via -r requirements/_base.in
Expand Down Expand Up @@ -269,11 +262,11 @@ pyparsing==3.0.9
# via packaging
pyrsistent==0.18.1
# via jsonschema
python-engineio==4.3.4
python-engineio==3.14.2
# via python-socketio
python-magic==0.4.25
# via -r requirements/_base.in
python-socketio==5.7.2
python-socketio==4.6.1
# via -r requirements/_base.in
pytz==2022.1
# via twilio
Expand Down Expand Up @@ -309,6 +302,8 @@ six==1.16.0
# isodate
# jsonschema
# openapi-core
# python-engineio
# python-socketio
sqlalchemy==1.4.37
# via
# -c requirements/../../../../packages/models-library/requirements/../../../requirements/constraints.txt
Expand Down
3 changes: 1 addition & 2 deletions services/web/server/requirements/_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ ptvsd==4.3.2
# via -r requirements/_test.in
py==1.11.0
# via
# -c requirements/../../../../requirements/constraints.txt
# -r requirements/_test.in
# pytest-forked
pylint==2.15.5
Expand Down Expand Up @@ -260,7 +259,7 @@ urllib3==1.26.11
# requests
websocket-client==1.4.1
# via docker
websockets==10.4
websockets==10.3
# via -r requirements/_test.in
wrapt==1.14.1
# via
Expand Down
35 changes: 0 additions & 35 deletions services/web/server/requirements/_tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#
# pip-compile --output-file=requirements/_tools.txt --strip-extras requirements/_tools.in
#
astroid==2.12.12
# via
# -c requirements/_test.txt
# pylint
black==22.10.0
# via -r requirements/../../../../requirements/devenv.txt
build==0.8.0
Expand All @@ -22,10 +18,6 @@ click==8.1.3
# -c requirements/_test.txt
# black
# pip-tools
dill==0.3.5.1
# via
# -c requirements/_test.txt
# pylint
distlib==0.3.6
# via virtualenv
filelock==3.8.0
Expand All @@ -38,16 +30,6 @@ isort==5.10.1
# via
# -c requirements/_test.txt
# -r requirements/../../../../requirements/devenv.txt
# pylint
lazy-object-proxy==1.7.1
# via
# -c requirements/_base.txt
# -c requirements/_test.txt
# astroid
mccabe==0.7.0
# via
# -c requirements/_test.txt
# pylint
mypy-extensions==0.4.3
# via black
nodeenv==1.7.0
Expand All @@ -69,14 +51,9 @@ platformdirs==2.5.2
# via
# -c requirements/_test.txt
# black
# pylint
# virtualenv
pre-commit==2.20.0
# via -r requirements/../../../../requirements/devenv.txt
pylint==2.15.5
# via
# -c requirements/_test.txt
# -r requirements/../../../../requirements/devenv.txt
pyparsing==3.0.9
# via
# -c requirements/_base.txt
Expand All @@ -96,27 +73,15 @@ tomli==2.0.1
# black
# build
# pep517
# pylint
tomlkit==0.11.5
# via
# -c requirements/_test.txt
# pylint
typing-extensions==4.3.0
# via
# -c requirements/_base.txt
# -c requirements/_test.txt
# astroid
# black
# pylint
virtualenv==20.16.5
# via pre-commit
wheel==0.37.1
# via pip-tools
wrapt==1.14.1
# via
# -c requirements/_base.txt
# -c requirements/_test.txt
# astroid

# The following packages are considered to be unsafe in a requirements file:
# pip
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import asyncio
import logging
from typing import AsyncIterator

from aiohttp import web
from socketio import AsyncServer
Expand All @@ -15,40 +13,14 @@ 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
# TODO: ujson to speed up?
# TODO: client_manager= to socketio.AsyncRedisManager/AsyncAioPikaManager for horizontal scaling (shared sessions)
sio = AsyncServer(
async_mode="aiohttp",
logger=log, # type: ignore
engineio_logger=False,
)
sio = AsyncServer(async_mode="aiohttp", logger=log, engineio_logger=False)
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 @@ -279,12 +279,8 @@ async def connect_to_socketio(client, user, socketio_client_factory: Callable):
"user_id": str(user["id"]),
"client_session_id": cur_client_session_id,
}
assert await socket_registry.find_keys(("socket_id", sio.get_sid())) == [
resource_key
]
assert sio.get_sid() in await socket_registry.find_resources(
resource_key, "socket_id"
)
assert await socket_registry.find_keys(("socket_id", sio.sid)) == [resource_key]
assert sio.sid in await socket_registry.find_resources(resource_key, "socket_id")
assert len(await socket_registry.find_resources(resource_key, "socket_id")) == 1
sio_connection_data = sio, resource_key
return sio_connection_data
Expand All @@ -293,12 +289,12 @@ async def connect_to_socketio(client, user, socketio_client_factory: Callable):
async def disconnect_user_from_socketio(client, sio_connection_data):
"""disconnect a previously connected socket.io connection"""
sio, resource_key = sio_connection_data
sid = sio.get_sid()
sid = sio.sid
socket_registry = get_registry(client.server.app)
await sio.disconnect()
assert not sio.sid
await asyncio.sleep(0) # just to ensure there is a context switch
assert not await socket_registry.find_keys(("socket_id", sio.get_sid()))
assert not await socket_registry.find_keys(("socket_id", sio.sid))
assert not sid in await socket_registry.find_resources(resource_key, "socket_id")
assert not await socket_registry.find_resources(resource_key, "socket_id")

Expand Down
Loading

0 comments on commit 6d48471

Please sign in to comment.