mailing list of musl libc
 help / color / mirror / code / Atom feed
* Thread hangs up when calling exit()
@ 2015-08-12 13:36 Eugene
  2015-08-12 14:04 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Eugene @ 2015-08-12 13:36 UTC (permalink / raw)
  To: musl

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.

Eugene





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Thread hangs up when calling exit()
  2015-08-12 13:36 Thread hangs up when calling exit() Eugene
@ 2015-08-12 14:04 ` Rich Felker
  2015-08-12 14:18   ` Jens Gustedt
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Felker @ 2015-08-12 14:04 UTC (permalink / raw)
  To: musl

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Thread hangs up when calling exit()
  2015-08-12 14:04 ` Rich Felker
@ 2015-08-12 14:18   ` Jens Gustedt
  0 siblings, 0 replies; 3+ messages in thread
From: Jens Gustedt @ 2015-08-12 14:18 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1103 bytes --]

Hello,

Am Mittwoch, den 12.08.2015, 10:04 -0400 schrieb Rich Felker:
> 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.
> > ...
> 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

yes, this behavior also seems to be in line with C11 threads and
io.

If you want to exit (and not abort) under such circumstances you could
use the C11 function quick_exit, which musl implements.

Jens

-- 
:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::




[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-08-12 14:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-12 13:36 Thread hangs up when calling exit() Eugene
2015-08-12 14:04 ` Rich Felker
2015-08-12 14:18   ` Jens Gustedt

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).