question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyboardInterrupt Signal does not raise KeyboardInterrupt Error

See original GitHub issue

🐛 Bug

If a KeyboardInterrupt signal is send during training via e.g. ctrl-c the trainer.fit() method finishes immediately but no KeyboardInterrupt error is raised. Hence, the remaining model training script (everything after the trainer.fit() call) continues as if nothing has happened. Since my training script saves a status file “successfull” to disc if no error occured, raising no KeyboardInterrupt error during training is quite problematic.

To Reproduce

The problem comes from the design of the _call_and_handle_interrupt() method in the lightning trainer class. This method wraps the entire training and has the following structure:

import time


def main():
    # _call_and_handle_interrupt method which wraps entire training has this structure
    #  However, the BaseException is never reached if a KeyboardInterrupt is caught and thus no error is raised
    try:
        # training here
        for i in range(100):
            time.sleep(1)
    except KeyboardInterrupt as e:
        print('handle KeyBoardInterrupt.')
        # why not raining here as well?
    except BaseException as e:
        # never reached when a KeyboardInterrupt is caught
        print('handle BaseException.')
        raise


if __name__ == '__main__':
    main()

Since the KeyboardInterrupt exception handling has no raise statement no error is raised and the trainer.fit() method exits with no error. It is said that the KeyboardInterrupt exception block will be removed in lightning version 1.7 but I think the current behavior is not expected. At least one should raise the caught KeyboardInterrupt after handling it.

Expected behavior

If I send a KeyboardInterrupt signal while training, I expect the trainer.fit() method to raise that error immediately so that my script finishes without executing further code which relys on the fact that no error occured.

cc @borda @tchaton @rohitgr7 @akihironitta @justusschock @awaelchli @ninginthecloud

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
daniellepintzcommented, Feb 24, 2022

When I worked on https://github.com/PyTorchLightning/pytorch-lightning/issues/8992 my intention was to remove the special handling for KeyboardInterrupt in v1.7 and treat it as a regular exception, as I wrote in the TODO. I am still fine with doing that. But if others prefer to do something else that is fine as well.

1reaction
awaelchlicommented, Feb 24, 2022

No objections for removing on_keyboard_interrupt 😃 I’m proposing to collect opinions on whether we should exit the program when a keyboard interrupt occurs (after handling it with the new on_exception hook)

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyboardInterrupt Signal does not raise KeyboardInterrupt Error
Since the KeyboardInterrupt exception handling has no raise statement no error is raised and the trainer.fit() method exits with no error. It is ......
Read more >
Python Keyboardinterrupt - Linux Hint
The KeyboardInterrupt exception is a standard exception that is thrown to manage faults with the keyboard. In Python, there is no special syntax...
Read more >
CTLR + C interrupt signal in python causes raise ...
Issue was with the IDE ,Thonny and IDLE When i tried running the code ... print ('KeyboardInterrupt exception is caught') else: print ('No...
Read more >
Complete Guide to Python KeyboardInterrupt - eduCBA
Guide to Python KeyboardInterrupt. Here we discuss how KeyboardInterrupt exception work, how to avoid KeyboardInterrupt exceptions in Python.
Read more >
Understand KeyboardInterrupt in Python Before You Regret
What is Keyboard Interrupt Exception? ... KeyboardInterrupt exception is a part of Python's built-in exceptions. When the programmer presses the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found