public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Zack Weinberg <zackw@panix.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v4 01/21] nptl: Do not close the pipe on tst-cancel{2,3}
Date: Tue, 7 Apr 2020 11:24:57 -0400	[thread overview]
Message-ID: <CAKCAbMht6supqBioSRE-B-WkjAACpz=4AnvZRnUDNhpWwyB4AQ@mail.gmail.com> (raw)
In-Reply-To: <20200403203201.7494-2-adhemerval.zanella@linaro.org>

On Fri, Apr 3, 2020 at 4:32 PM Adhemerval Zanella via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> This can cause a SIGPIPE before SIGCANCEL is processed, which makes
> write fail and the thread return an non expected result.

1) This is a sensible explanation for tst-cancel2.c, but tst-cancel3.c
does a read, not a write; the commit message should explain why this
is an appropriate change for both files.  Something like

# These tests cancel a thread that's supposed to be blocked while
writing or reading from a pipe.  If we close the other end of the
pipe, the closure might be reported to the thread before the
cancellation, causing a spurious failure.

2) The intention of the closes, seems to have been to prevent these
test cases blocking forever in pthread_join if the cancel isn't
delivered.  We don't actually need to do that, because the 20-second
timeout built into the test harness will suffice to make the test fail
in that case, but it might be worth adding a comment somewhere,
explaining that we're relying on the timeout.

3) I think it's no longer necessary to ignore SIGPIPE after this
change, and I think it's no longer necessary to have a loop in 'tf',
please remove that code also.

4) There's still a race in these tests; the cancellation might or
might not yet be pending when the child thread calls read/write.
Abstractly, we should be testing both a cancellation that's already
pending when we reach the cancellation point, and a cancellation
that's delivered while the thread is blocked on I/O.  It's possible to
ensure that the cancellation is already pending with a mutex, e.g.

static pthread_mutex_t prep_gate = PTHREAD_MUTEX_INITIALIZER;
static int fd[2];

static void *
tf (void *arg)
{
  /* The buffer size must be larger than the pipe size so that the
     write blocks.  */
  char buf[100000];

  /* Once we acquire this mutex, a cancellation request will be
     pending for this thread.  (pthread_mutex_(un)lock are not
     cancellation points.)  */
  pthread_mutex_lock (&prep_gate);
  pthread_mutex_unlock (&prep_gate);

  /* This write operation should be immediately cancelled.  */
  write (fd[1], buf, sizeof (buf));

  /* If control reaches this point, the test has failed;
     the parent will detect this.  */
  return arg;
}

static int
do_test (void)
{
  pthread_t th;
  void *r;

  if (pipe (fd) != 0)
    {
      puts ("pipe failed");
      return 1;
    }

  /* The child thread will wait to acquire this mutex.  */
  pthread_mutex_lock (&prep_gate);

  if (pthread_create (&th, NULL, tf, NULL) != 0)
    {
      puts ("create failed");
      return 1;
    }

  if (pthread_cancel (th) != 0)
    {
      puts ("cancel failed");
      return 1;
    }

  pthread_mutex_unlock (&prep_gate);

  // ...
}

But I don't know a good way to _guarantee_ that 'tf' is blocked on
read/write before the parent calls pthread_cancel.   Can you think of
anything?

zw

  reply	other threads:[~2020-04-07 15:25 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-03 20:31 [PATCH v4 00/21] nptl: Fix Race conditions in pthread cancellation [BZ#12683] Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 01/21] nptl: Do not close the pipe on tst-cancel{2,3} Adhemerval Zanella
2020-04-07 15:24   ` Zack Weinberg [this message]
2020-04-07 20:07     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 02/21] nptl: Fix Race conditions in pthread cancellation [BZ#12683] Adhemerval Zanella
2020-04-07 18:24   ` Zack Weinberg
2020-04-08 14:13     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 03/21] nptl: x86_64: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 04/21] nptl: x32: " Adhemerval Zanella
2020-04-03 21:22   ` Joseph Myers
2020-04-07 12:47     ` Adhemerval Zanella
2020-04-07 12:54       ` H.J. Lu
2020-04-07 13:33         ` Adhemerval Zanella
2020-04-07 13:40           ` H.J. Lu
2020-04-07 13:41             ` H.J. Lu
2020-04-07 13:55               ` Adhemerval Zanella
2020-04-07 13:59                 ` H.J. Lu
2020-04-07 14:04                   ` Adhemerval Zanella
2020-04-07 15:45                     ` H.J. Lu
2020-04-07 16:16                       ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 05/21] nptl: i386: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 06/21] nptl: ia64: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 07/21] nptl: mips: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 08/21] nptl: aarch64: " Adhemerval Zanella
2020-04-12 15:29   ` Stepan Golosunov
2020-04-15 14:30     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 09/21] nptl: arm: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 10/21] nptl: powerpc: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 11/21] nptl: microblaze: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 12/21] nptl: sparc: " Adhemerval Zanella
2020-04-12 15:33   ` Stepan Golosunov
2020-04-14 16:54     ` Stepan Golosunov
2020-04-15 14:48       ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 13/21] nptl: hppa: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 14/21] nptl: m68k: " Adhemerval Zanella
2020-04-03 21:34   ` Andreas Schwab
2020-04-07 12:46     ` Adhemerval Zanella
2020-04-12 15:42   ` Stepan Golosunov
2020-04-15 14:51     ` Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 15/21] nptl: alpha: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 16/21] nptl: sh: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 17/21] nptl: riscv: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 18/21] nptl: s390: " Adhemerval Zanella
2020-04-03 20:31 ` [PATCH v4 19/21] nptl: nios2: " Adhemerval Zanella
2020-04-03 20:32 ` [PATCH v4 20/21] nptl: csky: " Adhemerval Zanella
2020-04-03 20:32 ` [PATCH v4 21/21] Linux: Remove sysdep-cancel header Adhemerval Zanella

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='CAKCAbMht6supqBioSRE-B-WkjAACpz=4AnvZRnUDNhpWwyB4AQ@mail.gmail.com' \
    --to=zackw@panix.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@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).