Skip to content

Conversation

yihong0618
Copy link
Contributor

@yihong0618 yihong0618 commented Sep 8, 2025

breakpoint() in pdb the self.curframe will be None
and run something will cause crash

this patch fix it. but not sure if it is worth. if not will close.

@yihong0618 yihong0618 changed the title fix: breakpoint in pdb can cause crash gh-138641: fix breakpoint in pdb can cause crash Sep 8, 2025
@picnixz picnixz marked this pull request as draft September 8, 2025 09:52
@picnixz
Copy link
Member

picnixz commented Sep 8, 2025

If you're not sure if your patch is correct yet or not, let's put it as a draft first.

picnixz
picnixz previously requested changes Sep 8, 2025
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

There are other places where curframe is used such as completedefault. Please check all the places carefully.

@bedevere-app
Copy link

bedevere-app bot commented Sep 8, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@yihong0618
Copy link
Contributor Author

If you're not sure if your patch is correct yet or not, let's put it as a draft first.

copy will change and open it

@yihong0618
Copy link
Contributor Author

There are other places where curframe is used such as completedefault. Please check all the places carefully.

copy will check

@yihong0618 yihong0618 changed the title gh-138641: fix breakpoint in pdb can cause crash gh-138641: fix breakpoint in pdb can cause error Sep 8, 2025
@picnixz picnixz changed the title gh-138641: fix breakpoint in pdb can cause error gh-138641: prevent accessing non-existing curframe in pdb commands Sep 8, 2025
Signed-off-by: yihong0618 <[email protected]>
@picnixz picnixz marked this pull request as ready for review September 8, 2025 10:15
@yihong0618
Copy link
Contributor Author

yihong0618 commented Sep 8, 2025

There are other places where curframe is used such as completedefault. Please check all the places carefully.

check something others seems not raise error, I will left them

Signed-off-by: yihong0618 <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
@picnixz
Copy link
Member

picnixz commented Sep 8, 2025

others seems not raise error, I will left them

completedefault appears to be handled, so let's add an assert self.curframe at the beginning of the function. However, the following raise:

>>> breakpoint()
> <python-input-0>(1)<module>()
(Pdb) breakpoint()
(Pdb) debug 1
>>> breakpoint()
> <python-input-0>(1)<module>()
(Pdb) breakpoint()
(Pdb) break 1

And defaultFile may need to be patched as well because do_break() calls defaultFile(). The exceptions in do_break are silenced so there shouldn't be a need for patching them (there are bare except). However you need to patch do_debug and do_break.

@yihong0618
Copy link
Contributor Author

others seems not raise error, I will left them

completedefault appears to be handled, so let's add an assert self.curframe at the beginning of the function. However, the following raise:

>>> breakpoint()
> <python-input-0>(1)<module>()
(Pdb) breakpoint()
(Pdb) debug 1
>>> breakpoint()
> <python-input-0>(1)<module>()
(Pdb) breakpoint()
(Pdb) break 1

And defaultFile may need to be patched as well because do_break() calls defaultFile(). The exceptions in do_break are silenced so there shouldn't be a need for patching them (there are bare except). However you need to patch do_debug and do_break.

fixed and failed test seems not releate

Signed-off-by: yihong0618 <[email protected]>
Signed-off-by: yihong0618 <[email protected]>
@yihong0618
Copy link
Contributor Author

careful with another case
does it need to fix here?

Python 3.15.0a0 (heads/hy/inside_pdb_breakpoints_can_crash-dirty:cb73cb0750, Sep  8 2025, 19:02:2) [Clang 17.0.0 (clang-1700.0.13.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> breakpoint()
> <python-input-0>(1)<module>()
(Pdb) break 1
End of file
(Pdb) breakpoint()
(Pdb) breakpoint()
*** No current frame.
(Pdb) break 1
*** No current frame.
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    breakpoint()
    ~~~~~~~~~~^^
  File "/Users/yihong/repos/cpython/Lib/bdb.py", line 94, in wrapper
    ret = func(frame, *args)
  File "/Users/yihong/repos/cpython/Lib/bdb.py", line 154, in opcode_callback
    frame.f_trace(frame, 'opcode', None)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yihong/repos/cpython/Lib/bdb.py", line 289, in trace_dispatch
    return self.dispatch_opcode(frame, arg)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/yihong/repos/cpython/Lib/bdb.py", line 406, in dispatch_opcode
    self.user_opcode(frame)
    ~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/yihong/repos/cpython/Lib/pdb.py", line 547, in user_line
    self.interaction(frame, None)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/yihong/repos/cpython/Lib/pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "/Users/yihong/repos/cpython/Lib/pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "/Users/yihong/repos/cpython/Lib/cmd.py", line 149, in cmdloop
    stop = self.onecmd(line)
  File "/Users/yihong/repos/cpython/Lib/pdb.py", line 1041, in onecmd
    return cmd.Cmd.onecmd(self, line)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/yihong/repos/cpython/Lib/cmd.py", line 227, in onecmd
    return func(arg)
  File "/Users/yihong/repos/cpython/Lib/pdb.py", line 1399, in do_break
    filename = self.canonic(filename)
  File "/Users/yihong/repos/cpython/Lib/bdb.py", line 221, in canonic
    if filename == "<" + filename[1:-1] + ">":
                         ~~~~~~~~^^^^^^
TypeError: 'NoneType' object is not subscriptable

yihong0618 and others added 4 commits September 8, 2025 19:35
Signed-off-by: yihong0618 <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Signed-off-by: yihong0618 <[email protected]>
Signed-off-by: yihong0618 <[email protected]>
Co-authored-by: Tian Gao <[email protected]>
Lib/pdb.py Outdated
# Check if we're already in a pdb session by examining the call stack
frame = sys._getframe()
while frame:
if frame.f_code.co_name == 'interaction' and frame.f_code.co_filename.endswith('pdb.py'):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

seems can into a function

@picnixz picnixz dismissed their stale review September 8, 2025 14:44

logic has been changed as per Tian's suggestions

@picnixz
Copy link
Member

picnixz commented Sep 8, 2025

I'll bail from shepherding this one as Tian's is the best qualified here. Sorry for the un-necessary work that I made you do! (and thank you for your sponsor ❤️)

@yihong0618
Copy link
Contributor Author

yihong0618 commented Sep 8, 2025

I'll bail from shepherding this one as Tian's is the best qualified here. Sorry for the un-necessary work that I made you do! (and thank you for your sponsor ❤️)

no need~, my English is bad, sorry for that,
and I learned a lot from this.

Signed-off-by: yihong0618 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants