1 /* Test that pthread_cancel succeeds during thread exit.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* This test tries to trigger an internal race condition in
20 pthread_cancel, where the cancellation signal is sent after the
21 thread has begun the cancellation process. This can result in a
22 spurious ESRCH error. For the original bug 12889, the window is
23 quite small, so the bug was not reproduced in every run. */
27 #include <support/check.h>
28 #include <support/xthread.h>
29 #include <support/xunistd.h>
30 #include <sys/select.h>
33 /* Set to true by timeout_thread_function when the test should
38 timeout_thread_function (void *unused
)
40 usleep (5 * 1000 * 1000);
41 __atomic_store_n (&timeout
, true, __ATOMIC_RELAXED
);
45 /* Used for blocking the select function below. */
46 static int pipe_fds
[2];
49 canceled_thread_function (void *unused
)
59 FD_SET (pipe_fds
[0], &rfs
);
61 /* If the cancellation request is recognized early, the thread
62 begins exiting while the cancellation signal arrives. */
63 select (FD_SETSIZE
, &rfs
, &wfs
, &efs
, NULL
);
72 pthread_t thr_timeout
= xpthread_create (NULL
, timeout_thread_function
, NULL
);
74 while (!__atomic_load_n (&timeout
, __ATOMIC_RELAXED
))
76 pthread_t thr
= xpthread_create (NULL
, canceled_thread_function
, NULL
);
77 xpthread_cancel (thr
);
78 TEST_VERIFY (xpthread_join (thr
) == PTHREAD_CANCELED
);
81 xpthread_join (thr_timeout
);
87 #include <support/test-driver.c>