Subject: bug#31671: 26.1; edebug-defun doesn't step if functions run in a separate thread

From: Eli Zaretskii <[email-masked]>
Date: Tue, 05 Jun 2018 17:51:03 +0300

> Date: Mon, 04 Jun 2018 19:56:28 +0300
> From: Eli Zaretskii <[email-masked]>
> Cc: [email-masked]
> 
> The problem here is that Edebug enters recursive-editing (on the
> non-main thread which runs the function 'foo'), then waits for the
> user to press a key.  While it waits, it releases the global lock, and
> the main thread starts running.  So when you press a key, you are on a
> different thread, and 'throw' doesn't have a matching 'catch' (because
> that 'catch' is stored with the handlers of the thread which runs
> 'foo'.
> 
> Not yet sure how to deal with this.  Thoughts and ideas are welcome.

Any objections to the following band-aid?  (Of course, user-error does
nothing visible on non-main threads, and you have too invoke
thread-last-error to see the error message, but at least we don't
leave around a thread in limbo waiting forever for input that will
never come...)

--- src/thread.c~0	2018-01-28 06:56:25.000000000 +0200
+++ src/thread.c	2018-06-05 17:25:43.147566500 +0300
@@ -980,6 +980,23 @@ DEFUN ("thread-last-error", Fthread_last
   return last_thread_error;
 }
 
+DEFUN ("main-thread-p", Fmain_thread_p, Smain_thread_p, 0, 1, 0,
+       doc: /* Return non-nil if THREAD is the main thread.
+If THREAD is nil or omitted, it defaults to the current thread.  */)
+  (Lisp_Object thread)
+{
+  struct thread_state *tstate;
+
+  if (NILP (thread))
+    tstate = current_thread;
+  else
+    {
+      CHECK_THREAD (thread);
+      tstate = XTHREAD (thread);
+    }
+  return main_thread_p (tstate) ? Qt : Qnil;
+}
+
 
 
 bool
@@ -1073,6 +1090,7 @@ syms_of_threads (void)
       defsubr (&Scondition_mutex);
       defsubr (&Scondition_name);
       defsubr (&Sthread_last_error);
+      defsubr (&Smain_thread_p);
 
       staticpro (&last_thread_error);
       last_thread_error = Qnil;
--- lisp/emacs-lisp/edebug.el~0	2018-03-14 06:39:59.000000000 +0200
+++ lisp/emacs-lisp/edebug.el	2018-06-05 17:19:52.017355000 +0300
@@ -2425,6 +2425,8 @@
   (if inhibit-redisplay
       ;; Don't really try to enter edebug within an eval from redisplay.
       value
+    (or (main-thread-p)
+        (user-error "Debugging on non-main thread is not yet supported"))
     ;; Check breakpoints and pending input.
     ;; If edebug display should be updated, call edebug--display.
     ;; Return value.
--- lisp/emacs-lisp/debug.el~0	2018-01-03 13:08:56.000000000 +0200
+++ lisp/emacs-lisp/debug.el	2018-06-05 17:31:39.765134800 +0300
@@ -147,6 +147,8 @@
   (if inhibit-redisplay
       ;; Don't really try to enter debugger within an eval from redisplay.
       debugger-value
+    (or (main-thread-p)
+        (user-error "Debugging on non-main thread is not yet supported"))
     (unless noninteractive
       (message "Entering debugger..."))
     (let (debugger-value
--- doc/lispref/threads.texi~0	2018-01-03 13:08:46.000000000 +0200
+++ doc/lispref/threads.texi	2018-06-05 17:29:47.030735200 +0300
@@ -122,6 +122,12 @@
 Return the current thread.
 @end defun
 
+@defun main-thread-p &optional thread
+This function returns non-@code{nil} if @var{thread} is the main
+thread.  If @var{thread} is @code{nil} or omitted, it defaults to the
+current thread.
+@end defun
+
 @defun all-threads
 Return a list of all the live thread objects.  A new list is returned
 by each invocation.
--- etc/NEWS~	2018-03-14 06:39:58.000000000 +0200
+++ etc/NEWS	2018-06-05 17:37:13.301233500 +0300
@@ -361,6 +361,11 @@
 * Lisp Changes in Emacs 27.1
 
 +++
+** New primitive 'main-thread-p'.
+This can be used in code which needs to work differently when it runs
+in threads other than the main thread.
+
++++
 ** New function assoc-delete-all.
 
 ** 'print-quoted' now defaults to t, so if you want to see