classification
Title: AsyncIO strange behaviour
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: asyncio.create_task() documentation should mention user needs to keep reference to the task
View: 44665
Assigned To: Nosy List: asvetlov, greckov, nanjekyejoannah, ncoghlan, yselivanov
Priority: normal Keywords:

Created on 2020-12-02 12:17 by greckov, last changed 2021-10-22 14:42 by nanjekyejoannah. This issue is now closed.

Files
File name Uploaded Description Edit
output.txt greckov, 2020-12-02 12:17
Messages (6)
msg382307 - (view) Author: Alexander Greckov (greckov) Date: 2020-12-02 12:17
Hi! I've met strange behaviour related to the coroutine execution. Probably it's somehow related to the asyncio.Queue get() method. I have the following lines of code:
class WeightSource:
    def __init__(self):
        self._queue = asyncio.Queue(maxsize=1)
    def __aiter__(self):
        return self
    async def __anext__(self):
        return await self._queue.get()

async def _preconfigure(app: web.Application) -> None:
    setup_db()
    # asyncio.create_task(WeightSource.listen_scales('/dev/ttyACM0', 9600)
    asyncio.create_task(handle_weight_event_creation()) # coroutine prints WEIGHT <int>
    await create_track_tasks()

async def create_track_tasks():
    asyncio.create_task(track_scales_availability())
    asyncio.create_task(track_crm_availability())

async def track_scales_availability():
    async for weight in WeightSource():
        print(weight)

When I'm trying to run _preconfigure coroutine (automatically started by aiohttp), i see the message: ERROR:asyncio:Task was destroyed but it is pending. The strange things two:
The process and loop remains alive (so by the logic error should be not shown) and the second thing is when I export PYTHONASYNCIODEBUG=1 everything works well without any error. 
On unset this variable the error returns. When I don't use asyncio.Queue and just place asyncio.sleep(), coroutine doesn't fall. Error happens inside the `async for weight in WeightSource()` statement. Why PYTHONASYNCIODEBUG changes behaviour?
msg382309 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-12-02 12:35
I understand the issue as the following:

1. You don't save the result of `asyncio.create_task()` somewhere.
The task object is dereferenced and thus you see a warning.

2. With PYTHONASYNCIODEBUG on the task is referenced also by internal debug structure, that's why you don't see the warning.

Perhaps we should store a weak reference to task in _source_traceback

Minor thing but nice to have it fixed.
msg382310 - (view) Author: Alexander Greckov (greckov) Date: 2020-12-02 12:50
Thanks! That's resolved my problem, but this thing wasn't really obvious as for me. Probably it would be better to write about this explicitly in docs for the create_task. All in all this issue can be closed.
msg400194 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2021-08-24 02:29
While filed later, issue44665 correctly describes the problem reported here: asyncio is behaving as expected, but the docs don't make it clear that you need to save a reference to the result of ensure_future()/create_task() if you don't want to risk the task unexpectedly disappearing mid execution.

So marking this as a duplicate of that docs issue.
msg404777 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2021-10-22 14:42
I opened a documentation PR agreeing with @ncoghlan, that adds this behavior. See https://bugs.python.org/issue44665
msg404778 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2021-10-22 14:42
I meant, "that describes the behaviour"
History
Date User Action Args
2021-10-22 14:42:50nanjekyejoannahsetmessages: + msg404778
2021-10-22 14:42:14nanjekyejoannahsetnosy: + nanjekyejoannah
messages: + msg404777
2021-08-24 02:30:11ncoghlansetstatus: open -> closed
superseder: asyncio.create_task() documentation should mention user needs to keep reference to the task
resolution: duplicate
stage: resolved
2021-08-24 02:29:36ncoghlansetnosy: + ncoghlan
messages: + msg400194
2020-12-02 12:50:02greckovsetmessages: + msg382310
2020-12-02 12:35:29asvetlovsetmessages: + msg382309
2020-12-02 12:17:27greckovcreate