Skip to content

Conversation

@RytoEX
Copy link
Member

@RytoEX RytoEX commented Jan 7, 2026

Description

Update event loop usage for Python 3.11+.

Motivation and Context

Corrects/Updates event loop usage to address this warning:

simplehttp.py:11: DeprecationWarning: There is no current event loop
loop = asyncio.get_event_loop()

However, this surfaces new errors when quitting the application:

ResourceWarning: unclosed event loop <ProactorEventLoop running=False closed=False debug=False>

Adding a loop.close() to the end of the finally block resolves this.

However, other errors persist:

[ERROR] [default_exception_handler] Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<IocpProactor.accept.<locals>.accept_coro() running at pythoncore-3.14-64\Lib\asyncio\windows_events.py:567> wait_for=<_OverlappedFuture cancelled>>
[ERROR] [default_exception_handler] Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<IocpProactor.accept.<locals>.accept_coro() running at pythoncore-3.14-64\Lib\asyncio\windows_events.py:567> wait_for=<_OverlappedFuture cancelled>>
<sys>:0: ResourceWarning: unclosed <socket.socket fd=872, family=2, type=1, proto=0, laddr=('127.0.0.1', 8084)>
<sys>:0: ResourceWarning: unclosed <socket.socket fd=736, family=2, type=1, proto=0>
<sys>:0: ResourceWarning: unclosed <socket.socket fd=748, family=23, type=1, proto=0, laddr=('::1', 8084, 0, 0)>
<sys>:0: ResourceWarning: unclosed <socket.socket fd=508, family=23, type=1, proto=0>
[ERROR] [default_exception_handler] Task was destroyed but it is pending!
task: <Task cancelling name='Task-1' coro=<_run_app() running at .venv\Lib\site-packages\aiohttp\web.py:423> wait_for=<Future cancelled>>

However, these only occur on exit, and I'm not familiar enough with Python asyncio dev to address them.

Python 3.10:
https://docs.python.org/3.10/library/asyncio-eventloop.html#asyncio.get_event_loop
https://docs.python.org/3.10/library/asyncio-eventloop.html#hello-world-with-call-soon

Python 3.11:
https://docs.python.org/3.11/library/asyncio-eventloop.html#asyncio.get_event_loop
https://docs.python.org/3.11/library/asyncio-eventloop.html#hello-world-with-call-soon

Python 3.14:
https://docs.python.org/3.14/library/asyncio-eventloop.html#asyncio.get_event_loop
https://docs.python.org/3.14/library/asyncio-eventloop.html#hello-world-with-call-soon

How Has This Been Tested?

Tested locally on Windows with Python 3.11 and 3.14.

Types of changes

  • Tweak (non-breaking change to improve existing functionality)
  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@RytoEX RytoEX requested a review from tt2468 January 7, 2026 22:30
@RytoEX
Copy link
Member Author

RytoEX commented Jan 7, 2026

cc @Penwy @mihawk90 @prgmitchell

@Penwy
Copy link
Contributor

Penwy commented Jan 7, 2026

task.cancel() does not immediately cancel the task, but tells the task to be cancelled on the next cycle of the event loop.
Since by the point we're calling it, the loop isn't running, the task is never cancelled, hence the error.
So, basically, we need to run the loop once more after sending the cancel.
This should do it :

    finally:
        logging.info('Exiting application.')
        while applicationTask.cancel():  # Shuts down the HTTP server
            loop.call_soon(loop.stop)
            loop.run_forever()
        threadPool.shutdown()  # Shuts down the running thread pool
        loop.close()

I however do not guarantee it is the best way of doing it, ideally, we shouldn't be using run_forever at all, but this is another question.

@RytoEX
Copy link
Member Author

RytoEX commented Jan 8, 2026

From off-thread discussion, @tt2468 will take a look at this later.

@RytoEX
Copy link
Member Author

RytoEX commented Jan 8, 2026

Superseded by #197. cc @Penwy

@RytoEX RytoEX closed this Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants