How to quit ipdb while in post-mortem debugging?
Asked Answered
C

3

34

I like to inspect error in a Python script by using:

$ python3 -m pdb my_script.py

This drops me into a pdb prompt from where I can c continue the execution, and when it hits error, I can inspect the variables and then q quit the script execution to get back to my shell.

I tried the same with iPython debugger module, since it is more colorful:

$ python3 -m ipdb my_script.py

However, I am not able to quit the debugger once I am done inspecting the error. Using the q quit command just keeps switching it between re-executing the script and post-mortem mode:

$ python3 -m ipdb my_script.py
ipdb> c
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> Inspect some variables at this point
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program

How to quit this debugger?

Centerpiece answered 23/8, 2016 at 0:52 Comment(6)
Does ^C or ^D not work? I've occasionally gotten something similar and had to ^C several times to exitPatella
@Patella That doesn't work either!Centerpiece
Worst comes to worst, ^Z, then a kill %1 should stop itPatella
Possible duplicate of Exiting Python Debugger ipdbCenterpiece
What's your ipython version? This problem does reproduce in ipython==5.1.1. Check out this comment: github.com/gotcha/ipdb/issues/111Topnotch
^Z does not work either. ipdb catches it and displays ^Z.Tackling
L
11

This was a bug in IPython 5.1. It was fixed in this pull request and is no longer an issue from IPython 5.2 and onwards. You can now use q, quit(), or Ctrl+d to exit the debugger.

Life answered 25/2, 2017 at 23:42 Comment(3)
quit operation seems like slower than than using pdb itself. Is it a normal case?Ninfaningal
Quitting the debugger is immediate for me.Life
Seems like it jumps to the next breakpoint instead of quitting when I do qNinfaningal
C
47

As the user @ffeast commented, there is an open ipdb issue, and a few workarounds suggested. For me these worked well:

  • press ctrl+z and kill %1 (or whatever the Job number is)
  • execute ipdb> import os; os._exit(1)
Caesalpiniaceous answered 27/10, 2016 at 8:0 Comment(3)
ctrl+z won't really exit the process, it just sends it to the background. For example, you can get back into the debugger by using the command fg 1, you will see the debugger is still there. The second one import os; os._exit(1) worked in my case.Bestow
True, updated answer with a follow up to kill bg process.Caesalpiniaceous
Can import os; os._exit(1) be set to an alias within ipdb?Ninfaningal
L
11

This was a bug in IPython 5.1. It was fixed in this pull request and is no longer an issue from IPython 5.2 and onwards. You can now use q, quit(), or Ctrl+d to exit the debugger.

Life answered 25/2, 2017 at 23:42 Comment(3)
quit operation seems like slower than than using pdb itself. Is it a normal case?Ninfaningal
Quitting the debugger is immediate for me.Life
Seems like it jumps to the next breakpoint instead of quitting when I do qNinfaningal
B
3

Use ctrl+z or open a second terminal, then look for the process (ps -ax | grep python) and kill the process.

Step by Step:

  1. Get access to a terminal:

    • Option A: Press ctrl+z
    • Option B: If you have access to a the Ubuntu GUI, open a second terminal (ctrl+alt+t)
    • Option C: If you only have access to a command line, access a second tty (ctrl+alt+F2)
    • Option D: If you are accessing a server through ssh, make a new connection from another terminal ssh server (use option B or C, so you can open a second connection to execute the command)
  2. Look for the corresponding python PID of the process ps -ax | grep python. For example, the process id for my process (python my_stucked_process.py) would be 112923:

   3085 tty1     Sl+   15:53 /usr/bin/python /usr/bin/x-terminal-emulator
   112923 pts/2    Tl     0:01 python my_stucked_process.py
   113118 pts/2    S+     0:00 grep --color=auto python
  1. Kill the process kill -9 112923

@tutuDajuju suggested using ctrl+z but their suggestion will only send the process to the background (it will still exists consuming memory). You need to do the abovein order to really kill the process

Bestow answered 8/3, 2019 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.