Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Wed, 12 Aug 2015 16:36:20 +0300
From: Eugene <>
Subject: Thread hangs up when calling exit()


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.

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

void *func(void *arg)
         printf("New thread...\n");

         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;
         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);

Maybe "__stdin_used" must be replaced with "__stderr_used"?
It's looking strange for me to write and seek stdin in function 

Function fgets() obtains lock via macro FLOCK.
close_file() in "__stdio_exit.c" tries to obtain the same lock.
It leads to thread hangup.


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.