[PATCH] Make exit thread-safe

Jakub Jelinek jakub@redhat.com
Thu Feb 24 09:58:00 GMT 2005


On Thu, Feb 24, 2005 at 02:51:00PM +0530, Sripathi Kodi wrote:
> I have noticed that exit() in stdlib/exit.c is not thread-safe. I have a 
> testcase that creates 10 threads, and they all call 'exit' almost 
> immediately. I saw that multiple threads walk the __exit_funcs link list 
> simultaneously and remove it's elements, but the list has no locks to 
> protect it. Eventually one of the threads receives a SEGV. I have seen 
> this problem only on SMP, but I think it could happen on uniprocessor 
> machine as well. My testcase gets a SEGV 3-5 times out of 10.
> 
> I have noticed that there are no restrictions in POSIX on calls to 
> exit() based on threads, hence I think we need to take care of this 
> situation.  I am attaching a testcase that demonstrates the problem. I 
> am also attaching a patch that adds a lock to be held while walking the 
> __exit_funcs link list. Please let me know if the patch is alright.

There are restrictions in both POSIX and ISO C99.
http://www.opengroup.org/onlinepubs/009695399/functions/exit.html :
If exit() is called more than once, the behavior is undefined.
ISO C99, 7.20.4.3:
If more than one call to the exit function is executed by a program, the
behavior is undefined.

Your program calls exit more than once, therefore its behaviour is
undefined.  A segfault is a conforming implementation of that undefined
behaviour.

	Jakub



More information about the Libc-alpha mailing list