64

What should I do when Emacs stops responding?

My current "emacs-is-frozen" protocol looks like this:

  • Spam C-g (it sometimes works)
  • Wait for a while.
  • pkill -9 emacs

What alternate steps could I try to avoid losing work that hasn't been autosaved yet?

If anything, could I attach GDB to know what the likely issue is?

3
  • 1
    Could it be that emacs hangs in an uninterruptible system call? I think not, for then even kill -9 might not help … but that could depend on the OS, so I am throwing the suggestion out there anyhow. In my experience, NFS problems are the most likely culprit. Try to access a file on an NFS filesystem where the server is down or inaccessible, and you're stuck. You should be able to see that in the flags column of a ps listing (consult the man page). Apr 16, 2016 at 12:15
  • @HaraldHanche-Olsen kill -9 generally works :)
    – Clément
    Apr 16, 2016 at 17:54
  • 1
    Attaching GDB certainly is an option. If you do that from the source directory (specifically, the src/ subdirectory) and allow gdb to source the .gdbinit there, you'll get bonus commands (xbacktrace, etc.) for inspecting emacs internal structures (but the usefulness decreases dramatically if you have an optimized build, e.g. a release). See the etc/DEBUG file in the emacs tree for more information on debugging emacs.
    – YoungFrog
    Apr 17, 2016 at 22:11

2 Answers 2

79

When C-g doesn't work, you can sometimes get control back with:

pkill -SIGUSR2 emacs

As @Archenoth points out, sending the SIGUSR2 signal to Emacs turns on debug-on-quit. This can be useful, but you'll want to turn it off again at some point (possibly immediately). To do this, call M-x toggle-debug-on-quit.

More information on using SIGUSR1 and SIGUSR2 are found in the elisp manual, particularly (elisp) Misc Events and (elisp) Event Examples, with related discussion of debugging in (elisp) Error Debugging.

7
  • 2
    I don't, personally, remember a time when this didn't work. The signal handlers in Emacs exist at a very low level.
    – PythonNut
    Apr 15, 2016 at 18:23
  • 4
    @PythonNut I probably have an Emacs freeze (neither C-g or SIGUSR1 work) about once a month. It's not supposed to happen I know, I assume there's some horrible interaction between multiple packages and my OS. I can't reproduce it reliably enough to try debugging :/
    – Tyler
    Apr 15, 2016 at 18:56
  • 1
    I've tried this extensively over the last few months. Works nicely in most cases, though in some it still fails.
    – Clément
    Jul 12, 2016 at 23:37
  • Beautiful! It worked like a charm for me.
    – Felipe
    Sep 19, 2019 at 7:09
  • 5
    pkill -SIGUSR2 -i emacs ; emacsclient -e '(setq debug-on-quit nil)'
    – HappyFace
    May 4, 2020 at 1:47
5

It also sometimes helps to abort-recursive-edit with C-].

1
  • 2
    AFAIK this usually won't help during a freeze because C-] is not treated specially (so there is no reason that it manages to get out of a freeze and any other command doesn't).
    – YoungFrog
    Apr 17, 2016 at 2:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.