Bug 82027

Summary: Cannot create and delete threads indefinitely
Product: [Retired] Red Hat Linux Reporter: Jacques ANTHOINE <janthoine>
Component: glibcAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.2CC: fweimer
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2003-01-20 18:00:09 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 Jacques ANTHOINE 2003-01-16 15:02:20 UTC
Description of problem:
An main thread creates several threads and then delete them with pthread_cancel
() function.
After many tries, the pthread_create() function fails with EAGAIN errno.

The created threads are waiting on inputs from a device and cannot test the 
annulation point.

It seems to me the the max number of threads (1024) is reached.
In the linux documentation, I read the pthread_cancel() force the cancelled 
thread to call the pthread_exit() function. 

It seems to me the thread number is not decremented on a pthread_cancel() 
and/or pthread_exit().


Version-Release number of selected component (if applicable):
RedHat 7.2 , kernel 2.4

How reproducible:

Easy



Steps to Reproduce:
1. Test the following program :

	#include <pthread.h>
	#include <stdio.h>
	#include <stdlib.h>
	#include <stdio.h>
	#include <string.h>
	#include <unistd.h>

	#define NB_THREADS	10

	void * fn_thread (void * numero);

	static int compteur = 0;

int main (void)
{

	pthread_t thread [NB_THREADS];
	int       i;
	int       ret;

	while (1)
	{
		compteur ++;
		for (i = 0; i < NB_THREADS; i ++)
			if ((ret = pthread_create (& thread [i], 
                                    NULL, 
                                    fn_thread, 
                                    (void *) i)) != 0) 
			{
				perror("Error in pthread_create ");
				printf ("%s", strerror (ret));
				exit (1);
			}
		printf("Create %d threads \n",compteur*NB_THREADS);
		sleep(2);

		for (i = 0; i < NB_THREADS; i ++)
			pthread_cancel (thread [i]);
	}

	return (0);
}

void * fn_thread (void * num)
{
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

	while (1) {
		sleep(10);
	}
}

2. Build the executable code with the Makefile :

SHELL=/bin/sh

CFLAGS = -Wall -pedantic -g -D_REENTRANT -lpthread

EXECUTABLES=	exemple_create



all : ${EXECUTABLES}

clean : 
	@rm -f core *.o *.out
	@rm -f ${EXECUTABLES}


3. Run the program and get the following trace

[root@quickthemis 34]# ./exemple_create
Create 10 threads
Create 20 threads
Create 30 threads
Create 40 threads
Create 50 threads
Create 60 threads
Create 70 threads
Create 80 threads
Create 90 threads
Create 100 threads
Create 110 threads
Create 120 threads
Create 130 threads
Create 140 threads
Create 150 threads
Create 160 threads
Create 170 threads
Create 180 threads
Create 190 threads
Create 200 threads
Create 210 threads
Create 220 threads
Create 230 threads
Create 240 threads
Create 250 threads
Error in pthread_create : Interrupted system call
    
Actual results:

The threads are deleted by the pthread_cancel . Then threads creation failed on 
EAGAIN errno after 25 loops.

Expected results:

This program must run without limits

Additional info:

Comment 1 Jakub Jelinek 2003-01-20 18:00:09 UTC
Well, by default threads are created in PTHREAD_CREATE_JOINABLE detachstate,
which means unless you pthread_join on them, their resources aren't freed.
See man pthread_attr_setdetachstate(3), pthread_detach(3), pthread_join(3).

Comment 2 Jacques ANTHOINE 2003-01-21 08:55:12 UTC
Thanks a lot for your help.

Now, all is OK.

Best regards,