public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Rain <glibc@sunshowers.io>, libc-help@sourceware.org
Subject: Re: posix_spawn: parent can get stuck in uninterruptible sleep if child receives SIGTSTP early enough
Date: Mon, 22 Aug 2022 19:30:06 -0300	[thread overview]
Message-ID: <2afd2b84-e0de-3495-86e4-c8618c6dd3c1@linaro.org> (raw)
In-Reply-To: <dc0c9dac-0868-a176-f7de-20eab4c4a175@linaro.org>



On 22/08/22 14:48, Adhemerval Zanella Netto wrote:

>>>
>>>> 7. However, the clone hasn't exited in the parent and so it remains stuck in the clone3 syscall until the child receives a SIGCONT.
>>>>
>>>> I'm not sure what a reasonable way to handle this would be on the part of my CLI tool. The tool currently just gets stuck in uninterruptible sleep, resulting in a bad user experience.
>>>
>>> Reading through both your twitter discussion and the bug report against your
>>> tool [1] I think it is outside posix_spawn specification on how to handle
>>> SIGSTOP for the helper process itself in the tiny window between process
>>> creation and the setpgid.
>>>
>>>>
>>>> Here are solutions I've thought about that don't seem to work (please correct me if I'm wrong!)
>>>> 1. Setting the signal mask to include SIGTSTP. I do want to be able to send the child SIGTSTP after the clone(), and in my case the child is a third-party process so I can't depend on it to reset the signal mask.
>>>> 2. Spawning a stub process that execves the real child. It seems like the same issue exists when the main process calls the stub process, if I'm understanding the code correctly, so this won't help.
>>>>
>>>> ... though now as I'm writing this email out, maybe one solution is:
>>>>
>>>> * my tool spawns a stub process with SIGTSTP masked.
>>>> * the subprocess unmasks SIGTSTP (so it could receive the SIGTSTP here, but at least it won't block the parent process), then execves the third-party process.
>>>>
>>>> Is that the solution you would recommend?
>>>
>>> I am not sure this would work, since SIGSTOP cannot be caught, blocked, or 
>>> ignored.  What I think if might work is to spawn a stub process and make
>>> it a new session leader with setsid so it will not have a controlling
>>> terminal.  The stub process will be responsible to spawn new processes,
>>> so any interaction with the controlling terminal (the CTRL+Z) won't affected
>>> the posix_spawn helper thread. 
>>
>> That is definitely an interesting solution. However, is it necessary given that
>> Ctrl+Z is actually SIGTSTP, which can be blocked?
>>
>> Thanks again.
> 
> I think one possibility would to set the default signal actions to SIG_IGN, similar
> to POSIX_SPAWN_SETSIGDEF does for SIG_DFL (Solaris have POSIX_SPAWN_SETSIGIGN_NP
> as an extension).  It won't help much if the signal is received in the tiny window 
> between the helper process start and sigaction call, so I am afraid this will only 
> decrease the possibility of the deadlock, not eliminate it.

Sorry I got it backwards, since I forgot that glibc implementation does block
all signal prior spawning the helper process.  In fact using SIG_IGN as default
signal handling would require the spawn process to restore it to SIG_DFL, which
would prevent any process to be stopped.

Which goes back to your original suggestion:

> * my tool spawns a stub process with SIGTSTP masked.
> * the subprocess unmasks SIGTSTP (so it could receive the SIGTSTP here, but at least it won't block the parent process), then execves the third-party process.

Which I agree is a way to handle it, since we can not atomically unblock SIGTSTP
and execve.

I am not sure if this indeed characterize as a posix_spawn bug for glibc, since
to really fixing we would either need to go back to use fork or use something
like a helper process.

      parent reply	other threads:[~2022-08-22 22:30 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-14  3:30 Rain
2022-08-14  3:38 ` Rain
2022-08-22 16:51 ` Adhemerval Zanella Netto
2022-08-22 17:00   ` Rain
2022-08-22 17:48     ` Adhemerval Zanella Netto
2022-08-22 18:21       ` Florian Weimer
2022-08-22 18:32         ` Adhemerval Zanella Netto
2022-08-22 22:28           ` Adhemerval Zanella Netto
2022-09-13 10:04           ` Florian Weimer
2022-09-21 15:24             ` Adhemerval Zanella Netto
2022-09-22 12:18               ` Florian Weimer
2022-09-22 16:56                 ` Adhemerval Zanella Netto
2022-09-22 17:38                   ` Florian Weimer
2022-09-22 19:14                     ` Adhemerval Zanella Netto
2022-10-10 13:45                       ` Florian Weimer
2022-10-18 20:04                         ` Adhemerval Zanella Netto
2022-10-20 11:55                           ` Florian Weimer
2022-10-21  1:40                             ` Rain
2022-10-21 14:18                               ` Szabolcs Nagy
2022-08-22 22:30       ` Adhemerval Zanella Netto [this message]

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

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

  git send-email \
    --in-reply-to=2afd2b84-e0de-3495-86e4-c8618c6dd3c1@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=glibc@sunshowers.io \
    --cc=libc-help@sourceware.org \
    /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.
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).