Subject: 31.1.90; Threads + receiving a signal causes a segfault

From: Spencer Baugh <[email-masked]>
Date: Tue, 26 Aug 2025 12:19:07 -0400

repro.el:

(make-thread
 (lambda ()
   (sleep-for .1)
   (signal-process (emacs-pid) 'sigint)))
(sleep-for 100)

emacs -Q -l ./repro.el

Observe Emacs crashes with a segfault while shutting down.

In gdb, the segfault appears to be because current_thread is NULL.

This happens both in 30.2 and on trunk.

bt full:
#0  0x000000000041f32c in Fkill_emacs (arg=arg@entry=make_fixnum(2), restart=restart@entry=XIL(0)) at emacs.c:3002
        exit_code = <optimized out>
#1  0x000000000041f482 in terminate_due_to_signal (sig=sig@entry=2, backtrace_limit=backtrace_limit@entry=40) at lisp.h:1226
#2  0x000000000041f8f6 in handle_fatal_signal (sig=sig@entry=2) at sysdep.c:1800
#3  0x0000000000557804 in deliver_process_signal (sig=2, handler=0x41f8eb <handle_fatal_signal>) at sysdep.c:1758
        old_errno = 4
        on_main_thread = true
#4  0x00007fffefc12970 in <signal handler called> () at /lib64/libpthread.so.0
#5  0x00007fffec134cd9 in pselect () at /lib64/libc.so.6
#6  0x000000000063b1b0 in really_call_select (arg=0x7fffffffbb80) at thread.c:624
        sa = 0x7fffffffbb80
        self = 0xc77300 <main_thread>
        oldset = {
          __val = {0, 5631674, 1184, 0, 0, 6023007, 6, 5944137, 0, 5635264, 140736988778981, 8, 140737488337912, 13783008, 140737340983936, 6}
        }
#7  0x000000000063bd7e in flush_stack_call_func (arg=0x7fffffffbb80, func=0x63b160 <really_call_select>) at lisp.h:4509
        sa = {
          func = 0x419450 <pselect@plt>, 
          max_fds = 6, 
          rfds = 0x7fffffffbc70, 
          wfds = 0x7fffffffbcf0, 
          efds = 0x0, 
          timeout = 0x7fffffffc280, 
          sigmask = 0x0, 
          result = -135497884
        }
#8  0x000000000063bd7e in thread_select (func=<optimized out>, max_fds=max_fds@entry=6, rfds=rfds@entry=0x7fffffffbc70, wfds=wfds@entry=0x7fffffffbcf0, efds=efds@entry=0x0, timeout=timeout@entry=0x7fffffffc280, sigmask=0x0) at thread.c:656
        sa = {
          func = 0x419450 <pselect@plt>, 
          max_fds = 6, 
          rfds = 0x7fffffffbc70, 
          wfds = 0x7fffffffbcf0, 
          efds = 0x0, 
          timeout = 0x7fffffffc280, 
          sigmask = 0x0, 
          result = -135497884
        }
#9  0x0000000000668a3e in xg_select (fds_lim=6, rfds=rfds@entry=0x7fffffffc3f0, wfds=wfds@entry=0x7fffffffc470, efds=efds@entry=0x0, timeout=timeout@entry=0x7fffffffc280, sigmask=sigmask@entry=0x0) at xgselect.c:184
        all_rfds = {
          fds_bits = {32, 0 <repeats 15 times>}
        }
        all_wfds = {
          fds_bits = {0 <repeats 16 times>}
        }
        tmo = {
          tv_sec = 0, 
          tv_nsec = 0
        }
        tmop = 0x7fffffffc280
        context = 0xdb81a0
        have_wfds = <optimized out>
        gfds_buf = {{
            fd = 5, 
            events = 1, 
            revents = 0
          }, {
            fd = 15393381, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 48, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 6, 
            events = 0, 
            revents = 0
          }, {
            fd = 2, 
            events = 0, 
            revents = 0
          }, {
            fd = 2, 
            events = 0, 
            revents = 0
          }, {
            fd = 2, 
            events = 0, 
            revents = 0
          }, {
            fd = 2, 
            events = 0, 
            revents = 0
          }, {
            fd = -334913767, 
            events = 32767, 
            revents = 0
          }, {
            fd = 15040436, 
            events = 0, 
            revents = 0
          }, {
            fd = -331543520, 
            events = 32767, 
            revents = 0
          }, {
            fd = 6400, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 7, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = -1089891930, 
            events = 65152, 
            revents = 22775
          }, {
            fd = 13945992, 
            events = 0, 
            revents = 0
          }, {
            fd = 6448, 
            events = 0, 
            revents = 0
          }, {
            fd = -440, 
            events = 65535, 
            revents = 65535
          }, {
            fd = 100, 
            events = 0, 
            revents = 0
          }, {
            fd = 103, 
            events = 148, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 103, 
            events = 119, 
            revents = 0
          }, {
            fd = 12, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 124, 
            events = 111, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = -331543616, 
            events = 32767, 
            revents = 0
          }, {
            fd = 6400, 
            events = 0, 
            revents = 0
          }, {
            fd = -440, 
            events = 65535, 
            revents = 65535
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = -334909086, 
            events = 32767, 
            revents = 0
          }, {
            fd = 6400, 
            events = 0, 
            revents = 0
          }, {
            fd = 6400, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 5873064, 
            events = 0, 
            revents = 0
          }, {
            fd = 3840, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 24, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 15398112, 
            events = 0, 
            revents = 0
          }, {
            fd = 5873287, 
            events = 0, 
            revents = 0
          }, {
            fd = 3840, 
            events = 0, 
            revents = 0
          }, {
            fd = 15398096, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 4357902, 
            events = 0, 
            revents = 0
          }, {
            fd = 15397616, 
            events = 0, 
            revents = 0
          }, {
            fd = 321, 
            events = 0, 
            revents = 0
          }, {
            fd = 15397600, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 336, 
            events = 0, 
            revents = 0
          }, {
            fd = -334906405, 
            events = 32767, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = -331543616, 
            events = 32767, 
            revents = 0
          }, {
            fd = -511466344, 
            events = 25, 
            revents = 0
          }, {
            fd = 321, 
            events = 0, 
            revents = 0
          }, {
            fd = 321, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 5872976, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 25, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 14523552, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 25, 
            revents = 0
          }, {
            fd = 4381164, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 4830954, 
            events = 0, 
            revents = 0
          }, {
            fd = 15393376, 
            events = 0, 
            revents = 0
          }, {
            fd = 15393376, 
            events = 2, 
            revents = 0
          }, {
            fd = -16220, 
            events = 32767, 
            revents = 0
          }, {
            fd = 15393376, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 0, 
            revents = 0
          }, {
            fd = 14523552, 
            events = 0, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 0
          }, {
            fd = 4409751, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 5, 
            events = 0, 
            revents = 0
          }, {
            fd = 2, 
            events = 0, 
            revents = 0
          }, {
            fd = 538976288, 
            events = 80, 
            revents = 0
          }, {
            fd = 25, 
            events = 80, 
            revents = 0
          }, {
            fd = 2, 
            events = 8224, 
            revents = 8224
          }, {
            fd = 24, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 25, 
            revents = 0
          }, {
            fd = 80, 
            events = 1, 
            revents = 0
          }, {
            fd = 80, 
            events = 0, 
            revents = 256
          }, {
            fd = 0, 
            events = 80, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 25, 
            events = 25, 
            revents = 0
          }, {
            fd = 6457486, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 5510312, 
            events = 0, 
            revents = 0
          }, {
            fd = -16048, 
            events = 32767, 
            revents = 0
          }, {
            fd = 13783008, 
            events = 65533, 
            revents = 65535
          }, {
            fd = -512860104, 
            events = 32767, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 37392, 
            events = 0, 
            revents = 0
          }, {
            fd = 14523557, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 5, 
            events = 5, 
            revents = 0
          }, {
            fd = 37392, 
            events = 0, 
            revents = 0
          }, {
            fd = 14523557, 
            events = 0, 
            revents = 0
          }, {
            fd = 48, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = 0, 
            events = 0, 
            revents = 0
          }, {
            fd = 1, 
            events = 0, 
            revents = 0
          }, {
            fd = -15984, 
            events = 32767, 
            revents = 0
          }, {
            fd = -334485062, 
            events = 32767, 
            revents = 0
          }}
        gfds = 0x7fffffffbd70
        gfds_size = <optimized out>
        n_gfds = <optimized out>
        retval = 0
        our_fds = 0
        max_fds = <optimized out>
        i = <optimized out>
        nfds = <optimized out>
        tmo_in_millisec = -1
        must_free = <optimized out>
        need_to_dispatch = <optimized out>
#10 0x00000000006191a3 in wait_reading_process_output (time_limit=time_limit@entry=100, nsecs=nsecs@entry=0, read_kbd=read_kbd@entry=0, do_display=do_display@entry=false, wait_for_cell=wait_for_cell@entry=XIL(0), wait_proc=wait_proc@entry=0x0, just_wait_proc=0) at process.c:5711
        tls_nfds = 0
        tls_available = {
          fds_bits = {0 <repeats 16 times>}
        }
        process_skipped = <optimized out>
        wrapped = <optimized out>
        channel_start = <optimized out>
        child_fd = <optimized out>
        last_read_channel = -1
        channel = <optimized out>
        nfds = <optimized out>
        Available = {
          fds_bits = {0 <repeats 16 times>}
        }
        Writeok = {
          fds_bits = {0 <repeats 16 times>}
        }
        check_write = true
        check_delay = <optimized out>
        no_avail = false
        xerrno = 4
        proc = <optimized out>
        timeout = {
          tv_sec = 99, 
          tv_nsec = 999925830
        }
        end_time = <optimized out>
        timer_delay = <optimized out>
        got_output_end_time = {
          tv_sec = 0, 
          tv_nsec = -1
        }
        wait = TIMEOUT
        got_some_output = -1
        prev_wait_proc_nbytes_read = 0
        retry_for_async = <optimized out>
        now = <optimized out>
#11 0x000000000042788b in Fsleep_for (seconds=<optimized out>, milliseconds=<optimized out>) at lisp.h:1192
        t = {
          tv_sec = 100, 
          tv_nsec = 0
        }
        tend = {
          tv_sec = 1756225164, 
          tv_nsec = 695603177
        }
        duration = <optimized out>
#12 0x00000000005c1f2e in eval_sub (form=<optimized out>) at lisp.h:2243
        i = <optimized out>
        maxargs = 2
        args_left = XIL(0)
        numargs = 1
        original_fun = <optimized out>
        original_args = XIL(0x7ffff73ae593)
        count = {
          bytes = 1024
        }
        fun = <optimized out>
        val = <optimized out>
        funcar = <optimized out>
        argvals = {make_fixnum(100), XIL(0), XIL(0x7fffffffc748), XIL(0x400), make_fixnum(0), XIL(0x400), make_fixnum(0), XIL(0x7fffffffc750)}
#13 0x00000000005ea19a in readevalloop_eager_expand_eval (val=<optimized out>, macroexpand=XIL(0xb340)) at lisp.h:1192
#14 0x00000000005f0dbf in readevalloop (readcharfun=XIL(0xe57515), infile0=0x0, sourcename=XIL(0xe213f4), printflag=false, unibyte=<optimized out>, readfun=XIL(0), start=XIL(0), end=XIL(0)) at lread.c:2548
        c = <optimized out>
        val = XIL(0x7ffff73ae583)
        b = <optimized out>
        continue_reading_p = true
        lex_bound = <optimized out>
        whole_buffer = true
        first_sexp = <optimized out>
        macroexpand = XIL(0xb340)
#15 0x00000000005f2070 in Feval_buffer (buffer=<optimized out>, printflag=XIL(0), filename=XIL(0xe213f4), unibyte=XIL(0), do_allow_print=<optimized out>) at lisp.h:1488
        tem = XIL(0)
        buf = XIL(0xe57515)
#16 0x00007ffff5f1a59e in F6c6f61642d776974682d636f64652d636f6e76657273696f6e_load_with_code_conversion_0 () at /usr/local/home/sbaugh/src/emacs/emacs-30/src/../native-lisp/30.1.90-04e012b0/preloaded/mule-0685467c-3b823856.eln
#17 0x00000000005beba0 in Ffuncall (nargs=nargs@entry=5, args=args@entry=0x7fffffffcaa0) at eval.c:3097
        val = <optimized out>
#18 0x00000000005f1879 in Fload (file=XIL(0xe210a4), noerror=XIL(0), nomessage=XIL(0x30), nosuffix=<optimized out>, must_suffix=<optimized out>) at lisp.h:1488
        val = <optimized out>
        stream = 0x0
        fd = 5
        found = XIL(0xe213f4)
        efound = <optimized out>
        hist_file_name = <optimized out>
        newer = false
        compiled = false
        handler = <optimized out>
        fmode = 0x6b41e7 "r"
        version = 0
        no_native = false
        is_module = false
        is_native_elisp = false
        found_eff = <optimized out>
        is_elc = false
        input = {
          stream = 0xeed314, 
          lookahead = -92 '\244', 
          buf = "\020\342\000"
        }
#19 0x00007ffff57d56bc in F636f6d6d616e642d6c696e652d31_command_line_1_0 () at /usr/local/home/sbaugh/src/emacs/emacs-30/src/../native-lisp/30.1.90-04e012b0/preloaded/startup-1d646357-21c0c8b9.eln
#20 0x00000000005beba0 in Ffuncall (nargs=2, args=0x7fffffffd080) at eval.c:3097
        val = <optimized out>
#21 0x00007ffff57cd3af in F636f6d6d616e642d6c696e65_command_line_0 () at /usr/local/home/sbaugh/src/emacs/emacs-30/src/../native-lisp/30.1.90-04e012b0/preloaded/startup-1d646357-21c0c8b9.eln
#22 0x00000000005beba0 in Ffuncall (nargs=1, args=0x7fffffffd158) at eval.c:3097
        val = <optimized out>
#23 0x00007ffff57c8bfb in F6e6f726d616c2d746f702d6c6576656c_normal_top_level_0 () at /usr/local/home/sbaugh/src/emacs/emacs-30/src/../native-lisp/30.1.90-04e012b0/preloaded/startup-1d646357-21c0c8b9.eln
#24 0x00000000005c2012 in eval_sub (form=<optimized out>) at lisp.h:2243
        i = 0
        maxargs = <optimized out>
        args_left = XIL(0)
        numargs = 0
        original_fun = <optimized out>
        original_args = XIL(0)
        count = {
          bytes = 128
        }
        fun = <optimized out>
        val = <optimized out>
        funcar = <optimized out>
        argvals = {XIL(0xffffffffffffffff), XIL(0x7fffffffd3b4), XIL(0x7fffffffd3e8), make_fixnum(35184288361560), XIL(0xc77300), XIL(0xe1bb40), XIL(0x7ffff73deb43), make_fixnum(1504411)}
#25 0x00000000005c3b0f in Feval (form=XIL(0x7fffe210eaa3), lexical=lexical@entry=XIL(0x30)) at eval.c:2466
#26 0x0000000000537edb in top_level_2 () at lisp.h:1192
        res = <optimized out>
#27 0x00000000005bd532 in internal_condition_case (bfun=bfun@entry=0x537e90 <top_level_2>, handlers=handlers@entry=XIL(0x90), hfun=hfun@entry=0x53f140 <cmd_error>) at eval.c:1617
        val = <optimized out>
        c = 0xe1bb40
#28 0x0000000000539a02 in top_level_1 (ignore=ignore@entry=XIL(0)) at lisp.h:1192
#29 0x00000000005bd461 in internal_catch (tag=tag@entry=XIL(0x122d0), func=func@entry=0x5399e0 <top_level_1>, arg=arg@entry=XIL(0)) at eval.c:1296
        val = <optimized out>
        c = 0xef0e00
#30 0x0000000000537dbb in command_loop () at lisp.h:1192
#31 0x000000000053ecf6 in recursive_edit_1 () at keyboard.c:754
        val = <optimized out>
#32 0x000000000053f084 in Frecursive_edit () at keyboard.c:837
        buffer = <optimized out>
#33 0x00000000004267a7 in main (argc=<optimized out>, argv=<optimized out>) at emacs.c:2646
        stack_bottom_variable = 0xc4acf64ff28ad800
        old_argc = <optimized out>
        no_loadup = false
        junk = 0x0
        dname_arg = 0x0
        ch_to_dir = 0x0
        original_pwd = <optimized out>
        dump_mode = <optimized out>
        skip_args = 1
        temacs = 0x0
        attempt_load_pdump = <optimized out>
        only_version = <optimized out>
        rlim = {
          rlim_cur = 10022912, 
          rlim_max = 18446744073709551615
        }
        lc_all = <optimized out>
        sockfd = -1
        module_assertions = <optimized out>

In GNU Emacs 30.1.90 (build 9, x86_64-pc-linux-gnu, X toolkit, cairo
 version 1.15.12, Xaw scroll bars) of 2025-08-14 built on
 igm-qws-u22796a
Repository revision: 6adc26ffa74aedbd1cfa9a1ee72073ebccea2b96
Repository branch: emacs-30
Windowing system distributor 'The X.Org Foundation', version 11.0.12101016
System Description: Rocky Linux 8.10 (Green Obsidian)

Configured using:
 'configure --with-x-toolkit=lucid --without-gpm --without-gconf
 --without-selinux --without-imagemagick --with-modules --with-gif=no
 --with-cairo --with-rsvg --without-compress-install --with-tree-sitter
 --with-native-compilation=aot
 PKG_CONFIG_PATH=/usr/local/home/garnish/libtree-sitter/0.22.6-1/lib/pkgconfig/'

Configured features:
CAIRO DBUS FREETYPE GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LIBSYSTEMD
LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP
SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER X11 XDBE XIM
XINPUT2 XPM LUCID ZLIB

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