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

From: Stefan Kangas <[email-masked]>
Date: Wed, 1 Jan 2025 22:57:38 -0600

I have run into a bug with make-thread on macOS 15.2, running on an M2.

I can reproduce the issue consistently both on emacs-30 and master by
evaluating this in emacs -Q:

    (make-thread (lambda () (sleep-for 1)) "bug")

This leads to Emacs freezing up completely within a fraction of a
second.  I have time to move point once or maybe twice before it gets
non-responsive, let's say within a few tenths of a second.

Running under lldb, I see the following lines get printed over and over:

2025-01-02 05:47:20.776364+0100 emacs[78593:1366649] [General]
nextEventMatchingMask should only be called from the Main Thread!
2025-01-02 05:47:20.777212+0100 emacs[78593:1366649] [General] (
	0   CoreFoundation                      0x000000018e2c6e80
__exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x000000018ddaecd8
objc_exception_throw + 88
	2   AppKit                              0x0000000192721c24
-[NSApplication(NSEventRouting)
_nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3072
	3   AppKit                              0x0000000191db0060
-[NSApplication run] + 480
	4   emacs                               0x0000000100403fec -[EmacsApp run] + 88
	5   emacs                               0x00000001004024b0 ns_select_1 + 1144
	6   emacs                               0x000000010040202c ns_select + 72
	7   emacs                               0x000000010036930c
wait_reading_process_output + 5264
	8   emacs                               0x000000010000b400 Fsleep_for + 372
	9   emacs                               0x00000001002bd5d8 eval_sub + 2164
	10  emacs                               0x00000001002bdd2c Fprogn + 100
	11  emacs                               0x00000001002c79dc
funcall_lambda + 1964
	12  emacs                               0x00000001002c6af4
funcall_general + 276
	13  emacs                               0x00000001002bedf8 Ffuncall + 464
	14  emacs                               0x00000001003bf944
invoke_thread_function + 48
	15  emacs                               0x00000001002c2a9c
internal_condition_case + 148
	16  emacs                               0x00000001003be36c run_thread + 340
	17  libsystem_pthread.dylib             0x000000018e16c2e4 _pthread_start + 136
	18  libsystem_pthread.dylib             0x000000018e1670fc thread_start + 8
)
2025-01-02 05:47:20.777270+0100 emacs[78593:1366649] [General]
nextEventMatchingMask should only be called from the Main Thread!
2025-01-02 05:47:20.778126+0100 emacs[78593:1366649] [General] (

When I kill the process in the lldb window with Ctrl+C, I can get the
following (this is on emacs-30):

[...]
2025-01-02 05:47:20.778199+0100 emacs[78593:1366649] [General]
nextEventMatchingMask should only be called from the Main Thread!
Process 78593 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x000000018e12dbbc libsystem_kernel.dylib`__psynch_mutexwait + 8
libsystem_kernel.dylib`:
->  0x18e12dbbc <+8>:  b.lo   0x18e12dbdc               ; <+40>
    0x18e12dbc0 <+12>: pacibsp
    0x18e12dbc4 <+16>: stp    x29, x30, [sp, #-0x10]!
    0x18e12dbc8 <+20>: mov    x29, sp
Target 0: (emacs) stopped.
(lldb) bt all
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
  * frame #0: 0x000000018e12dbbc libsystem_kernel.dylib`__psynch_mutexwait + 8
    frame #1: 0x000000018e1693f8
libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_wait + 84
    frame #2: 0x000000018e166dbc
libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_slow + 220
    frame #3: 0x00000001003bfb94
emacs`sys_mutex_lock(mutex=0x0000000100b7bd30) at systhread.c:140:15
    frame #4: 0x00000001003bcef8
emacs`acquire_global_lock(self=0x0000000100b074d0) at thread.c:160:3
    frame #5: 0x00000001003bdce0
emacs`really_call_select(arg=0x000000016fdfc5d8) at thread.c:636:7
    frame #6: 0x000000010026ee70
emacs`flush_stack_call_func1(func=(emacs`really_call_select at
thread.c:614), arg=0x000000016fdfc5d8) at alloc.c:5534:3
    frame #7: 0x00000001003bd35c
emacs`flush_stack_call_func(func=(emacs`really_call_select at
thread.c:614), arg=0x000000016fdfc5d8) at lisp.h:4509:3
    frame #8: 0x00000001003bdc14
emacs`thread_select(func=(libsystem_kernel.dylib`pselect$DARWIN_EXTSN),
max_fds=0, rfds=0x0000000000000000, wfds=0x0000000000000000,
efds=0x0000000000000000, timeout=0x000000016fdfc690,
sigmask=0x0000000000000000) at thread.c:656:3
    frame #9: 0x00000001004022e4 emacs`ns_select_1(nfds=0,
readfds=0x000000016fdfcf2c, writefds=0x000000016fdfceac,
exceptfds=0x0000000000000000, timeout=0x000000016fdfce80,
sigmask=0x0000000000000000, run_loop_only=NO) at nsterm.m:4883:7
    frame #10: 0x000000010040202c emacs`ns_select(nfds=0,
readfds=0x000000016fdfcf2c, writefds=0x000000016fdfceac,
exceptfds=0x0000000000000000, timeout=0x000000016fdfce80,
sigmask=0x0000000000000000) at nsterm.m:5006:10
    frame #11: 0x000000010036930c
emacs`wait_reading_process_output(time_limit=30, nsecs=0, read_kbd=-1,
do_display=true, wait_for_cell=(i = 0x0000000000000000),
wait_proc=0x0000000000000000, just_wait_proc=0) at process.c:5753:18
    frame #12: 0x000000010000b7b8 emacs`sit_for(timeout=(i =
0x000000000000007a), reading=true, display_option=1) at
dispnew.c:6335:7
    frame #13: 0x00000001001a5964 emacs`read_char(commandflag=1,
map=(i = 0x0000000138061a63), prev_event=(i = 0x0000000000000000),
used_mouse_menu=0x000000016fdfe367, end_time=0x0000000000000000) at
keyboard.c:2923:11
    frame #14: 0x00000001001a0ed0
emacs`read_key_sequence(keybuf=0x000000016fdfea38, prompt=(i =
0x0000000000000000), dont_downcase_last=false,
can_return_switch_frame=true, fix_current_buffer=true,
prevent_redisplay=false, disable_text_conversion_p=false) at
keyboard.c:10743:12
    frame #15: 0x000000010019ebac emacs`command_loop_1 at keyboard.c:1429:15
    frame #16: 0x00000001002c2a9c
emacs`internal_condition_case(bfun=(emacs`command_loop_1 at
keyboard.c:1324), handlers=(i = 0x0000000000000090),
hfun=(emacs`cmd_error at keyboard.c:970)) at eval.c:1613:25
    frame #17: 0x000000010019e560 emacs`command_loop_2(handlers=(i =
0x0000000000000090)) at keyboard.c:1168:11
    frame #18: 0x00000001002c1b64 emacs`internal_catch(tag=(i =
0x0000000000011220), func=(emacs`command_loop_2 at keyboard.c:1164),
arg=(i = 0x0000000000000090)) at eval.c:1292:25
    frame #19: 0x000000010019d4f8 emacs`command_loop at keyboard.c:1146:2
    frame #20: 0x000000010019d2a0 emacs`recursive_edit_1 at keyboard.c:754:9
    frame #21: 0x000000010019d884 emacs`Frecursive_edit at keyboard.c:837:3
    frame #22: 0x0000000100199c50 emacs`main(argc=2,
argv=0x000000016fdff4c8) at emacs.c:2635:3
    frame #23: 0x000000018ddec274 dyld`start + 2840
  thread #4
    frame #0: 0x000000018e1311a8 libsystem_kernel.dylib`__pselect + 8
    frame #1: 0x000000018e131080
libsystem_kernel.dylib`pselect$DARWIN_EXTSN + 64
    frame #2: 0x0000000100405c18 emacs`-[EmacsApp
fd_handler:](self=0x0000000156610fe0, _cmd="fd_handler:",
unused=0x0000000000000000) at nsterm.m:6440:20
    frame #3: 0x000000018f41cc24 Foundation`__NSThread__start__ + 724
    frame #4: 0x000000018e16c2e4 libsystem_pthread.dylib`_pthread_start + 136
  thread #5, name = 'com.apple.NSEventThread'
    frame #0: 0x000000018e12af54 libsystem_kernel.dylib`mach_msg2_trap + 8
    frame #1: 0x000000018e13d604 libsystem_kernel.dylib`mach_msg2_internal + 80
    frame #2: 0x000000018e133af8 libsystem_kernel.dylib`mach_msg_overwrite + 480
    frame #3: 0x000000018e12b29c libsystem_kernel.dylib`mach_msg + 24
    frame #4: 0x000000018e254a3c CoreFoundation`__CFRunLoopServiceMachPort + 160
    frame #5: 0x000000018e25329c CoreFoundation`__CFRunLoopRun + 1212
    frame #6: 0x000000018e252724 CoreFoundation`CFRunLoopRunSpecific + 588
    frame #7: 0x0000000191ee1a20 AppKit`_NSEventThread + 148
    frame #8: 0x000000018e16c2e4 libsystem_pthread.dylib`_pthread_start + 136
  thread #6
    frame #0: 0x000000018e1670e8 libsystem_pthread.dylib`start_wqthread
  thread #7, name = 'bug'
    frame #0: 0x000000018de3a2b0
dyld`dyld3::MachOLoaded::findClosestSymbol(unsigned long long, char
const**, unsigned long long*) const + 488
    frame #1: 0x000000018de1b13c dyld`dyld4::APIs::dladdr(void const*,
dl_info*) + 236
    frame #2: 0x000000018e012f00 libsystem_c.dylib`backtrace_symbols + 144
    frame #3: 0x000000018f4998c0 Foundation`-[_NSCallStackArray
descriptionWithLocale:indent:] + 144
    frame #4: 0x000000018f3e8c10 Foundation`_NS_os_log_callback + 276
    frame #5: 0x000000018debee60
libsystem_trace.dylib`_os_log_fmt_flatten_NSCF + 64
    frame #6: 0x000000018dec5830
libsystem_trace.dylib`_os_log_fmt_flatten_object_impl + 372
    frame #7: 0x000000018debc9c8
libsystem_trace.dylib`_os_log_impl_flatten_and_send + 2144
    frame #8: 0x000000018debc150 libsystem_trace.dylib`_os_log + 168
    frame #9: 0x000000018debc0a0 libsystem_trace.dylib`_os_log_impl + 28
    frame #10: 0x000000019209151c AppKit`-[NSApplication reportException:] + 624
    frame #11: 0x0000000191db0118 AppKit`-[NSApplication run] + 664
    frame #12: 0x0000000100403fec emacs`-[EmacsApp
run](self=0x0000000156610fe0, _cmd="run") at nsterm.m:5938:7
    frame #13: 0x00000001004024b0 emacs`ns_select_1(nfds=0,
readfds=0x00000001708c26bc, writefds=0x00000001708c263c,
exceptfds=0x0000000000000000, timeout=0x00000001708c2610,
sigmask=0x0000000000000000, run_loop_only=NO) at nsterm.m:4954:3
    frame #14: 0x000000010040202c emacs`ns_select(nfds=0,
readfds=0x00000001708c26bc, writefds=0x00000001708c263c,
exceptfds=0x0000000000000000, timeout=0x00000001708c2610,
sigmask=0x0000000000000000) at nsterm.m:5006:10
    frame #15: 0x000000010036930c
emacs`wait_reading_process_output(time_limit=1, nsecs=0, read_kbd=0,
do_display=false, wait_for_cell=(i = 0x0000000000000000),
wait_proc=0x0000000000000000, just_wait_proc=0) at process.c:5753:18
    frame #16: 0x000000010000b400 emacs`Fsleep_for(seconds=(i =
0x0000000000000006), milliseconds=(i = 0x0000000000000000)) at
dispnew.c:6248:2
    frame #17: 0x00000001002bd5d8 emacs`eval_sub(form=(i =
0x0000000138061f43)) at eval.c:2601:15
    frame #18: 0x00000001002bdd2c emacs`Fprogn(body=(i =
0x0000000000000000)) at eval.c:439:13
    frame #19: 0x00000001002c79dc emacs`funcall_lambda(fun=(i =
0x000000015809908d), nargs=0, arg_vector=0x000000015693e868) at
eval.c:3350:15
    frame #20: 0x00000001002c6af4 emacs`funcall_general(fun=(i =
0x000000015809908d), numargs=0, args=0x000000015693e868) at
eval.c:3044:12
    frame #21: 0x00000001002bedf8 emacs`Ffuncall(nargs=1,
args=0x000000015693e860) at eval.c:3093:21
    frame #22: 0x00000001003bf944 emacs`invoke_thread_function at
thread.c:744:28
    frame #23: 0x00000001002c2a9c
emacs`internal_condition_case(bfun=(emacs`invoke_thread_function at
thread.c:741), handlers=(i = 0x0000000000000030),
hfun=(emacs`record_thread_error at thread.c:752)) at eval.c:1613:25
    frame #24: 0x00000001003be36c
emacs`run_thread(state=0x000000015693e840) at thread.c:815:3
    frame #25: 0x000000018e16c2e4 libsystem_pthread.dylib`_pthread_start + 136
  thread #8, name = 'HIE: __ 6f45e584fd961eaa 2025-01-02 05:47:10.265'
    frame #0: 0x000000018e12af54 libsystem_kernel.dylib`mach_msg2_trap + 8
    frame #1: 0x000000018e13d604 libsystem_kernel.dylib`mach_msg2_internal + 80
    frame #2: 0x000000018e15a458 libsystem_kernel.dylib`thread_suspend + 108
    frame #3: 0x0000000194bce174
HIServices`SOME_OTHER_THREAD_SWALLOWED_AT_LEAST_ONE_EXCEPTION + 20
    frame #4: 0x000000018f41cc24 Foundation`__NSThread__start__ + 724
    frame #5: 0x000000018e16c2e4 libsystem_pthread.dylib`_pthread_start + 136
  thread #9
    frame #0: 0x000000018e12cba4 libsystem_kernel.dylib`__workq_kernreturn + 8
(lldb)

In GNU Emacs 30.0.93 (build 1, aarch64-apple-darwin24.2.0, NS
 appkit-2575.30 Version 15.2 (Build 24C101)) of 2025-01-02 built on
 foo.local
Repository revision: 43a8f0de5ecbe8430917968af703f4a630567461
Repository branch: emacs-30
Windowing system distributor 'Apple', version 10.3.2575
System Description:  macOS 15.2

Configured using:
 'configure --without-dbus --with-native-compilation
 --enable-checking=yes --enable-check-lisp-object-type
 'CFLAGS=-I/opt/homebrew/opt/libmps/include -O0 -g3'
 LDFLAGS=-L/opt/homebrew/opt/libmps/lib'

Configured features:
ACL GNUTLS LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE NS PDUMPER
PNG SQLITE3 THREADS TOOLKIT_SCROLL_BARS TREE_SITTER WEBP ZLIB

Important settings:
  value of $LC_CTYPE: UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr compile comint ansi-osc ansi-color ring comp-run
bytecomp byte-compile comp-common rx emacsbug message mailcap yank-media
puny dired dired-loaddefs rfc822 mml mml-sec password-cache epa derived
epg rfc6068 epg-config gnus-util text-property-search time-date subr-x
mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils
mailheader cl-loaddefs cl-lib sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils rmc iso-transl tooltip cconv eldoc paren
electric uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/ns-win ns-win ucs-normalize mule-util term/common-win tool-bar dnd
fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow
isearch easymenu timer select scroll-bar mouse jit-lock font-lock syntax
font-core term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads kqueue cocoa ns lcms2
multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 55821 9594) (symbols 48 6633 0) (strings 32 15516 4510)
 (string-bytes 1 404656) (vectors 16 11284)
 (vector-slots 8 125438 8014) (floats 8 21 14) (intervals 56 243 1)
 (buffers 992 11))