Skip to content

Commit db03a28

Browse files
authored
⬆️ Maintenance: Upgrade python-socketio + flakyness (ITISFoundation#3622)
1 parent 51d6df3 commit db03a28

File tree

8 files changed

+265
-137
lines changed

8 files changed

+265
-137
lines changed

services/web/server/requirements/_base.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# From 5.0.0, https://github.com/miguelgrinberg/python-socketio/blob/main/CHANGES.md
2323
# test_resource_manager.py::test_websocket_resource_management fails because
2424
# socket_id saved in redis does not correspond to client's sio
25-
python-socketio~=4.6.1
25+
python-socketio
2626

2727

2828

services/web/server/requirements/_base.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
# pip-compile --output-file=requirements/_base.txt --strip-extras requirements/_base.in
66
#
77
aio-pika==8.2.4
8-
# via -r requirements/_base.in
8+
# via
9+
# -c requirements/../../../../packages/service-library/requirements/./_base.in
10+
# -r requirements/../../../../packages/service-library/requirements/_base.in
11+
# -r requirements/../../../../packages/simcore-sdk/requirements/../../../packages/service-library/requirements/_base.in
12+
# -r requirements/_base.in
913
aiocache==0.11.1
1014
# via -r requirements/../../../../packages/simcore-sdk/requirements/_base.in
1115
aiodebug==2.3.0
@@ -84,6 +88,8 @@ attrs==21.4.0
8488
# aiohttp
8589
# jsonschema
8690
# openapi-core
91+
bidict==0.22.0
92+
# via python-socketio
8793
certifi==2022.6.15
8894
# via requests
8995
cffi==1.15.0
@@ -198,6 +204,7 @@ openapi-spec-validator==0.4.0
198204
# -c requirements/../../../../packages/service-library/requirements/././constraints.txt
199205
# -c requirements/../../../../packages/service-library/requirements/./constraints.txt
200206
# -c requirements/../../../../packages/simcore-sdk/requirements/../../../packages/service-library/requirements/./constraints.txt
207+
# -c requirements/../../../../packages/simcore-sdk/requirements/./constraints.txt
201208
# openapi-core
202209
openpyxl==3.0.9
203210
# via -r requirements/_base.in
@@ -262,11 +269,11 @@ pyparsing==3.0.9
262269
# via packaging
263270
pyrsistent==0.18.1
264271
# via jsonschema
265-
python-engineio==3.14.2
272+
python-engineio==4.3.4
266273
# via python-socketio
267274
python-magic==0.4.25
268275
# via -r requirements/_base.in
269-
python-socketio==4.6.1
276+
python-socketio==5.7.2
270277
# via -r requirements/_base.in
271278
pytz==2022.1
272279
# via twilio
@@ -302,8 +309,6 @@ six==1.16.0
302309
# isodate
303310
# jsonschema
304311
# openapi-core
305-
# python-engineio
306-
# python-socketio
307312
sqlalchemy==1.4.37
308313
# via
309314
# -c requirements/../../../../packages/models-library/requirements/../../../requirements/constraints.txt

services/web/server/requirements/_test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ ptvsd==4.3.2
155155
# via -r requirements/_test.in
156156
py==1.11.0
157157
# via
158+
# -c requirements/../../../../requirements/constraints.txt
158159
# -r requirements/_test.in
159160
# pytest-forked
160161
pylint==2.15.5
@@ -259,7 +260,7 @@ urllib3==1.26.11
259260
# requests
260261
websocket-client==1.4.1
261262
# via docker
262-
websockets==10.3
263+
websockets==10.4
263264
# via -r requirements/_test.in
264265
wrapt==1.14.1
265266
# via

services/web/server/requirements/_tools.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#
55
# pip-compile --output-file=requirements/_tools.txt --strip-extras requirements/_tools.in
66
#
7+
astroid==2.12.12
8+
# via
9+
# -c requirements/_test.txt
10+
# pylint
711
black==22.10.0
812
# via -r requirements/../../../../requirements/devenv.txt
913
build==0.8.0
@@ -18,6 +22,10 @@ click==8.1.3
1822
# -c requirements/_test.txt
1923
# black
2024
# pip-tools
25+
dill==0.3.5.1
26+
# via
27+
# -c requirements/_test.txt
28+
# pylint
2129
distlib==0.3.6
2230
# via virtualenv
2331
filelock==3.8.0
@@ -30,6 +38,16 @@ isort==5.10.1
3038
# via
3139
# -c requirements/_test.txt
3240
# -r requirements/../../../../requirements/devenv.txt
41+
# pylint
42+
lazy-object-proxy==1.7.1
43+
# via
44+
# -c requirements/_base.txt
45+
# -c requirements/_test.txt
46+
# astroid
47+
mccabe==0.7.0
48+
# via
49+
# -c requirements/_test.txt
50+
# pylint
3351
mypy-extensions==0.4.3
3452
# via black
3553
nodeenv==1.7.0
@@ -51,9 +69,14 @@ platformdirs==2.5.2
5169
# via
5270
# -c requirements/_test.txt
5371
# black
72+
# pylint
5473
# virtualenv
5574
pre-commit==2.20.0
5675
# via -r requirements/../../../../requirements/devenv.txt
76+
pylint==2.15.5
77+
# via
78+
# -c requirements/_test.txt
79+
# -r requirements/../../../../requirements/devenv.txt
5780
pyparsing==3.0.9
5881
# via
5982
# -c requirements/_base.txt
@@ -73,15 +96,27 @@ tomli==2.0.1
7396
# black
7497
# build
7598
# pep517
99+
# pylint
100+
tomlkit==0.11.5
101+
# via
102+
# -c requirements/_test.txt
103+
# pylint
76104
typing-extensions==4.3.0
77105
# via
78106
# -c requirements/_base.txt
79107
# -c requirements/_test.txt
108+
# astroid
80109
# black
110+
# pylint
81111
virtualenv==20.16.5
82112
# via pre-commit
83113
wheel==0.37.1
84114
# via pip-tools
115+
wrapt==1.14.1
116+
# via
117+
# -c requirements/_base.txt
118+
# -c requirements/_test.txt
119+
# astroid
85120

86121
# The following packages are considered to be unsafe in a requirements file:
87122
# pip

services/web/server/src/simcore_service_webserver/socketio/server.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import asyncio
12
import logging
3+
from typing import AsyncIterator
24

35
from aiohttp import web
46
from socketio import AsyncServer
@@ -13,14 +15,40 @@ def get_socket_server(app: web.Application) -> AsyncServer:
1315
return app[APP_CLIENT_SOCKET_SERVER_KEY]
1416

1517

18+
async def _socketio_server_cleanup_ctx(_app: web.Application) -> AsyncIterator[None]:
19+
yield
20+
# NOTE: this is ugly. It seems though that python-enginio does not
21+
# cleanup its background tasks properly.
22+
# https://github.com/miguelgrinberg/python-socketio/discussions/1092
23+
current_tasks = asyncio.tasks.all_tasks()
24+
cancelled_tasks = []
25+
for task in current_tasks:
26+
coro = task.get_coro()
27+
if any(
28+
coro_name in coro.__qualname__ # type: ignore
29+
for coro_name in [
30+
"AsyncServer._service_task",
31+
"AsyncSocket.schedule_ping",
32+
]
33+
):
34+
task.cancel()
35+
cancelled_tasks.append(task)
36+
await asyncio.gather(*cancelled_tasks, return_exceptions=True)
37+
38+
1639
def setup_socketio_server(app: web.Application):
1740
if app.get(APP_CLIENT_SOCKET_SERVER_KEY) is None:
1841
# SEE https://github.com/miguelgrinberg/python-socketio/blob/v4.6.1/docs/server.rst#aiohttp
1942
# TODO: ujson to speed up?
2043
# TODO: client_manager= to socketio.AsyncRedisManager/AsyncAioPikaManager for horizontal scaling (shared sessions)
21-
sio = AsyncServer(async_mode="aiohttp", logger=log, engineio_logger=False)
44+
sio = AsyncServer(
45+
async_mode="aiohttp",
46+
logger=log, # type: ignore
47+
engineio_logger=False,
48+
)
2249
sio.attach(app)
2350

2451
app[APP_CLIENT_SOCKET_SERVER_KEY] = sio
52+
app.cleanup_ctx.append(_socketio_server_cleanup_ctx)
2553

2654
return get_socket_server(app)

services/web/server/tests/integration/01/test_garbage_collection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,12 @@ async def connect_to_socketio(client, user, socketio_client_factory: Callable):
279279
"user_id": str(user["id"]),
280280
"client_session_id": cur_client_session_id,
281281
}
282-
assert await socket_registry.find_keys(("socket_id", sio.sid)) == [resource_key]
283-
assert sio.sid in await socket_registry.find_resources(resource_key, "socket_id")
282+
assert await socket_registry.find_keys(("socket_id", sio.get_sid())) == [
283+
resource_key
284+
]
285+
assert sio.get_sid() in await socket_registry.find_resources(
286+
resource_key, "socket_id"
287+
)
284288
assert len(await socket_registry.find_resources(resource_key, "socket_id")) == 1
285289
sio_connection_data = sio, resource_key
286290
return sio_connection_data
@@ -289,12 +293,12 @@ async def connect_to_socketio(client, user, socketio_client_factory: Callable):
289293
async def disconnect_user_from_socketio(client, sio_connection_data):
290294
"""disconnect a previously connected socket.io connection"""
291295
sio, resource_key = sio_connection_data
292-
sid = sio.sid
296+
sid = sio.get_sid()
293297
socket_registry = get_registry(client.server.app)
294298
await sio.disconnect()
295299
assert not sio.sid
296300
await asyncio.sleep(0) # just to ensure there is a context switch
297-
assert not await socket_registry.find_keys(("socket_id", sio.sid))
301+
assert not await socket_registry.find_keys(("socket_id", sio.get_sid()))
298302
assert not sid in await socket_registry.find_resources(resource_key, "socket_id")
299303
assert not await socket_registry.find_resources(resource_key, "socket_id")
300304

0 commit comments

Comments
 (0)