Subject: bug#36609: 27.0.50; Possible race-condition in threading implementation

From: Eli Zaretskii <[email-masked]>
Date: Sun, 06 Jun 2021 19:35:31 +0300

> From: [email-masked]
> Cc: Pip Cet <[email-masked]>, [email-masked], [email-masked],
>     Eli Zaretskii <[email-masked]>
> Date: Sun, 06 Jun 2021 11:25:01 -0400
> 
> #1. Want to revert commit 9c62ffb

This will bring back bug#36609, so we cannot do that without
discussing first why you think that commit was wrong.

> #2. Fails on tip of master, succeeds after patch in #1.

Please explain what does "fails" mean, and why do you think the above
commit is the culprit.  (A much simpler test case will be
appreciated, btw.)

>     Fails not necessarily because xgselect.c is wrong, but rather because
>     channel-recv blocks on a mutex before channel-send can get its act together.

You mean, in this code:

   (let ((channel (make-channel 1)))
     (make-thread
      (lambda nil
	(channel-send (car channel) 42))
      "produce")
     (channel-recv (cdr channel))
     (ignore-errors (enable-command 'list-threads))
     (call-interactively #'list-threads))

?  Here, channel-send is called by a new thread, created by
make-thread.  In this code, it is _expected_ that channel-recv will be
called (by the main thread) _before_ channel-send is called by the new
thread, because make-thread creates a thread, but the newly created
thread doesn't run until it can acquire the global lock.  Meanwhile,
the main thread continues running and calls channel-recv.  The new
thread will not begin running, AFAIU, until the main thread calls
condition-wait inside channel-recv.

By "blocks on a mutex", did you mean that channel-recv blocks trying
to acquire the mutex here:

   (cl-defgeneric channel-recv ((sink channel-terminal))
     (with-mutex (oref sink mutex) <<<<<<<<<<<<<<<<<<<<<<<<<<
       (with-slots (condition msg-queue) sink

If so, which thread holds that mutex at this point?

> #4. What #3 probably intended, succeeds after patch in #1.

Yes, race conditions can be solved by using sleep-for, but that's not
really a clean solution, at least not in my book.