From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8286 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Thread hangs up when calling exit() Date: Wed, 12 Aug 2015 10:04:27 -0400 Message-ID: <20150812140427.GB31018@brightrain.aerifal.cx> References: <55CB4BD4.5050200@ndmsystems.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1439388284 1723 80.91.229.3 (12 Aug 2015 14:04:44 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Aug 2015 14:04:44 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8298-gllmg-musl=m.gmane.org@lists.openwall.com Wed Aug 12 16:04:44 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1ZPWe3-0004bB-PO for gllmg-musl@m.gmane.org; Wed, 12 Aug 2015 16:04:43 +0200 Original-Received: (qmail 9367 invoked by uid 550); 12 Aug 2015 14:04:41 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 9345 invoked from network); 12 Aug 2015 14:04:40 -0000 Content-Disposition: inline In-Reply-To: <55CB4BD4.5050200@ndmsystems.com> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:8286 Archived-At: 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 > #include > #include > #include > #include > > 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