unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Unconditional quit on SIGUSR2
@ 2011-03-28  7:14 Daniel Colascione
  2011-03-28 11:48 ` Eli Zaretskii
  0 siblings, 1 reply; 36+ messages in thread
From: Daniel Colascione @ 2011-03-28  7:14 UTC (permalink / raw)
  To: Emacs development discussions


[-- Attachment #1.1: Type: text/plain, Size: 1550 bytes --]

Sometimes Emacs gets stuck in places where quitting is suppressed,
such as during redisplay.  For most users, the only way to deal with
this situation is to abruptly terminate Emacs.

This patch allows users to recover from this situation by sending Emacs
a SIGUSR2 signal.  When we receive it, we set debug-on-quit to t,
inhibit-quit to nil, and quit-flag to t.  These operations will force
Emacs to stop at the next possible opportunity and display a backtrace.
 From here, users can save their work, and developers can figure out
what caused the hang.

This functionality cannot be achieved by simply modifying
special-event-map: event handled by this keymap are delivered too late
to cancel some loops.  For example, even after (define-key
special-event-map [sigusr2] #'debug), SIGUSR2 will not break out of a
(while t) loop.

Instead, we introduce a new sigusr2-action variable that controls the
operation of the signal handler at a lower level.  If this variable has
value 'quit, then when we receive SIGUSR2, we perform the operations we
described above and eat the signal.  Otherwise, we generate an event and
send it through special-event-map as before.  In order to be maximally
useful to users encountering unexpected infinite loops, we set
sigusr2-action to 'quit by default.

SIGUSR2 is chosen under the assumption that users would use SIGUSR1
instead of SIGUSR2 to implement any custom functionality based on
receiving signals through special-event-map, reducing the likelihood of
breaking existing code.


[-- Attachment #1.2: debugsig.patch --]
[-- Type: text/plain, Size: 4084 bytes --]

=== modified file 'doc/emacs/trouble.texi'
--- doc/emacs/trouble.texi	2011-01-25 04:08:28 +0000
+++ doc/emacs/trouble.texi	2011-03-28 06:53:07 +0000
@@ -812,6 +812,15 @@
 This backtrace is useful for debugging such long loops, so if you can
 produce it, copy it into the bug report.
 
+@vindex sigusr2-action
+If normal quitting does nothing and you are using a system that
+supports signals, then as a last resort, you can try sending Emacs the
+SIGUSR2 signal.  When Emacs receives this signal, it checks whether
+@code{sigusr2-action} is equal to @code{quit}.  If so, Emacs stops
+what it's doing, sets @code{debug-on-quit} to @code{t}, and quits just
+as if you had typed @kbd{C-g}.  This process happens even in places
+where quitting is normally not allowed.
+
 @item
 Check whether any programs you have loaded into the Lisp world,
 including your @file{.emacs} file, set any variables that may affect the

=== modified file 'doc/lispref/debugging.texi'
--- doc/lispref/debugging.texi	2011-01-25 04:08:28 +0000
+++ doc/lispref/debugging.texi	2011-03-28 06:54:07 +0000
@@ -185,6 +185,19 @@
 when you quit.  @xref{Quitting}.
 @end defopt
 
+@defopt sigusr2-action
+On systems with signal support, this variable determines what happens
+when Emacs receives SIGUSR2.  When its value is @code{quit}, Emacs
+will quit just as if @kbd{C-g} had been pressed, except that Emacs
+will first set @code{debug-on-quit} to @code{t} and
+@code{inhibit-quit} to @code{nil} so that the quit is not ignored.
+When @code{sigusr2-action} has any other value, Emacs will handle the
+signal as specified in @code{special-event-map} (@pxref{Active
+Keymaps}).  This feature is useful for terminating infinite loops in
+places where quits would normally be ignored, such as in code that
+runs during redisplay.
+@end defopt
+
 @node Function Debugging
 @subsection Entering the Debugger on a Function Call
 @cindex function call debugging

=== modified file 'lisp/cus-start.el'
--- lisp/cus-start.el	2011-03-27 10:55:07 +0000
+++ lisp/cus-start.el	2011-03-28 06:04:18 +0000
@@ -259,6 +259,10 @@
 	     (suggest-key-bindings keyboard (choice (const :tag "off" nil)
 						    (integer :tag "time" 2)
 						    (other :tag "on")))
+             (sigusr2-action debug
+			    (choice (const :tag "Handle SIGUSR2 normally")
+                                    (const :tag "Quit on SIGUSR2" quit))
+                            "24.1")
 
 ;; This is not good news because it will use the wrong
 ;; version-specific directories when you upgrade.  We need

=== modified file 'src/keyboard.c'
--- src/keyboard.c	2011-03-27 02:27:11 +0000
+++ src/keyboard.c	2011-03-28 06:46:50 +0000
@@ -7168,6 +7168,21 @@
 
   SIGNAL_THREAD_CHECK (sig);
 
+#ifdef SIGUSR2
+  if (sig == SIGUSR2)
+    {
+      /* We can be called at any time, but because variable assignment
+         is atomic, any races are harmless.  */
+      if (Vsigusr2_action == Qquit)
+        {
+          debug_on_quit = 1;
+          Vquit_flag = Qt;
+          Vinhibit_quit = Qnil;
+          return;
+        }
+    }
+#endif /* SIGUSR2 */
+
   for (p = user_signals; p; p = p->next)
     if (p->sig == sig)
       {
@@ -11356,6 +11371,10 @@
   poll_suppress_count = 1;
   start_polling ();
 #endif
+
+#ifdef SIGUSR2
+  Vsigusr2_action = Qquit;
+#endif
 }
 
 /* This type's only use is in syms_of_keyboard, to initialize the
@@ -12178,6 +12197,19 @@
 `deactivate-mark' call uses this to set the window selection.  */);
   Vsaved_region_selection = Qnil;
 
+#ifdef SIGUSR2
+  DEFVAR_LISP ("sigusr2-action",
+               Vsigusr2_action,
+               doc: /* Controls what Emacs does when it receives a
+SIGUSR2.  If 'quit, set `inhibit-quit' to nil and `quit-flag' to t,
+quitting unconditionally at the next opportunity. Any other value
+causes nothing to be done.
+
+In all cases, Emacs generates a `sigusr2' event as it normally
+would. See `special-event-map'.  */);
+  Vsigusr2_action = Qnil;
+#endif
+
   /* Create the initial keyboard. */
   initial_kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
   init_kboard (initial_kboard);


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2011-04-26 14:47 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-28  7:14 [PATCH] Unconditional quit on SIGUSR2 Daniel Colascione
2011-03-28 11:48 ` Eli Zaretskii
2011-03-28 12:32   ` Julien Danjou
2011-03-28 14:32     ` Eli Zaretskii
2011-03-28 14:49       ` Julien Danjou
2011-03-28 13:38   ` Daniel Colascione
2011-03-28 14:47     ` Eli Zaretskii
2011-03-28 17:23       ` Daniel Colascione
2011-03-28 18:37         ` Eli Zaretskii
2011-03-28 19:29           ` Daniel Colascione
2011-03-28 19:41             ` Eli Zaretskii
2011-03-28 19:49               ` Daniel Colascione
2011-03-28 19:52                 ` Lennart Borgman
2011-03-28 19:56                   ` Daniel Colascione
2011-03-28 20:06                     ` Lennart Borgman
2011-03-28 20:12                       ` Daniel Colascione
2011-03-28 20:45                         ` Lennart Borgman
2011-03-28 21:13                           ` In praise of font-lock (Was: Re: [PATCH] Unconditional quit on SIGUSR2) Daniel Colascione
2011-03-28 21:27                             ` Lennart Borgman
2011-03-28 20:10                     ` [PATCH] Unconditional quit on SIGUSR2 chad
2011-03-28 22:00                 ` Eli Zaretskii
2011-03-28 22:08                   ` Daniel Colascione
2011-03-28 22:20                     ` chad
2011-03-29 17:55                       ` Daniel Colascione
2011-03-29 18:14                         ` Daniel Colascione
2011-04-25  2:16                         ` Daniel Colascione
2011-04-25  9:23                           ` joakim
2011-04-25  9:33                             ` Daniel Colascione
2011-04-25 13:32                           ` Stefan Monnier
2011-04-25 18:21                             ` Daniel Colascione
2011-04-25 18:43                               ` Stefan Monnier
2011-04-26 14:47                             ` Richard Stallman
2011-03-29 13:23         ` Tom Tromey
2011-03-29 14:25           ` Stefan Monnier
2011-03-29 14:47             ` Tom Tromey
2011-03-29 21:37               ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).