This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: [PATCH] Make exit thread-safe
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Sripathi Kodi <sripathik at in dot ibm dot com>
- Cc: libc-alpha at sources dot redhat dot com
- Date: Thu, 24 Feb 2005 10:56:28 +0100
- Subject: Re: [PATCH] Make exit thread-safe
- References: <421D9C7C.70601@in.ibm.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
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