unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Philipp Stephani <phst@google.com>,
	Emacs developers <emacs-devel@gnu.org>
Subject: Re: scratch/sigchld-fd 8f0ce42 1/2: Fix deadlock when receiving SIGCHLD during 'pselect'.
Date: Tue, 19 Jan 2021 19:21:39 +0100	[thread overview]
Message-ID: <CAArVCkRg-mamb1YVgzQDYMRsR-CP=cD3OzGsLL3PzWykhi8Yog@mail.gmail.com> (raw)
In-Reply-To: <834kjd7yk5.fsf@gnu.org>

Am Di., 19. Jan. 2021 um 16:09 Uhr schrieb Eli Zaretskii <eliz@gnu.org>:
>
> > Date: Sat, 16 Jan 2021 13:49:49 -0500 (EST)
> > From: p.stephani2@gmail.com (Philipp Stephani)
> >
> > branch: scratch/sigchld-fd
> > commit 8f0ce42d3eb9b212424a4a25a376287ffc94a73e
> > Author: Philipp Stephani <phst@google.com>
> > Commit: Philipp Stephani <phst@google.com>
> >
> >     Fix deadlock when receiving SIGCHLD during 'pselect'.
> >
> >     If we receive and handle a SIGCHLD signal for a process while waiting
> >     for that process, 'pselect' might never return.  Instead, we have to
> >     explicitly 'pselect' that the process status has changed.  We do this
> >     by writing to a pipe in the SIGCHLD handler and having
> >     'wait_reading_process_output' select on it.
> >
> >     * src/process.c (child_signal_init): New helper function to create a
> >     pipe for SIGCHLD notifications.
> >     (child_signal_read, child_signal_notify): New helper functions to
> >     read from/write to the child signal pipe.
> >     (create_process): Initialize the child signal pipe on first use.
> >     (handle_child_signal): Notify waiters that a process status has
> >     changed.
> >     (wait_reading_process_output): Make sure that we also catch
> >     SIGCHLD/process status changes.
> >
> >     * test/src/process-tests.el
> >     (process-tests/fd-setsize-no-crash/make-process): Remove workaround,
> >     which is no longer needed.
>
> Philipp, can you please elaborate about this changeset (which was in
> the meantime merged with master)?  The comments you added mention some
> kind of deadlock, but don't describe what kind and in which situations
> it could happen, and without that it's hard to understand what
> problem(s) it tries to solve.

I had hoped that the fixed unit test and the commit description would
be clear enough. The issue appears to be that accept-process-output
frequently hangs (doesn't return) even though the process has already
finished. That's what process-tests/fd-setsize-no-crash/make-process
relies on. After some printf debugging, I concluded that
wait_reading_process_output wouldn't be notified about the process
status change in any way, and thus hang forever. A form like (sit-for
0.1) would trigger the process status change to be processed.

>
> In general, pselect is supposed to return with EINTR when SIGCHLD
> happoens while we are inside the call to pselect, and EINTR seems to
> be already handled by wait_reading_process_output.  So I wonder why we
> need that additional "self-pipe" to be watched by pselect.

Yes, I'm wondering about that as well, but it's definitely the
behavior I see. Before commiting to master, I ran the test
process-tests/fd-setsize-no-crash/make-process multiple times with and
without the commit, and the outcome was clear: without the commit
accept-process-output would frequently hang, with the commit it never
hangs.
This is pure speculation, but I could imagine multiple things going on:
- Maybe there's no guarantee that pselect actually returns EINTR on SIGCHLD.
- Maybe EINTR is returned too early, before the signal handler got the
chance to update the process status.

>
> In addition, AFAIU this pipe should not be needed on MS-Windows, where
> the pselect emulation waits on the sub-process handles together with
> the other file descriptors, and so gets awakened when a process exits
> or dies.  But again, without knowing the exact situations against
> which this changeset tries to protect, it is hard to make a decision.
>

It's definitely not needed on Windows, which has a superior mechanism
anyway (process handles are waitable objects in Windows). I opted to
create the additional pipe on Windows as well - the costs should be
small, and it keeps the code more consistent between the operating
systems.



  reply	other threads:[~2021-01-19 18:21 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210116184947.2105.45267@vcs0.savannah.gnu.org>
     [not found] ` <20210116184949.3C17C211A5@vcs0.savannah.gnu.org>
2021-01-19 15:10   ` scratch/sigchld-fd 8f0ce42 1/2: Fix deadlock when receiving SIGCHLD during 'pselect' Eli Zaretskii
2021-01-19 18:21     ` Philipp Stephani [this message]
2021-01-19 19:14       ` Eli Zaretskii
2021-01-19 20:22         ` Philipp Stephani
2021-01-20 15:05           ` Eli Zaretskii
2021-01-23 17:36             ` Philipp Stephani
2021-01-23 18:23               ` Eli Zaretskii
2021-01-23 18:30                 ` Philipp Stephani
2021-01-19 20:46         ` Andreas Schwab
2021-01-19 20:58           ` Philipp Stephani
2021-01-20 18:07             ` Andreas Schwab
2021-01-20  3:37           ` Eli Zaretskii
2021-01-20  8:37             ` Andreas Schwab
2021-01-20  8:52               ` Eli Zaretskii
2021-01-20  9:18                 ` Andreas Schwab
2021-01-20 10:14                   ` Eli Zaretskii
2021-01-20 10:17                     ` Andreas Schwab
2021-01-20 15:30                       ` Eli Zaretskii
2021-01-20 15:48                         ` Andreas Schwab
2021-01-20 16:40                           ` Eli Zaretskii
2021-01-20 16:44                             ` Andreas Schwab
2021-01-20 17:03                               ` Eli Zaretskii
2021-01-20 17:30                                 ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAArVCkRg-mamb1YVgzQDYMRsR-CP=cD3OzGsLL3PzWykhi8Yog@mail.gmail.com' \
    --to=p.stephani2@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=phst@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).