Subject: bug#32487: 26.1.50; keyboard-quit while main thread blocked crashes Emacs

From: Gemini Lasswell <[email-masked]>
Date: Mon, 20 Aug 2018 15:57:08 -0700

In both emacs-26 and master, it's possible to hang or crash Emacs with
Lisp code which uses threads and is buggy in ways which cause the main
thread to be blocked, combined with keyboard-quit.

To reproduce, copy the code listed below into a buffer and evaluate it
(with emacs -Q if you wish).  Then choose one of:

M-x my-hang-1 RET
M-x my-hang-2 RET
M-x my-hang-3 RET

Result: Emacs will stop responding to keyboard input.  Repeated use of
C-g will do nothing when running under X11 and will cause Emacs to crash
when running in a terminal.

(defvar my-mutex-1 (make-mutex "mutex-1"))

(defun my-wait-on-locked-mutex ()
  (with-mutex my-mutex-1
    (message "mutex-1 wasn't locked")))

(defun my-hang-1 ()
  (interactive)
  (with-mutex my-mutex-1
    (let ((thread (make-thread #'my-wait-on-locked-mutex)))
      (thread-join thread))))

(defvar my-mutex-2 (make-mutex "mutex-2"))
(defvar my-var-2 nil)

(defun my-inf-loop ()
  (mutex-lock my-mutex-2)
  (while t
    (setq my-var-2 t)
    (thread-yield)))

(defun my-hang-2 ()
  (interactive)
  (setq my-var-2 nil)
  (let ((thread (make-thread #'my-inf-loop)))
    (while (not my-var-2)
      (thread-yield))
    (with-mutex my-mutex-2
      (message "mutex-2 wasn't locked"))))

(defvar my-cond-mutex (make-mutex "cond-mutex"))
(defvar my-cond-var (make-condition-variable my-cond-mutex))
(defvar my-var-3 nil)

(defun my-fail-to-notify ()
  (with-mutex my-cond-mutex
    (setq my-var-3 t)))

(defun my-hang-3 ()
  (interactive)
  (setq my-var-3 nil)
  (let ((thread (make-thread #'my-fail-to-notify)))
    (with-mutex my-cond-mutex
      (while (not my-var-3)
	(condition-wait my-cond-var)))))

In GNU Emacs 26.1.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.28)
 of 2018-08-05 built on localhost
Windowing system distributor 'The X.Org Foundation', version 11.0.11906000
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.

Configured using:
 'configure
 --prefix=/nix/store/4pwyx7ixl5fbdj75kmbsnzcrbsvkvg09-emacs-26.1.50
 --with-modules --with-x-toolkit=gtk3 --with-xft'

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND DBUS GSETTINGS NOTIFY
LIBSELINUX GNUTLS LIBXML2 FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11
MODULES THREADS LCMS2

Important settings:
  value of $EMACSLOADPATH: /nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp:
  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
  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
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/magit-2.11.0/magit-popup hides /nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/magit-popup-2.12.2/magit-popup
/nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/soap-client-3.1.3/soap-inspect hides /nix/store/4pwyx7ixl5fbdj75kmbsnzcrbsvkvg09-emacs-26.1.50/share/emacs/26.1.50/lisp/net/soap-inspect
/nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/soap-client-3.1.3/soap-client hides /nix/store/4pwyx7ixl5fbdj75kmbsnzcrbsvkvg09-emacs-26.1.50/share/emacs/26.1.50/lisp/net/soap-client
/nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/seq-2.20/seq hides /nix/store/4pwyx7ixl5fbdj75kmbsnzcrbsvkvg09-emacs-26.1.50/share/emacs/26.1.50/lisp/emacs-lisp/seq
/nix/store/7xj2afwwci2r69g5ghhf7qlinlpxw858-emacs-packages-deps/share/emacs/site-lisp/elpa/let-alist-1.0.5/let-alist hides /nix/store/4pwyx7ixl5fbdj75kmbsnzcrbsvkvg09-emacs-26.1.50/share/emacs/26.1.50/lisp/emacs-lisp/let-alist

Features:
(shadow sort mail-extr emacsbug message rmc puny seq seq-25 byte-opt gv
bytecomp byte-compile cconv cl-loaddefs cl-lib dired dired-loaddefs
format-spec rfc822 mml easymenu mml-sec password-cache epa derived epg
epg-config gnus-util rmail rmail-loaddefs mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils elec-pair time-date
mule-util tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/x-win x-win term/common-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page menu-bar
rfn-eshadow isearch timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core term/tty-colors frame cl-generic 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 charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray minibuffer
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
move-toolbar gtk x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 95020 4549)
 (symbols 48 20383 1)
 (miscs 40 44 118)
 (strings 32 28447 1654)
 (string-bytes 1 773489)
 (vectors 16 14062)
 (vector-slots 8 502938 6272)
 (floats 8 49 68)
 (intervals 56 216 0)
 (buffers 992 11))