Why is Emacs Hanging?

Even after using Emacs for 15 years there’s still so much I can learn. I probably should have already known this, but there’s a first time for everything.

It’s rare that Emacs hangs. Exceedingly rare. Which is probably why I didn’t know how to deal with it. Today Emacs started hanging when trying to open files over a remote TRAMP session (SSH).

The most important key of all that everyone who uses Emacs knows is C-g. This is the universal “quit” key and it has the power to interrupt any long running processes. What I didn’t know about is M-x toggle-debug-on-quit. I’ve used toggle-debug-on-error extensively when programming Elisp (I even have it bound to a key in Elisp buffers). toggle-debug-on-quit is similar except the debugger is invoked when you C-g.

While this is enabled, I was able to reproduce the hang, then press C-g. I could see that what was happening is ess-r-package-auto-activate was being called via after-change-major-mode-hook, this was in turn calling on TRAMP again to try to find an R package or something. I don’t regularly use ESS mode, so I simply disabled this behaviour with (setq ess-r-package-auto-activate nil).

toggle-debug-on-quit should be toggled off again aftewards, as quitting isn’t actually an error most of the time. Doom modeline handily displays an icon when it’s enabled, confirming that I’m the last person to know about this.

Something else interesting to consider here is packages can still affect Emacs performance even if you aren’t using them. I haven’t used R or ESS mode for years, but I’ve left them in my config because, why not? But these “dormant” packages can still be impacting performance and it might be worth auditing hooks like after-change-major-mode-hook to check for packages you don’t really need any more.