Subject: bug#28630: 27.0.50;C-g while a non-main thread is sitting crashes Emacs

From: [email-masked]
Date: Mon, 9 Oct 2017 19:34:28 +0900

>> In principle, the method I used for the Mac port is also applicable to
>> X11 (but probably not for NS where secondary threads cannot read
>> keyboard input).  Each pselect call not involving keyboard input
>> additionally monitors one side of a socket pair, and the SIGALRM
>> handler writes to the other side.  This way, a pselect call in a
>> secondary thread is unblocked when the signal arrived, and then it
>> tries to see if keyboard input is available when none of other threads
>> is monitoring the keyboard input channel.
>
> But the problematic part in these examples is that the main thread
> always waits for keyboard input, because the non-main thread starts
> running only when the main thread becomes idle, and becoming idle
> means calling pselect.  And since the main thread is always the first
> one to call pselect, it is the thread which finds the keyboard
> descriptor unmonitored, and installs itself as its monitoring thread.
>
> So with your proposal, more often than not, the "none of other threads
> is monitoring the keyboard input channel" part will never be true,
> except for the main thread.  Or am I missing something?

In both Examples, the main thread is not blocking at pselect, but
thread-join (sys_cond_wait internally), i.e., without monitoring
the keyboard descriptor. The secondary one is blocking at pselect
in sit-for (Example 1) or sleep-for (Example 2).  So "none of
other threads is monitoring the keyboard input channel" part
becomes true for the secondary thread after returning from
pselect.

In Example 2, no thread is monitoring the keyboard while the
secondary thread is blocking at pselect, and that is why the Mac
port could not be interrupted with C-g previously.  I think this
basically also applies to X11.  For GTK+ cases, my guess is the
additionally monitored descriptors obtained by
g_main_context_query has something to do with its peculiar
behavior.

				     YAMAMOTO Mitsuharu
				[email-masked]