public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Handles the masked signal when the thread exits
@ 2021-11-18 18:07 Gibeom Gwon
  2021-11-18 18:08 ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Gibeom Gwon @ 2021-11-18 18:07 UTC (permalink / raw)
  To: libc-help

Hello,

I am experiencing strange(unintended?) behavior when using pthread with 
signals. If I set the signal mask with pthread_sigmask() in the thread 
function and the process has pending signal when thread is exiting, 
signal handler executed in thread.

It looks like glibc restores original signal mask at the end of the 
thread. So I suspect this eventually triggers masked signal handler. But 
I'm not sure it is intended or not.

Code samples:

sleep.c
-------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>

pthread_t thread;
pid_t pid;

void stop_threads() {
		pthread_cancel(thread);
		pthread_join(thread, NULL);
}

void sigint_handler(int signum) {
	printf("sigint: %ld\n",pthread_self());
	stop_threads();
	exit(0);
}

void sigchld_handler(int signum) {
	printf("sigchld: %ld\n",pthread_self());
	pid_t pid;
	int status;
	while((pid = waitpid(-1,&status,WNOHANG)) > 0) {}
}

void spawn_sleep() {
	pid = fork();
	if(pid == 0)
		execl("./sleep","sleep",NULL);
}

void* worker(void *arg) {
	sigset_t mask;
	sigfillset(&mask);
	pthread_sigmask(SIG_SETMASK,&mask,NULL);

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
	int i = 10;
	while(i) {
		printf("worker...\n");
		sleep(1);
		i--;
	}
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
     	printf("thread close\n");
	pthread_exit(0);
}

void spawn_thread() {
     pthread_create(&thread,NULL,worker,NULL);
     printf("child thread: %ld\n",thread);
}

int main() {
	printf("main thread: %ld\n",pthread_self());
	spawn_sleep();	

	struct sigaction sa1;
	sa1.sa_handler = sigint_handler;
	sigemptyset(&sa1.sa_mask);
	sigaddset(&sa1.sa_mask,SIGCHLD);
	sa1.sa_flags = 0;
	sigaction(SIGINT, &sa1, 0);

	struct sigaction sa2;
	sa2.sa_handler = sigchld_handler;
	sigemptyset(&sa2.sa_mask);
	sa2.sa_flags = 0;
	sigaction(SIGCHLD, &sa2, 0);

	spawn_thread();

	while(1)
		sleep(1);

	return 0;
}


result
======
$ ./signal
main thread: 139882152073024
child thread: 139882152068672
worker...
worker...
worker...
^Csigint: 139882152073024
worker...
worker...
worker...
worker...
worker...
worker...
worker...
thread close
sigchld: 139882152068672

Comprehensive signal mask set in worker() but SIGCHLD handler executed 
after thread function exited.

Regards,
Gibeom Gwon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:07 Handles the masked signal when the thread exits Gibeom Gwon
@ 2021-11-18 18:08 ` Florian Weimer
  2021-11-18 18:11   ` Gibeom Gwon
  0 siblings, 1 reply; 9+ messages in thread
From: Florian Weimer @ 2021-11-18 18:08 UTC (permalink / raw)
  To: Gibeom Gwon; +Cc: libc-help

* Gibeom Gwon:

> I am experiencing strange(unintended?) behavior when using pthread
> with signals. If I set the signal mask with pthread_sigmask() in the
> thread function and the process has pending signal when thread is
> exiting, signal handler executed in thread.
>
> It looks like glibc restores original signal mask at the end of the
> thread. So I suspect this eventually triggers masked signal
> handler. But I'm not sure it is intended or not.

Which glibc version are you using?

Thanks,
Florian


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:08 ` Florian Weimer
@ 2021-11-18 18:11   ` Gibeom Gwon
  2021-11-18 18:19     ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Gibeom Gwon @ 2021-11-18 18:11 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-help

On 11/19/21 03:08, Florian Weimer wrote:
> * Gibeom Gwon:
> 
>> I am experiencing strange(unintended?) behavior when using pthread
>> with signals. If I set the signal mask with pthread_sigmask() in the
>> thread function and the process has pending signal when thread is
>> exiting, signal handler executed in thread.
>>
>> It looks like glibc restores original signal mask at the end of the
>> thread. So I suspect this eventually triggers masked signal
>> handler. But I'm not sure it is intended or not.
> 
> Which glibc version are you using?
> 
> Thanks,
> Florian
> 

Ah, I forgot to write the version. I'm using 2.33 and linux distribution
  is Arch Linux.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:11   ` Gibeom Gwon
@ 2021-11-18 18:19     ` Florian Weimer
  2021-11-18 18:25       ` Gibeom Gwon
  0 siblings, 1 reply; 9+ messages in thread
From: Florian Weimer @ 2021-11-18 18:19 UTC (permalink / raw)
  To: Gibeom Gwon; +Cc: libc-help

* Gibeom Gwon:

> On 11/19/21 03:08, Florian Weimer wrote:
>> * Gibeom Gwon:
>> 
>>> I am experiencing strange(unintended?) behavior when using pthread
>>> with signals. If I set the signal mask with pthread_sigmask() in the
>>> thread function and the process has pending signal when thread is
>>> exiting, signal handler executed in thread.
>>>
>>> It looks like glibc restores original signal mask at the end of the
>>> thread. So I suspect this eventually triggers masked signal
>>> handler. But I'm not sure it is intended or not.
>> Which glibc version are you using?

> Ah, I forgot to write the version. I'm using 2.33 and linux
> distribution is Arch Linux.

We had some issues with signals and thread exit in 2.34, but they are
exclusive to that release and cannot happen in 2.33.  I can't reproduce
the behavior you see with upstream 2.33, either.

What's your kernel version?  I've tried 5.14.13 and 5.14.17.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:19     ` Florian Weimer
@ 2021-11-18 18:25       ` Gibeom Gwon
  2021-11-18 18:31         ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Gibeom Gwon @ 2021-11-18 18:25 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-help

On 11/19/21 03:19, Florian Weimer wrote:
> * Gibeom Gwon:
> 
>> On 11/19/21 03:08, Florian Weimer wrote:
>>> * Gibeom Gwon:
>>>
>>>> I am experiencing strange(unintended?) behavior when using pthread
>>>> with signals. If I set the signal mask with pthread_sigmask() in the
>>>> thread function and the process has pending signal when thread is
>>>> exiting, signal handler executed in thread.
>>>>
>>>> It looks like glibc restores original signal mask at the end of the
>>>> thread. So I suspect this eventually triggers masked signal
>>>> handler. But I'm not sure it is intended or not.
>>> Which glibc version are you using?
> 
>> Ah, I forgot to write the version. I'm using 2.33 and linux
>> distribution is Arch Linux.
> 
> We had some issues with signals and thread exit in 2.34, but they are
> exclusive to that release and cannot happen in 2.33.  I can't reproduce
> the behavior you see with upstream 2.33, either.
> 
> What's your kernel version?  I've tried 5.14.13 and 5.14.17.
> 
> Thanks,
> Florian
> 

Kernel version is 5.15.2.

Well, I accidently dropped sleep.c code sample. Here are the code
samples again. Sorry to bother you.

sleep.c
-------
#include <unistd.h>

int main() {
	sleep(50);
	return 0;
}

signal.c
--------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>

pthread_t thread;
pid_t pid;

void stop_threads() {
		pthread_cancel(thread);
		pthread_join(thread, NULL);
}

void sigint_handler(int signum) {
	printf("sigint: %ld\n",pthread_self());
	stop_threads();
	exit(0);
}

void sigchld_handler(int signum) {
	printf("sigchld: %ld\n",pthread_self());
	pid_t pid;
	int status;
	while((pid = waitpid(-1,&status,WNOHANG)) > 0) {}
}

void spawn_sleep() {
	pid = fork();
	if(pid == 0)
		execl("./sleep","sleep",NULL);
}

void* worker(void *arg) {
	sigset_t mask;
	sigfillset(&mask);
	pthread_sigmask(SIG_SETMASK,&mask,NULL);

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
	int i = 10;
	while(i) {
		printf("worker...\n");
		sleep(1);
		i--;
	}
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
     	printf("thread close\n");
	pthread_exit(0);
}

void spawn_thread() {
     pthread_create(&thread,NULL,worker,NULL);
     printf("child thread: %ld\n",thread);
}

int main() {
	printf("main thread: %ld\n",pthread_self());
	spawn_sleep();	

	struct sigaction sa1;
	sa1.sa_handler = sigint_handler;
	sigemptyset(&sa1.sa_mask);
	sigaddset(&sa1.sa_mask,SIGCHLD);
	sa1.sa_flags = 0;
	sigaction(SIGINT, &sa1, 0);

	struct sigaction sa2;
	sa2.sa_handler = sigchld_handler;
	sigemptyset(&sa2.sa_mask);
	sa2.sa_flags = 0;
	sigaction(SIGCHLD, &sa2, 0);

	spawn_thread();

	while(1)
		sleep(1);

	return 0;
}


Regards,
Gibeom Gwon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:25       ` Gibeom Gwon
@ 2021-11-18 18:31         ` Florian Weimer
  2021-11-18 18:35           ` Gibeom Gwon
  0 siblings, 1 reply; 9+ messages in thread
From: Florian Weimer @ 2021-11-18 18:31 UTC (permalink / raw)
  To: Gibeom Gwon; +Cc: libc-help

* Gibeom Gwon:

> On 11/19/21 03:19, Florian Weimer wrote:
>> * Gibeom Gwon:
>> 
>>> On 11/19/21 03:08, Florian Weimer wrote:
>>>> * Gibeom Gwon:
>>>>
>>>>> I am experiencing strange(unintended?) behavior when using pthread
>>>>> with signals. If I set the signal mask with pthread_sigmask() in the
>>>>> thread function and the process has pending signal when thread is
>>>>> exiting, signal handler executed in thread.
>>>>>
>>>>> It looks like glibc restores original signal mask at the end of the
>>>>> thread. So I suspect this eventually triggers masked signal
>>>>> handler. But I'm not sure it is intended or not.
>>>> Which glibc version are you using?
>> 
>>> Ah, I forgot to write the version. I'm using 2.33 and linux
>>> distribution is Arch Linux.
>> We had some issues with signals and thread exit in 2.34, but they
>> are
>> exclusive to that release and cannot happen in 2.33.  I can't reproduce
>> the behavior you see with upstream 2.33, either.
>> What's your kernel version?  I've tried 5.14.13 and 5.14.17.
>> Thanks,
>> Florian
>> 
>
> Kernel version is 5.15.2.
>
> Well, I accidently dropped sleep.c code sample. Here are the code
> samples again. Sorry to bother you.

Ah, now the test makes a little bit more sense.  But I still get the
expected output:

main thread: 140317152449984
child thread: 140317150295616
worker...
worker...
worker...
worker...
worker...
worker...
worker...
worker...
worker...
worker...
thread close
sigchld: 140317152449984

So the signal gets delivered on the main thread, as expected.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:31         ` Florian Weimer
@ 2021-11-18 18:35           ` Gibeom Gwon
  2021-11-18 19:10             ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Gibeom Gwon @ 2021-11-18 18:35 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-help

On 11/19/21 03:31, Florian Weimer wrote:
> * Gibeom Gwon:
> 
>> On 11/19/21 03:19, Florian Weimer wrote:
>>> * Gibeom Gwon:
>>>
>>>> On 11/19/21 03:08, Florian Weimer wrote:
>>>>> * Gibeom Gwon:
>>>>>
>>>>>> I am experiencing strange(unintended?) behavior when using pthread
>>>>>> with signals. If I set the signal mask with pthread_sigmask() in the
>>>>>> thread function and the process has pending signal when thread is
>>>>>> exiting, signal handler executed in thread.
>>>>>>
>>>>>> It looks like glibc restores original signal mask at the end of the
>>>>>> thread. So I suspect this eventually triggers masked signal
>>>>>> handler. But I'm not sure it is intended or not.
>>>>> Which glibc version are you using?
>>>
>>>> Ah, I forgot to write the version. I'm using 2.33 and linux
>>>> distribution is Arch Linux.
>>> We had some issues with signals and thread exit in 2.34, but they
>>> are
>>> exclusive to that release and cannot happen in 2.33.  I can't reproduce
>>> the behavior you see with upstream 2.33, either.
>>> What's your kernel version?  I've tried 5.14.13 and 5.14.17.
>>> Thanks,
>>> Florian
>>>
>>
>> Kernel version is 5.15.2.
>>
>> Well, I accidently dropped sleep.c code sample. Here are the code
>> samples again. Sorry to bother you.
> 
> Ah, now the test makes a little bit more sense.  But I still get the
> expected output:
> 
> main thread: 140317152449984
> child thread: 140317150295616
> worker...
> worker...
> worker...
> worker...
> worker...
> worker...
> worker...
> worker...
> worker...
> worker...
> thread close
> sigchld: 140317152449984
> 
> So the signal gets delivered on the main thread, as expected.
> 
> Thanks,
> Florian
> 

I hit Ctrl+C in the middle of running signal program. You can see
^C and sigint:.. printed in my result.

Regards,
Gibeom Gwon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 18:35           ` Gibeom Gwon
@ 2021-11-18 19:10             ` Florian Weimer
  2021-11-18 19:29               ` Gibeom Gwon
  0 siblings, 1 reply; 9+ messages in thread
From: Florian Weimer @ 2021-11-18 19:10 UTC (permalink / raw)
  To: Gibeom Gwon; +Cc: libc-help

* Gibeom Gwon:

> I hit Ctrl+C in the middle of running signal program. You can see
> ^C and sigint:.. printed in my result.

I see it now.  It's a real bug.  It only happens with cancellation.

Would yo umind filing a bug here?

  <https://sourceware.org/bugzilla/enter_bug.cgi?product=glibc&component=nptl>

The fix should look something like this:

diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index d6ea43a754..bad9eeb52f 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -406,8 +406,6 @@ start_thread (void *arg)
   unwind_buf.priv.data.prev = NULL;
   unwind_buf.priv.data.cleanup = NULL;
 
-  __libc_signal_restore_set (&pd->sigmask);
-
   /* Allow setxid from now onwards.  */
   if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
     futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
@@ -417,6 +415,8 @@ start_thread (void *arg)
       /* Store the new cleanup handler info.  */
       THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
 
+      __libc_signal_restore_set (&pd->sigmask);
+
       LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg);
 
       /* Run the code the user provided.  */

Thanks,
Florian


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Handles the masked signal when the thread exits
  2021-11-18 19:10             ` Florian Weimer
@ 2021-11-18 19:29               ` Gibeom Gwon
  0 siblings, 0 replies; 9+ messages in thread
From: Gibeom Gwon @ 2021-11-18 19:29 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-help

On 11/19/21 04:10, Florian Weimer wrote:
> * Gibeom Gwon:
> 
>> I hit Ctrl+C in the middle of running signal program. You can see
>> ^C and sigint:.. printed in my result.
> 
> I see it now.  It's a real bug.  It only happens with cancellation.
> 
> Would yo umind filing a bug here?
> 
>    <https://sourceware.org/bugzilla/enter_bug.cgi?product=glibc&component=nptl>
> 

Opened the bug. <https://sourceware.org/bugzilla/show_bug.cgi?id=28607>
Thank you!

Regards,
Gibeom Gwon


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-11-18 19:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18 18:07 Handles the masked signal when the thread exits Gibeom Gwon
2021-11-18 18:08 ` Florian Weimer
2021-11-18 18:11   ` Gibeom Gwon
2021-11-18 18:19     ` Florian Weimer
2021-11-18 18:25       ` Gibeom Gwon
2021-11-18 18:31         ` Florian Weimer
2021-11-18 18:35           ` Gibeom Gwon
2021-11-18 19:10             ` Florian Weimer
2021-11-18 19:29               ` Gibeom Gwon

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).