Wednesday, January 30, 2019

Incorrect handling of CTRL-C in pdb

I usually start a python program I want to debug by running:
    python3 -m pdb -c continue MY_PROGRAM
I can use CTRL-C to break the program, adding break points, and I can CTRL-C multiple times.
However, recently, for some unknown reason, pdb is no longer able to continue to run the program after the second CTRL-C. Looking into pdb.py, I found the following piece of code for handling CTRL-C.

    def sigint_handler(self, signum, frame):
        if self.allow_kbdint:
            raise KeyboardInterrupt
        self.message("\nProgram interrupted. (Use 'cont' to resume).")
        self.set_step()
        self.set_trace(frame)
        # restore previous signal handler
        signal.signal(signal.SIGINT, self._previous_sigint_handler)

Inspecting the value of self._previous_sigint_handler, it points to the default sigint handler, which will break the program. It turns out that the last two lines were removed in python3.5.3 in this commit.

So you can solve this problem by upgrading to a newer version of python3. Or if it's not easy to upgrade (like me, using Ubuntu 16.04, python 3.6.3 or newer is not available), commenting out the last line seems to work.