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?

Clément
  • 3,924
  • 1
  • 22
  • 37
  • 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). – Harald Hanche-Olsen Apr 16 '16 at 12:15
  • @HaraldHanche-Olsen kill -9 generally works :) – Clément Apr 16 '16 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 '16 at 22:11

2 Answers2

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.

Archenoth
  • 2,106
  • 16
  • 17
Tyler
  • 21,719
  • 1
  • 52
  • 92
  • 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 '16 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 '16 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 '16 at 23:37
  • Beautiful! It worked like a charm for me. – Felipe Sep 19 '19 at 07:09
  • 5
    `pkill -SIGUSR2 -i emacs ; emacsclient -e '(setq debug-on-quit nil)'` – HappyFace May 04 '20 at 01:47
  • In my case (OSX) I had to do "pkill -SIGUSR2 Aquamacs" followed by "pkill Aquamacs" to get the desired behavior. – Digicrat Dec 14 '20 at 20:41
  • Could I do the same approach for emacsclient like: `pkill -SIGUSR2 emacsclient`? – alper Sep 29 '22 at 11:33
5

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

Adobe
  • 1,859
  • 13
  • 27
  • 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 '16 at 02:06