This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: wait_for(coro, timeout=0) memleak
Type: resource usage Stage:
Components: asyncio Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, ixje, yselivanov
Priority: normal Keywords:

Created on 2019-05-25 12:09 by ixje, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test_leak_minimal.py ixje, 2019-05-25 12:09 minimal example that bleeds memory
test_leak_minimal_mem_consumption.png ixje, 2019-05-25 12:26
Messages (6)
msg343470 - (view) Author: ixje (ixje) Date: 2019-05-25 12:09
I have a networked process that looks somewhat like this in its most basic form

```
import asyncio

shutting_down = False

async def read_message(reader, timeout=30):
    async def _read(reader: asyncio.StreamReader):
        try:
            d = await reader.readexactly(24)
            # do something with the data
            print("I'm never called")
        except:
            return None

    try:
        return await asyncio.wait_for(_read(reader), timeout)
    except Exception:
        return None


async def my_service():
    reader, writer = await asyncio.open_connection('127.0.0.1', 20333)
    while not shutting_down:
        m = await read_message(reader, timeout=0)
        if m is None:
            continue
        # else process message


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(my_service())
    loop.run_forever()
```

read_message() has a default timeout of 30, but I thought setting it to 0 (instead of None) would be equal to blocking. This bleeds 16GB of memory in ~3 minutes. A minimal example is provided.

I manually applied the patch of https://bugs.python.org/issue36613 to a self compiled build of 3.7.3 (https://github.com/python/cpython/commit/ef4ec6ed12d6c6200a85068f60483723298b6ff4) on Ubuntu 18.04 and that did not solve the problem.
msg343472 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019-05-25 12:19
Thanks for the report.
If the problem  is in asyncio.wait_for() function the real network code can be stripped for the leakage example and replaced with `await asyncio.sleep()`.

Would you try to boil down the snippet by converting it into a code that I can execute on my laptop to reproduce the problem?
msg343473 - (view) Author: ixje (ixje) Date: 2019-05-25 12:22
Hi Andrew, 
There is an attached minimal example (that differs from the code given in the first comment). I couldn't attach 2 files. So I pasted the code of one file to showcase how we could run into the issue, then a minimal reproducible example without network code in the attachment.
msg343475 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019-05-25 12:25
Nice, thanks!
msg343476 - (view) Author: ixje (ixje) Date: 2019-05-25 12:26
This is the consumption I'm seeing.
msg343477 - (view) Author: ixje (ixje) Date: 2019-05-25 12:34
Perhaps also worth mentioning is that when we supply None as timeout value in the `wait_for()` in the minimal sample, then it still reports 60MB memory usage. Seems pretty steep for doing basically nothing but looping around.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81223
2019-05-25 12:34:53ixjesetmessages: + msg343477
2019-05-25 12:26:15ixjesetfiles: + test_leak_minimal_mem_consumption.png

messages: + msg343476
2019-05-25 12:25:31asvetlovsetmessages: + msg343475
2019-05-25 12:22:12ixjesetmessages: + msg343473
2019-05-25 12:19:50asvetlovsetmessages: + msg343472
2019-05-25 12:09:54ixjecreate