LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2011, 02:20 AM   #1
zxtole
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Rep: Reputation: Disabled
Unhappy pclose() hangs and never return in multi-threaded program


hi guys,

i'm using popen() and redirection to generate a ".gz" file for ftp download like this:

Code:
FILE* testfd;
testfd = popen("/bin/gzip - -fq > /home/ftp/testfile.gz","w");
...
fprintf(testfd, "blablabla");
...
pclose(testfd);
it just worked fine for a few days, then i found something wield and the program would not response anymore:

Code:
#:/ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
....
root      1065  1064  0 Aug26 ?        00:00:00 myserver
....
root      1077  1065  0 Aug26 ?        00:00:00 sh -c /bin/gzip - -fq > /home/ftp/testfile.gz
root      1078  1077  0 Aug26 ?        00:00:00 /bin/gzip - -fq
....
the file /home/ftp/testfile.gz looks ok but its size is 0.

i try to look up into it and attach to PID 1065 with gdb, the backtrace comes out as below:

Code:
(gdb) bt
#0  0x40109588 in _IO_proc_close () from /lib/libc.so.6
#1  0x40110ec8 in _IO_file_close_it () from /lib/libc.so.6
#2  0x40107860 in fclose () from /lib/libc.so.6
#3  0x000f9a64 in generate_testfile ()
#4  0x00101ddc in handleMsgFromClient ()
#5  0x00102c94 in work_pthreadMain ()
#6  0x40056a74 in pthread_start_thread () from /lib/libpthread.so.0
#7  0x401776c8 in clone () from /lib/libc.so.6
#8  0x401776c8 in clone () from /lib/libc.so.6
Previous frame identical to this frame (corrupt stack?)
(gdb)
btw, "myserver" is a multi-threaded program using pthread library.

i have no idea why fclose() hangs here, and the child process would not exit as before.

anyone have any idea?
 
Old 08-29-2011, 06:25 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd recommend asking the Mods to move this to the Programming forum, using the Report button.
 
1 members found this post helpful.
Old 08-29-2011, 11:47 PM   #3
zxtole
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
I'd recommend asking the Mods to move this to the Programming forum, using the Report button.
thx, chrism01. You mean "Report LQ Bug"? That's the only "report" button i could find on this page....
 
Old 08-30-2011, 08:04 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
When it hangs, please ps the gzip process (using its pid) and post it. Specifically, the "STAT" column is of interest.
Kevin Barry
 
Old 08-30-2011, 07:54 PM   #5
zxtole
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
When it hangs, please ps the gzip process (using its pid) and post it. Specifically, the "STAT" column is of interest.
Kevin Barry
The "STAT" column is "S" for these two "gzip" processes both, i've looked into it already. They were not zombies, definitely.

It seems that the "gzip" command was still waiting for an "EOF" from standard input, but "pclose()" hung during "fclose()" call and never returned.

Is that true?
 
Old 08-30-2011, 08:54 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by zxtole View Post
The "STAT" column is "S" for these two "gzip" processes both, i've looked into it already. They were not zombies, definitely.

It seems that the "gzip" command was still waiting for an "EOF" from standard input, but "pclose()" hung during "fclose()" call and never returned.

Is that true?
Yes, this is the interpretation I would have given.

I would strace the server program and pipe the output through egrep 'pipe|fork|close|wait' (remember strace writes to stderr). With a little work you should be able to make sure the server-side descriptor created by popen (with pipe) is closed (with pclose) before it waits for the shell process to exit. You might also make sure the other end of the pipe is closed by the server after forking.

If you can, run the server with strace just enough for the hang to happen. If it isn't too lengthy, post the strace output filtered through the egrep command above.
Kevin Barry
 
1 members found this post helpful.
Old 08-30-2011, 09:56 PM   #7
zxtole
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
ahh, thank you, ta0kira. That's a good advice, i'll have a try asap.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-threaded rsync ? permalac Linux - Newbie 6 10-27-2010 06:17 PM
[SOLVED] Multi threaded Wine txm123 Linux - Newbie 2 08-14-2010 01:56 PM
apache: running multi-threaded or multi fork? Swakoo Linux - General 1 03-20-2008 07:18 AM
How to debug a multi-threaded program? nalsrayatko Programming 4 11-29-2007 01:57 PM
Multi-Threaded C pragti Programming 1 06-01-2004 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration