Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

asyncio.run interacts surprisingly with debug mode #85862

Closed
hauntsaninja opened this issue Sep 3, 2020 · 5 comments
Closed

asyncio.run interacts surprisingly with debug mode #85862

hauntsaninja opened this issue Sep 3, 2020 · 5 comments

Comments

@hauntsaninja
Copy link
Contributor

BPO 41696
Nosy @asvetlov, @1st1, @miss-islington, @hauntsaninja
PRs
  • bpo-41696: Fix handling of debug mode in asyncio.run #22069
  • [3.9] bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) #22071
  • [3.8] bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) #22072
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-03-12.20:01:32.982>
    created_at = <Date 2020-09-03.00:33:50.817>
    labels = ['expert-asyncio']
    title = 'asyncio.run interacts surprisingly with debug mode'
    updated_at = <Date 2022-03-12.20:01:32.982>
    user = 'https://github.com/hauntsaninja'

    bugs.python.org fields:

    activity = <Date 2022-03-12.20:01:32.982>
    actor = 'asvetlov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-12.20:01:32.982>
    closer = 'asvetlov'
    components = ['asyncio']
    creation = <Date 2020-09-03.00:33:50.817>
    creator = 'hauntsaninja'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41696
    keywords = ['patch']
    message_count = 4.0
    messages = ['376268', '376269', '376282', '376318', '376319']
    nosy_count = 4.0
    nosy_names = ['asvetlov', 'yselivanov', 'miss-islington', 'hauntsaninja']
    pr_nums = ['22069', '22071', '22072']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue41696'
    versions = []

    @hauntsaninja
    Copy link
    Contributor Author

    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:

    loop.set_debug(debug)

    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

    @1st1
    Copy link
    Member

    1st1 commented Sep 3, 2020

    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?

    @1st1
    Copy link
    Member

    1st1 commented Sep 3, 2020

    New changeset 0770ad9 by Shantanu in branch 'master':
    bpo-41696: Fix handling of debug mode in asyncio.run (bpo-22069)
    0770ad9

    @1st1
    Copy link
    Member

    1st1 commented Sep 3, 2020

    New changeset 1f5f127 by Miss Islington (bot) in branch '3.8':
    bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) (bpo-22072)
    1f5f127

    @1st1
    Copy link
    Member

    1st1 commented Sep 3, 2020

    New changeset a268005 by Miss Islington (bot) in branch '3.9':
    bpo-41696: Fix handling of debug mode in asyncio.run (GH-22069) (bpo-22071)
    a268005

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants