Subject: bug#75275: 30.0.92; `make-thread` bug on macOS 15.2

From: Alan Third <[email-masked]>
Date: Thu, 2 Jan 2025 16:06:25 +0000

On Thu, Jan 02, 2025 at 09:19:36AM -0600, Stefan Kangas wrote:
> That patch fixes it for me, indeed.  Thanks!

Hi Stefan, any chance you could check this version?

I have a suspicion that Gerd's will break C-g again. I'm not sure this
will be suitable for Emacs 30 though as it's a slightly larger change.

The C-g thing is hard to test as I think it relied on Gerd's machine
randomly hanging and him hammering C-g and it doing nothing. I'm not
sure how to replicate that original hang.
-- 
Alan Third
>From 528dcb934485a5a1331b4cf851216a3750df3210 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Thu, 2 Jan 2025 15:56:20 +0000
Subject: [PATCH] Fix C-g handling on NS (bug#75275)

* src/nsterm.m (ns_select_1): Remove function.
(ns_select): Reinstate by copying the contents of ns_select_1 back
in. Add check for C-g at top. Stop fall-through to NS run loop when not
required.
([EmacsView showFontPanel]): Just call [NSApp run] directly, calling
ns_select for this makes ns_select more complex.
---
 src/nsterm.m | 44 ++++++++++++++++++--------------------------
 1 file changed, 18 insertions(+), 26 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index a4398e79211..652eee1f988 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -4826,9 +4826,9 @@ Function modeled after x_draw_glyph_string_box ().

 static int
-ns_select_1 (int nfds, fd_set *readfds, fd_set *writefds,
-	     fd_set *exceptfds, struct timespec *timeout,
-	     sigset_t *sigmask, BOOL run_loop_only)
+ns_select (int nfds, fd_set *readfds, fd_set *writefds,
+           fd_set *exceptfds, struct timespec *timeout,
+           sigset_t *sigmask)
 /* --------------------------------------------------------------------------
      Replacement for select, checking for events
    -------------------------------------------------------------------------- */
@@ -4837,6 +4837,7 @@ Function modeled after x_draw_glyph_string_box ().
   int t, k, nr = 0;
   struct input_event event;
   char c;
+  NSEvent *ev;
 
   NSTRACE_WHEN (NSTRACE_GROUP_EVENTS, "ns_select");
 
@@ -4844,15 +4845,16 @@ Function modeled after x_draw_glyph_string_box ().
   check_native_fs ();
 #endif
 
-  /* If there are input events pending, store them so that Emacs can
-     recognize C-g.  (And we must make sure [NSApp run] is called in
-     this function, so that C-g has a chance to land in
-     hold_event_q.)  */
-  if (hold_event_q.nr > 0)
+  /* Check if there are any C-g events on the queue.  */
+  while ([NSThread isMainThread]
+         && (ev = [NSApp nextEventMatchingMask:NSEventMaskKeyDown
+                                    untilDate:nil
+                                       inMode:NSDefaultRunLoopMode
+                                       dequeue:NO]))
     {
-      for (int i = 0; i < hold_event_q.nr; ++i)
-        kbd_buffer_store_event_hold (&hold_event_q.q[i], NULL);
-      hold_event_q.nr = 0;
+      if ([ev modifierFlags] == NSEventModifierFlagControl
+          && [[ev charactersIgnoringModifiers] isEqualToString:@"g"])
+        Vquit_flag = Qt;
     }
 
   eassert (nfds <= FD_SETSIZE);
@@ -4863,13 +4865,10 @@ Function modeled after x_draw_glyph_string_box ().
     }
 
   /* emacs -nw doesn't have an NSApp, so we're done.  */
-  if (NSApp == nil)
-    return thread_select (pselect, nfds, readfds, writefds, exceptfds,
-			  timeout, sigmask);
-
-  if (![NSThread isMainThread]
+  if (NSApp == nil
+      || ![NSThread isMainThread]
       || (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0))
-    thread_select (pselect, nfds, readfds, writefds,
+    return thread_select (pselect, nfds, readfds, writefds,
 		   exceptfds, timeout, sigmask);
   else
     {
@@ -4992,14 +4991,6 @@ Function modeled after x_draw_glyph_string_box ().
   return result;
 }
 
-int
-ns_select (int nfds, fd_set *readfds, fd_set *writefds,
-	   fd_set *exceptfds, struct timespec *timeout,
-	   sigset_t *sigmask)
-{
-  return ns_select_1 (nfds, readfds, writefds, exceptfds,
-		      timeout, sigmask, NO);
-}
 
 #ifdef HAVE_PTHREAD
 void
@@ -6790,7 +6781,8 @@ - (Lisp_Object) showFontPanel
 	 && [[fm fontPanel: YES] isVisible]
 #endif
 	 )
-    ns_select_1 (0, NULL, NULL, NULL, &timeout, NULL, YES);
+
+  [NSApp run];
   unblock_input ();
 
   if (font_panel_result)
-- 
2.41.0