How to interrupt emacs when control-g does not work in very large files?

I doubt it's font lock mode. That probably only does sections near what's visible. It's probably copying data in memory.

sigusr1

sigusr2

These events are generated when the Emacs process receives the signals SIGUSR1 and SIGUSR2. They contain no additional data because signals do not carry additional information. They can be useful for debugging (see Error Debugging).

Source: GNU Emacs Lisp Reference Manual: Misc Events

I don't know what it would do (or what version of emacs you are running) but you could try a PROBABLY non-destructive signal (USR1 or USR2).

$ kill -USR1 pid

Or you could go to sleep and see if it recovers.


I'm unaware of any signal handling that emacs does but you could use the Emacs Server by putting server-start in your ~/.emacs file so you can do RPC on your running instance of emacs.

Then when you want to kill emacs and write you can call from your shell emacsclient -e '(save-buffers-kill-emacs t)'.


Here was my answer to a similar question which may help: https://stackoverflow.com/a/47070702/3818556


So far the only way I found ever working is to send a SIGUSR2 signal to the busy emacs process using command line kill -- no matter if it's running as a server (with --daemon) or not. This will not force terminating the emacs process while being able to interrupt what it's doing. Let's try an example to break a sleep-for loop running on emacs server. (Yes, sleep-for can be break by C-g but this is just an example. It worked on some cases when emacs does not respond to keyboard commands.)

First, start emacs server then use emacsclient connect to it, enter M-: (sleep-for 120). Now go to another terminal and find the process ID of the server using command line: ps x|grep 'emacs.*--daemon'. Assuming the PID we found here is 12345. Now use the terminal to break it:

kill -USR2 12345

We should now see the sleep-for loop interrupted. On some cases I need to send this signal multiple times.

Also notice that SIGUSR1 does not work so SIGUSR2 is recommended, and this might only work on Emacs versions greater than v24. Hope this helps!