Skip to content

Commit f6e25f4

Browse files
munozcojourdain
authored andcommitted
fix: create event loop if there is none
1 parent fe74ba7 commit f6e25f4

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/wslink/core.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ def schedule_callback(delay, callback, *args, **kwargs):
4040
# Using "asyncio.get_running_loop()" requires the event loop to be running
4141
# already, so we use "asyncio.get_event_loop()" here so that we can support
4242
# scheduling tasks before the server is started.
43-
loop = asyncio.get_event_loop()
43+
# starting from python 3.14, "asyncio.get_event_loop()" fails if there is no event loop
44+
try:
45+
loop = asyncio.get_event_loop()
46+
except RuntimeError:
47+
loop = asyncio.new_event_loop()
48+
asyncio.set_event_loop(loop)
4449
return loop.call_later(delay, functools.partial(callback, *args, **kwargs))
4550

4651

@@ -61,7 +66,11 @@ def schedule_coroutine(delay, coro_func, *args, done_callback=None, **kwargs):
6166
'<coro-name>' was never awaited".
6267
"""
6368
# See method above for comment on "get_event_loop()" vs "get_running_loop()".
64-
loop = asyncio.get_event_loop()
69+
try:
70+
loop = asyncio.get_event_loop()
71+
except RuntimeError:
72+
loop = asyncio.new_event_loop()
73+
asyncio.set_event_loop(loop)
6574
coro_partial = functools.partial(coro_func, *args, **kwargs)
6675
if done_callback is not None:
6776
return loop.call_later(

src/wslink/protocol.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ def __init__(self, server_config):
3131
self._last_active_client_id = None
3232
self._config = server_config
3333
self._shutdown_task = None
34-
self._completion = asyncio.get_event_loop().create_future()
34+
try:
35+
loop = asyncio.get_event_loop()
36+
except RuntimeError:
37+
loop = asyncio.new_event_loop()
38+
asyncio.set_event_loop(loop)
39+
self._completion = loop.create_future()
3540
self._app = None
3641

3742
# -------------------------------------------------------------------------

src/wslink/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ def start_webserver(
260260
# asyncio.run(ws_server.start())
261261

262262
# Until then, we can start the server this way
263-
loop = asyncio.get_event_loop()
263+
try:
264+
loop = asyncio.get_event_loop()
265+
except RuntimeError:
266+
loop = asyncio.new_event_loop()
267+
asyncio.set_event_loop(loop)
264268

265269
port_callback = None
266270
if hasattr(wslinkServer, "port_callback"):

0 commit comments

Comments
 (0)