[New-bugs-announce] [issue41696] asyncio.run interacts surprisingly with debug mode

Shantanu report at bugs.python.org
Wed Sep 2 20:33:50 EDT 2020


New submission from Shantanu <hauntsaninja at gmail.com>:

To quote the docs (https://docs.python.org/3/library/asyncio-dev.html):

```
By default asyncio runs in production mode. In order to ease the development asyncio has a debug mode.

There are several ways to enable asyncio debug mode:

    Setting the PYTHONASYNCIODEBUG environment variable to 1.

    Using the -X dev Python command line option.

    Passing debug=True to asyncio.run().

    Calling loop.set_debug().
```

Unfortunately, this documentation is misleading (or wrong) when using asyncio.run. For instance, from a naive read of the above, I'd expect debug mode to be True for many of the following:

```
~/tmp λ cat test57.py
import asyncio
import time

async def foo():
    loop = asyncio.get_event_loop()
    time.sleep(2)
    print(loop.get_debug())

print(asyncio.coroutines._DEBUG)
asyncio.run(foo())
~/tmp λ python3 --version       
Python 3.7.3

# expected
~/tmp λ python3 test57.py
False
False

# surprising, would expect this to work
~/tmp λ python3 -X dev test57.py
True
False

# surprising, would expect this to work
~/tmp λ PYTHONASYNCIODEBUG=1 python3 test57.py
True
False

# expected
~/tmp λ vim test57.py
~/tmp λ cat test57.py
import asyncio
import time

async def foo():
    loop = asyncio.get_event_loop()
    time.sleep(2)
    print(loop.get_debug())

print(asyncio.coroutines._DEBUG)
asyncio.run(foo(), debug=True)
~/tmp λ python3 test57.py       
False
True
Executing <Task finished coro=<foo() done, defined at test57.py:4> result=None created at /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:563> took 2.003 seconds
~/tmp λ 
```

This appears to be because asyncio.run sets debug mode here: https://github.com/python/cpython/blob/4a97b1517a6b5ff22e2984b677a680b07ff0ce11/Lib/asyncio/runners.py#L42

We should either change asyncio.run to use the existing value of debug mode if left unspecified, or we should update the documentation to explicitly call out that the environment variable and command line flag will not work when using asyncio.run

----------
components: asyncio
messages: 376268
nosy: asvetlov, hauntsaninja, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.run interacts surprisingly with debug mode

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41696>
_______________________________________


More information about the New-bugs-announce mailing list