Bug 120125

Summary: pthread_setcancelstate() can't disable pthread_cancel()
Product: Red Hat Enterprise Linux 3 Reporter: Shao Hui <hshao>
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED ERRATA QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 3.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: glibc-2.3.2-95.20 Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-04-08 03:11:47 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Shao Hui 2004-04-06 08:52:58 UTC
Description of problem:
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) behave very strange.
it can't disable pthread_cancel(). see the code below.

Version-Release number of selected component (if applicable):
glibc-2.3.2-95.3
kernel-2.4.21-9.0.1.EL

How reproducible:
#include <stdio.h>
#include <pthread.h>

pthread_t t1;
pthread_t t2;

void *thread1(void *arg)
{
	printf("thread1: started\n");
	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
	printf("thread1: disable cancel\n");
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	sleep(3);
	printf("thread1: here we go\n");
	printf("thread1: enable cancel\n");
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	printf("thread1: test cancel\n");
	pthread_testcancel();
	printf("thread1: return\n");
}

void *thread2(void *arg)
{
	printf("thread2: started\n\n");
	sleep(1);
	printf("thread2: cancel thread1\n");
	pthread_cancel(t1);
	printf("thread2: thread canceled\n");
	printf("thread2: return\n");
}

main(int argc, char **argv)
{
	pthread_create(&t1, NULL, thread1, NULL);
	pthread_create(&t2, NULL, thread2, NULL);
	pthread_join(t2, NULL);
	printf("thread2: exited\n\n");
	pthread_join(t1, NULL);
	printf("thread1: exited\n");
}

gcc -o test test.c -lpthread
./test
thread1: started
thread1: disable cancel
thread2: started

thread2: cancel thread1
thread2: thread canceled
thread2: return
thread2: exited

thread1: here we go
thread1: exited

export LD_ASSUME_KERNEL=2.4.1
./test
thread1: started
thread1: disable cancel
thread2: started

thread2: cancel thread1
thread2: thread canceled
thread2: return
thread2: exited

thread1: here we go
thread1: enable cancel
thread1: test cancel
thread1: exited

after thread1 print "here we go", thread1 exited. Is there a cancel
testing
exactly after printf()? but the cancelstate should be disabled. when I
export
LD_ASSUME_KERNEL=2.4.1, the result looks good. is it a bug of NPTL? or
just a
feature?

Steps to Reproduce:
1.
2.
3.
  
Actual results:


Expected results:


Additional info:

Comment 1 Jakub Jelinek 2004-04-07 11:21:44 UTC
Works for me with current RHEL3 U2 glibc
(ftp://people.redhat.com/jakub/glibc/2.3.2-95.20/)
as well as FC2t2 glibc.
There have been several fixes related to PTHREAD_CANCEL_DISABLE in
glibc after RHEL3 has been released.

Comment 2 Shao Hui 2004-04-08 03:11:47 UTC
yes, glibc-2.3.2-95.20 works for me, thanks a lot :)