[PATCH] pthread_detach: add a comment about a known segfault bug.
Florian Weimer
fweimer@redhat.com
Tue Nov 25 19:25:45 GMT 2025
* Aaron Jacobs:
> On Tue, Nov 25, 2025 at 2:21 AM Sam James <sam@gentoo.org> wrote:
>> If you can test azanella's patches in an environment where you can
>> reliably reproduce the bug, that would be very helpful indeed.
>
> Will do if/when I figure out how to wrangle our build system to let me use
> local edits to glibc. But I've also attached a test that reproduces the
> problem once every few thousand runs, at least when run on Google's
> distributed test running system.
>
> (It uses Google-isms, but all open source stuff, and all should be easily
> translatable if you want to avoid the dependencies.)
This is a semi-automatic conversion. It looks okay to me, but I haven't
seen it reproducing the problem.
I wonder if we could speed up the main loop by replacing the final wait
in the main loop with something that looks at the kernel task list for
the process. It's still busy-waiting, but it could reduce the effective
timeout.
Thanks,
Florian
/* Creates and then detaches a bunch of threads, each of which creates
and detaches other threads, and so on, up to a certain number of
total threads. */
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
#include <support/xthread.h>
#include <unistd.h>
static atomic_int threads_left = 500;
static void *
spawn_some_threads (void *ignored)
{
pthread_attr_t attr;
xpthread_attr_init (&attr);
xpthread_attr_setstacksize (&attr, 1 << 18);
int to_spawn = 1 + arc4random_uniform (10);
for (int i = 0; i < to_spawn; ++i)
{
/* Do not go beyond the global limit. */
if (atomic_fetch_sub_explicit (&threads_left, 1,
memory_order_relaxed) <= 0)
break;
xpthread_detach (xpthread_create (&attr, spawn_some_threads, NULL));
}
xpthread_attr_destroy (&attr);
return NULL;
}
static int
do_test (void)
{
for (int j = 0; j < 100; ++j)
{
/* Keep spawning threads until the limit has been reached. */
while (atomic_load_explicit (&threads_left, memory_order_relaxed) > 0)
{
spawn_some_threads (NULL);
usleep (10 * 1000); /* 10 ms */
}
/* Give the detached threads some time to crash. */
usleep (200 * 1000); /* 200 ms */
/* Next iteration will count down from 500 again. */
atomic_store_explicit (&threads_left, 500, memory_order_relaxed);
}
return 0;
}
/* Each iteration runs for about 200 ms, so 100 iterations need about
20 seconds. Use a 60-second timeout to account for slow machines. */
#define TIMEOUT 60
#include <support/test-driver.c>
More information about the Libc-alpha
mailing list