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

Unable to exit pdb when script becomes invalid #75304

Closed
jaraco opened this issue Aug 4, 2017 · 5 comments
Closed

Unable to exit pdb when script becomes invalid #75304

jaraco opened this issue Aug 4, 2017 · 5 comments
Labels
stdlib Python modules in the Lib dir

Comments

@jaraco
Copy link
Member

jaraco commented Aug 4, 2017

BPO 31121
Nosy @jaraco, @xdegaye, @hexagonrecursion

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 = None
created_at = <Date 2017-08-04.14:03:56.661>
labels = ['library']
title = 'Unable to exit pdb when script becomes invalid'
updated_at = <Date 2020-11-17.07:28:13.884>
user = 'https://github.com/jaraco'

bugs.python.org fields:

activity = <Date 2020-11-17.07:28:13.884>
actor = 'hexagonrecursion'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2017-08-04.14:03:56.661>
creator = 'jaraco'
dependencies = []
files = []
hgrepos = []
issue_num = 31121
keywords = []
message_count = 2.0
messages = ['299748', '381214']
nosy_count = 3.0
nosy_names = ['jaraco', 'xdegaye', 'hexagonrecursion']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue31121'
versions = ['Python 3.6']

@jaraco
Copy link
Member Author

jaraco commented Aug 4, 2017

Similar to bpo-16180 and possible a duplicate of bpo-14743, if the script being run under pdb becomes invalid such as through a chdir operation, pdb will get trapped in a loop, catching its own exception.

$ mkdir foo
$ cat > foo/script.py
import os
os.chdir('foo')
$ python -m pdb foo/script.py
> /Users/jaraco/Dropbox/code/privacymate/harvester/foo/script.py(1)<module>()
-> import os
(Pdb) c
The program finished and will be restarted
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1667, in main
    pdb._runscript(mainpyfile)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1545, in _runscript
    with open(filename, "rb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'foo/script.py'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py(1545)_runscript()
-> with open(filename, "rb") as fp:
(Pdb) q
Post mortem debugger finished. The foo/script.py will be restarted
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1667, in main
    pdb._runscript(mainpyfile)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1545, in _runscript
    with open(filename, "rb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'foo/script.py'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py(1545)_runscript()
-> with open(filename, "rb") as fp:
(Pdb) q
Post mortem debugger finished. The foo/script.py will be restarted
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1667, in main
    pdb._runscript(mainpyfile)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py", line 1545, in _runscript
    with open(filename, "rb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'foo/script.py'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py(1545)_runscript()
-> with open(filename, "rb") as fp:
(Pdb)

I suggest PDB should always pass exceptions when the stack trace terminates in the pdb module.

@jaraco jaraco added the stdlib Python modules in the Lib dir label Aug 4, 2017
@hexagonrecursion
Copy link
Mannequin

hexagonrecursion mannequin commented Nov 17, 2020

Another way to reproduce this that will continue to work once bpo-42383 is fixed is to delete or move the script while it's being debugged.

@gaogaotiantian
Copy link
Member

This does not reproduce in main anymore because the script path is remembered before it's being executed. chdir will not trap pdb anymore.

@jaraco
Copy link
Member Author

jaraco commented Feb 6, 2024

I believe the issue is still reproducible. I reproduced in on Python 3.12.1, this time using the technique of deleting the file:

 draft @ mkdir foo
 draft @ cat > foo/script.py
import os
os.remove('foo/script.py')
raise ValueError()
 draft @ py -3.12 -m pdb foo/script.py
> /Users/jaraco/draft/foo/script.py(1)<module>()
-> import os
(Pdb) c
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1925, in main
    pdb._run(target)
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1719, in _run
    self.run(target.code)
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/bdb.py", line 600, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "/Users/jaraco/draft/foo/script.py", line 3, in <module>
ValueError
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Users/jaraco/draft/foo/script.py(3)<module>()
(Pdb) q
Post mortem debugger finished. The /Users/jaraco/draft/foo/script.py will be restarted
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1925, in main
    pdb._run(target)
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1719, in _run
    self.run(target.code)
             ^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 160, in code
    with io.open_code(self) as fp:
         ^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/Users/jaraco/draft/foo/script.py'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py(160)code()
-> with io.open_code(self) as fp:
(Pdb) q
Post mortem debugger finished. The /Users/jaraco/draft/foo/script.py will be restarted
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1925, in main
    pdb._run(target)
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 1719, in _run
    self.run(target.code)
             ^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py", line 160, in code
    with io.open_code(self) as fp:
         ^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/Users/jaraco/draft/foo/script.py'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /opt/homebrew/Cellar/python@3.12/3.12.1_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pdb.py(160)code()
-> with io.open_code(self) as fp:

I've tested on main as well, at 7fdd423, and indeed the repro with deleting the file no longer falls into the loop:

 cpython main @ mkdir foo
 cpython main @ cat > foo/script.py
import os
os.remove('foo/script.py')
raise ValueError()
 cpython main @ ./python.exe -m pdb foo/script.py
> /Users/jaraco/code/python/cpython/foo/script.py(1)<module>()
-> import os
(Pdb) c
Traceback (most recent call last):
  File "/Users/jaraco/code/python/cpython/Lib/pdb.py", line 2294, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "/Users/jaraco/code/python/cpython/Lib/pdb.py", line 2058, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "/Users/jaraco/code/python/cpython/Lib/bdb.py", line 607, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/Users/jaraco/code/python/cpython/foo/script.py", line 3, in <module>
ValueError
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Users/jaraco/code/python/cpython/foo/script.py(3)<module>()
(Pdb) q
Post mortem debugger finished. The /Users/jaraco/code/python/cpython/foo/script.py will be restarted

@gaogaotiantian
Copy link
Member

Yeah it's fixed in main but not 3.12. It's also not a trivial fix so I'm not sure if we will backport the fix. The issue has been there for more than 6 years anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

2 participants