Skip to content
New issue

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

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

Already on GitHub? # to your account

Immediate async tasks are not awaited #5929

Open
mdellweg opened this issue Oct 23, 2024 · 5 comments
Open

Immediate async tasks are not awaited #5929

mdellweg opened this issue Oct 23, 2024 · 5 comments
Labels

Comments

@mdellweg
Copy link
Member

mdellweg commented Oct 23, 2024

Tasks defined as async coroutines are not properly handled when run immediate by the api worker.

@gerrod3
Copy link
Contributor

gerrod3 commented Feb 3, 2025

How so, am I missing something it looks like they are:

if asyncio.iscoroutine(result):
_logger.debug(_("Task is coroutine %s"), task.pk)
loop = asyncio.get_event_loop()
loop.run_until_complete(result)

@pedro-psb
Copy link
Member

Hmm, if you see the line above, the actual task code (func) is called as a sync function.
This is checking the task return value is a coroutine. I didn't know we even did something with the task fn return value.
Or that's how async tasks are supposed to be? Like wrapped in a sync function.

Also, do you know any task defined as an async coroutine? I feel like Mathias once said that there were none, really.

result = func(*args, **kwargs)
if asyncio.iscoroutine(result):
_logger.debug(_("Task is coroutine %s"), task.pk)
loop = asyncio.get_event_loop()
loop.run_until_complete(result)

@gerrod3
Copy link
Contributor

gerrod3 commented Feb 4, 2025

>>> import asyncio
>>> async def hello():
...     print("hello")
...     
>>> a = hello()
>>> asyncio.iscoroutine(a)
True
>>> loop = asyncio.get_event_loop()
<python-input-4>:1: DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()
>>> loop = asyncio.new_event_loop()
>>> loop.run_until_complete(a)
hello

With my current understanding of asyncio I think we have valid handling code, the async code won't run till put into an event loop. Our only potential problem is that I don't know what the status of the current event loop is in the api worker, so we might be calling deprecated code (deprecated in python 3.12 to call get_event_loop without a running loop).

@pedro-psb
Copy link
Member

Oh, I see, thanks for clarifying! Asyncio is not in my heart yet 😅

@mdellweg
Copy link
Member Author

mdellweg commented Feb 6, 2025

First, yes, we don't have any async tasks yet. But also I cannot see what tipped me off back then. Maybe it got fixed in the meantime... I'm OK with closing this one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants