Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 12 Aug 2015 10:04:27 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: Thread hangs up when calling exit()

On Wed, Aug 12, 2015 at 04:36:20PM +0300, Eugene wrote:
> Hello,
> 
> I have problem with threads.
> Main thread waits for input through function fgets().
> Other thread calls function exit() and hangs up.
> Main thread continues working.
> 
> Musl version: 1.1.10.
> 
> Example:
> #include <pthread.h>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <unistd.h>
> 
> void *func(void *arg)
> {
>         printf("New thread...\n");
>         sleep(5);
>         exit(EXIT_SUCCESS);
> 
>         return (void *) 0;
> }
> 
> int main(int argc, char *argv[])
> {
>         char buf[100];
>         int res = EXIT_FAILURE, ret;
>         pthread_t tid;
> 
>         ret = pthread_create(&tid, NULL, func, NULL);
>         if (ret) {
>                 fprintf(stderr, "pthread_create: %s\n", strerror(ret));
>                 goto out;
>         }
> 
>         while (fgets(buf, sizeof buf, stdin) != NULL)
>                 printf("buf = %s\n", buf);
> 
>         res = EXIT_SUCCESS;
> out:
>         return res;
> }
> 
> Expected result: program exits.
> Actual results: main thread continues working.
> 
> Correct behavior may be obtained with following patch:
> diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
> index 191b445..71a9677 100644
> --- a/src/stdio/__stdio_exit.c
> +++ b/src/stdio/__stdio_exit.c
> @@ -17,7 +17,7 @@ void __stdio_exit(void)
>  {
>         FILE *f;
>         for (f=*__ofl_lock(); f; f=f->next) close_file(f);
> -       close_file(__stdin_used);
> +       //close_file(__stdin_used);
>         close_file(__stdout_used);
>  }
> 
> Maybe "__stdin_used" must be replaced with "__stderr_used"?
> It's looking strange for me to write and seek stdin in function
> close_file().
> 
> Function fgets() obtains lock via macro FLOCK.
> close_file() in "__stdio_exit.c" tries to obtain the same lock.
> It leads to thread hangup.

As far as I can tell this is the required behavior specified by POSIX.
I raised a related issue with the standards body in 2012 and the
effects on exit were deemed intentional. See:

http://austingroupbugs.net/view.php?id=611

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.