Skip to content

Conversation

@cocolato
Copy link
Contributor

@cocolato cocolato commented Nov 26, 2025

Fix: #141982

I found that while debugging with pdb you currently cannot set a breakpoint on an async function. Reproducer:

import asyncio

async def main():
    print(f"Hello")
    await asyncio.sleep(1)
    print(f"World!")

asyncio.run(main())

If you run pdb and try to set a breakpoint on main you get:

λ › ./python -m pdb main.py                                                                                                                                          
> /cpython/main.py(1)<module>()
-> import asyncio
(Pdb) break main
*** The specified object 'main' is not a function or was not found along sys.path.
(Pdb) quit

This happens because the regular expression used by find_function does not match async functions:

cpython/Lib/pdb.py

Lines 132 to 133 in 2ff8608

def find_function(funcname, filename):
cre = re.compile(r'def\s+%s(\s*\[.+\])?\s*[(]' % re.escape(funcname))

After the fix:

λ › ./python -m pdb main.py                                                                                                                                          
> /cpython/main.py(1)<module>()
-> import asyncio
(Pdb) break main
Breakpoint 1 at /cpython/main.py:4
(Pdb) continue
> /cpython/main.py(4)main()
-> print(f"Hello")
(Pdb) quit

Now we can set breakpoints on async functions as expected.

@bedevere-app
Copy link

bedevere-app bot commented Nov 26, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@aisk aisk added stdlib Standard Library Python modules in the Lib/ directory and removed stdlib Standard Library Python modules in the Lib/ directory labels Nov 26, 2025
@aisk
Copy link
Contributor

aisk commented Nov 27, 2025

I'm not familiar with the pdb module, but I think that even after this change, functions defined within an if branch or other indented blocks still can't be found by this function. Perhaps we could use another approach, like walking the AST in the find_function to handle this situation.

For now, however, I think it's acceptable to just handle the async function case described in the original issue because this is the most common situation.

Copy link
Member

@gaogaotiantian gaogaotiantian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's optimize the test case a bit. So we want a test case that is accurate and comprehensive, while being concise. The sleep and World part did not do anything - we don't need them.

Checking Hello is not the best practice because there could be many ways that Hello appears in stdout - it's part of the source code and you are printing it. Say the breakpoint is never set, you'd still have Hello in stdout.

One way is to check World is not printed out - which could be an indicator that a breakpoint is set. For Hello I think we need to check it in a better way.

@bedevere-app
Copy link

bedevere-app bot commented Nov 30, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@cocolato
Copy link
Contributor Author

Thanks for review, @gaogaotiantian ! Do you think the current tests are sufficient?

@gaogaotiantian
Copy link
Member

Thank you for fixing this.

Skip coroutine tests if SKIP_CORO_TESTS is True.
@gaogaotiantian gaogaotiantian merged commit fddc24e into python:main Dec 2, 2025
46 checks passed
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pdb breakpoints on async functions

4 participants