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: asyncio.run interacts surprisingly with debug mode
Type: Stage: resolved
Components: asyncio Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, hauntsaninja, miss-islington, yselivanov
Priority: normal Keywords: patch

Created on 2020-09-03 00:33 by hauntsaninja, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22069 merged hauntsaninja, 2020-09-03 01:32
PR 22071 merged miss-islington, 2020-09-03 04:55
PR 22072 merged miss-islington, 2020-09-03 04:55
Messages (4)
msg376268 - (view) Author: Shantanu (hauntsaninja) * Date: 2020-09-03 00:33
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
msg376269 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-09-03 01:11
Yes, this is a bug. The default for the `debug` parameter should be `None` and it should only call `loop.set_debug` if a non-None bool value was explicitly passed. Care to submit a PR?
msg376282 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-09-03 04:54
New changeset 0770ad948cb6d9f7f6c4002efd83e27c27069808 by Shantanu in branch 'master':
bpo-41696: Fix handling of debug mode in asyncio.run (#22069)
https://github.com/python/cpython/commit/0770ad948cb6d9f7f6c4002efd83e27c27069808
msg376318 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-09-03 20:54
New changeset 1f5f12737755f246fee83a54c600779ad76934a4 by Miss Islington (bot) in branch '3.8':
bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) (#22072)
https://github.com/python/cpython/commit/1f5f12737755f246fee83a54c600779ad76934a4
msg376319 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2020-09-03 20:54
New changeset a2680058ee1f43cab282e9f4c2b35fd89464ebb6 by Miss Islington (bot) in branch '3.9':
bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) (#22071)
https://github.com/python/cpython/commit/a2680058ee1f43cab282e9f4c2b35fd89464ebb6
History
Date User Action Args
2022-04-11 14:59:35adminsetgithub: 85862
2022-03-12 20:01:32asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-09-03 20:54:15yselivanovsetmessages: + msg376319
2020-09-03 20:54:15yselivanovsetmessages: + msg376318
2020-09-03 04:55:19miss-islingtonsetpull_requests: + pull_request21161
2020-09-03 04:55:12miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21160
2020-09-03 04:54:54yselivanovsetmessages: + msg376282
2020-09-03 01:32:59hauntsaninjasetkeywords: + patch
stage: patch review
pull_requests: + pull_request21159
2020-09-03 01:11:48yselivanovsetmessages: + msg376269
2020-09-03 00:33:50hauntsaninjacreate