31

This question already has an answer here:

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?

marked as duplicate by Gilles Feb 22 '17 at 13:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
34

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.

  • 1
    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
  • 1
    @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
3

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

  • 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 2:06

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