--- Begin Message ---
- To: Debian Bug Tracking System <submit@bugs.debian.org>
- Subject: libc6-amd64: Thread management in a cloned process error
- From: Фёдор Гвоздев <fagvozdev@mail.ru>
- Date: Fri, 10 Jul 2015 14:19:14 +0300
- Message-id: <1436527154.584363326@f304.i.mail.ru>
- Reply-to: Фёдор Гвоздев <fagvozdev@mail.ru>
Package: libc6-amd64
Severity: normal
Dear Maintainer,
It seems that I've found a bug in pthread part or libc.
The point is that software we develop requires to create and destroy threads inside a cloned process asynchronously (PTHREAD_CANCEL_ASYNCHRONOUS is set).
Everything is fine if CLONE_VM is not set, but we need it because main and child process _must_ share the same address space.
Below is an example that demonstrate the question.
Being compiled with CLONE_VM flag the program hangs at pthread_join while without CLONE_VM everything is fine.
We have tested the following example with musl library and everything worked as expected, but we cannot use it for the whole project.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
/* Compile with # gcc -o pthread_test -lpthread pthread_test.c */
#define CLONE 1
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thread_test(void *unused)
{
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_mutex_unlock(&mutex);
pause();
return NULL;
}
int test(void *unused)
{
void *status;
int r;
pthread_t thread;
pthread_mutex_lock(&mutex);
if ((r = pthread_create(&thread, NULL, thread_test, NULL)) != 0) {
printf("pthread_create returned %d\n", r);
exit(1);
}
pthread_mutex_lock(&mutex);
printf("cancelling thread\n");
r = pthread_cancel(thread);
printf("pthread cancel returned : %s(%d)\n", strerror(r), r);
printf("joining thread\n");
r = pthread_join(thread, &status);
printf("pthread_join returned : %s\n", strerror(r));
if (status == PTHREAD_CANCELED)
printf("child was canceled\n");
else
printf("child exit status is %u\n", (unsigned)status);
return 0;
}
int main(void)
{
#if CLONE
void *stack = (void *)malloc(16384);
if (clone(test, stack + 16383, CLONE_VM | CLONE_FS | CLONE_FILES, NULL) < 0) {
printf("clone failed\n");
exit(1);
}
waitpid(0, NULL, __WCLONE);
return 0;
#else
void *stack = (void *)malloc(16384);
if (clone(test, stack + 16383, /*CLONE_VM |*/ CLONE_FS | CLONE_FILES, NULL) < 0) {
printf("clone failed\n");
exit(1);
}
waitpid(0, NULL, __WCLONE);
return 0;
#endif
}
-- System Information:
Debian Release: stretch/sid
APT prefers unstable
APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386
Kernel: Linux 4.0.0-2-amd64 (SMP w/4 CPU cores)
Locale: LANG=ru_RU.UTF-8, LC_CTYPE=ru_RU.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
--- End Message ---
--- Begin Message ---
- To: Фёдор Гвоздев <fagvozdev@mail.ru>
- Cc: 792029-done@bugs.debian.org
- Subject: Re: Bug#792029: libc6-amd64: Thread management in a cloned process error
- From: Aurelien Jarno <aurel32@debian.org>
- Date: Sat, 11 Jan 2025 14:52:49 +0100
- Message-id: <Z4J3sSlABi1tMYcP@aurel32.net>
- In-reply-to: <1436527154.584363326@f304.i.mail.ru>
- References: <1436527154.584363326@f304.i.mail.ru>
Version: 2.24-10
Hi,
On 2015-07-10 14:19, Фёдор Гвоздев wrote:
> Package: libc6-amd64
> Severity: normal
>
> Dear Maintainer,
>
> It seems that I've found a bug in pthread part or libc.
> The point is that software we develop requires to create and destroy threads inside a cloned process asynchronously (PTHREAD_CANCEL_ASYNCHRONOUS is set).
> Everything is fine if CLONE_VM is not set, but we need it because main and child process _must_ share the same address space.
> Below is an example that demonstrate the question.
> Being compiled with CLONE_VM flag the program hangs at pthread_join while without CLONE_VM everything is fine.
This has been fixed in libc6 version 2.24-10 with the following change:
* any/cvs-remove-pid-tid-cache-clone.diff: patch from upstream to
remove cached PID/TID in clone. Closes: #857909.
Closing the bug accordingly.
Regards
Aurelien
--
Aurelien Jarno GPG: 4096R/1DDD8C9B
aurelien@aurel32.net http://aurel32.net
--- End Message ---