Skip to content

gh-141982: Fix pdb can't set breakpoints on async functions#141983

Merged
gaogaotiantian merged 9 commits into
python:mainfrom
cocolato:fix_pdb_async
Dec 2, 2025
Merged

gh-141982: Fix pdb can't set breakpoints on async functions#141983
gaogaotiantian merged 9 commits into
python:mainfrom
cocolato:fix_pdb_async

Conversation

@cocolato

@cocolato cocolato commented Nov 26, 2025

Copy link
Copy Markdown
Member

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

bedevere-app Bot commented Nov 26, 2025

Copy link
Copy Markdown

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
Comment thread Misc/NEWS.d/next/Library/2025-11-26-15-36-55.gh-issue-141982.-8yHA3.rst Outdated
@aisk

aisk commented Nov 27, 2025

Copy link
Copy Markdown
Member

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.

@gaogaotiantian gaogaotiantian left a comment

Copy link
Copy Markdown
Member

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

bedevere-app Bot commented Nov 30, 2025

Copy link
Copy Markdown

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
Copy Markdown
Member Author

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

Comment thread Lib/test/test_pdb.py Outdated
Comment thread Lib/test/test_pdb.py Outdated
Comment thread Lib/test/test_pdb.py Outdated
@gaogaotiantian

Copy link
Copy Markdown
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
@cocolato cocolato deleted the fix_pdb_async branch January 20, 2026 14:11
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

5 participants