Skip to content

3.3.0 broken - switching from asyncio.get_event_loop() to asyncio.run() causing major issues #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Zippy1999 opened this issue Mar 11, 2025 · 4 comments

Comments

@Zippy1999
Copy link

Zippy1999 commented Mar 11, 2025

In version 3.2.0, calls to get the running event loop were executed with asyncio.get_event_loop / run_until_complete

In version 3.3.0 asyncio.run() has been used instead. This closes the running event loop after the code is executed.

When using this in lambda code which needs to run asyncio.get_event_loop it causes subsequent executions to fail

The failure errors:

ersions/3.11.9/lib/python3.11/asyncio/events.py", line 683, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'MainThread'.

The changed code is in __init__.py in the metric_scope decorator.

@Zippy1999 Zippy1999 changed the title 3.3.0 broken - switching from asyncio.get_event_loop() to asyncio.run() causing major issues in test suites 3.3.0 broken - switching from asyncio.get_event_loop() to asyncio.run() causing major issues Mar 11, 2025
@Zippy1999
Copy link
Author

Running code in aws lambda with python 3.11 runtime

@SamStephens
Copy link

@Zippy1999 are you using the hack I documented in this issue?

@metric_scope
def handler(event, context, metrics):
    try:
        # Actual handler logic
    finally:
        # Need to call flush like this because it's a coroutine/asynchronous
        loop = asyncio.get_event_loop()
        loop.run_until_complete(metrics.flush())

Because it looks like the change in 3.3.0 broke this hack - in which case I think the right thing to do is remove the hack.

@Zippy1999
Copy link
Author

Zippy1999 commented Mar 12, 2025

@SamStephens thanks for responding 👍

I've checked the code in the affected lambda - we are not currently implementing this hack.

I know that run() would be the preferred one to use especially given that get_event_loop() is being deprecated.

I am investigating to see how much work it would be to move to replace any instances of get_event_loop() / get_running_loop() with asyncio.run() in the lambda affected. This could potentially be problematic though as I know of at least one dependency which the lambda uses which utilises get_running_loop()

I know we can't stop progress nor would I want to 😆 - but a small warning on the release notes of 3.3.0 may be helpful to others of a potential "breaking" change? 😅

@SamStephens
Copy link

@Zippy1999 sorry, I missed that you're using asyncio.get_event_loop in your code - my bad.

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

No branches or pull requests

2 participants