Networking Forums

Networking Forums > Computer Networking > Linux Networking > Calling close(sock) in one thread doesn't make recv(sock,...) return

Reply
Thread Tools Display Modes

Calling close(sock) in one thread doesn't make recv(sock,...) return

 
 
Boris Bak
Guest
Posts: n/a

 
      01-31-2004, 10:03 PM
Hi,

I am running a library on Red Hat 8.0.

It has 2 threads. In one thread I do recv(sock,...), which blocks until
data is received. When application that called the library exits, the
library executes clean-up code as part of which it calls close(sock) on
the same socket that recv(sock,...) was issued before. And then does
pthread_join() to wait for the reading thread to finish

What I notice is recv(sock,...) never returns, which makes my join()
hang. If I press Ctrl-C, it seems to terminate the recv and allows the
clean-up code to finish normally.

Does anybody know a way to terminate a recv(sock,...), besides close()?
And why does the close() don't do the job in the first place?

Thanks for you help!

boris

 
Reply With Quote
 
 
 
 
Cameron Kerr
Guest
Posts: n/a

 
      01-31-2004, 11:52 PM
Boris Bak <(E-Mail Removed)> wrote:
> Hi,
>
> I am running a library on Red Hat 8.0.


You're running an unsupported version of RH, you should upgrade.

> It has 2 threads. In one thread I do recv(sock,...), which blocks until
> data is received. When application that called the library exits, the
> library executes clean-up code as part of which it calls close(sock) on
> the same socket that recv(sock,...) was issued before. And then does
> pthread_join() to wait for the reading thread to finish
>
> What I notice is recv(sock,...) never returns, which makes my join()
> hang. If I press Ctrl-C, it seems to terminate the recv and allows the
> clean-up code to finish normally.
>
> Does anybody know a way to terminate a recv(sock,...), besides close()?
> And why does the close() don't do the job in the first place?


A couple of suggestions.

1) Just before you all close(), send yourself a signal that you have set
an empty signal handler to. That function doesn't need to do anything
but return. The side effect of that is that receiving signals will
interrupt signal calls, causing recv to return with -1, and errno being
EINTR. Close should then return pretty much immediately.

2) Use select() with a timeout to prevent recv() blocking infinately. Then
you just run select() in a loop, reading when you don't timeout and
there is something readable. This will cause a certain amount of lag
when you shutdown the application however (depending on the select()
timeout.) You would weigh this against the everhead caused by repeated
polling. A 1 second timeout should be plenty.

All in all, I would suggest using select() over the signal method, its
cleaner, and there are fewer complications when you don't mix signals
and threads.

You may get better advice on programming in
comp.os.linux.development.apps or comp.unix.programmer

--
Cameron Kerr
(E-Mail Removed) : http://nzgeeks.org/cameron/
Empowered by Perl!
 
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows 2003 server doesn't close TCP connection how can I prevent warning mesage Windows Networking 0 10-03-2006 03:41 PM
recv() problem, need help!! michal.shmueli@gmail.com Linux Networking 4 04-10-2006 04:33 PM
IPW2200 doesn't work - make from sources problems lunak Linux Networking 2 03-30-2006 09:22 AM
Compile the 'sock' program in the book 'TCP/IP Illustrated Vol.1' Steven Woody Linux Networking 2 11-21-2005 08:16 AM
Dante as a sock server: HELP! Stef Linux Networking 0 08-07-2004 02:28 PM



1 2 3 4 5 6 7 8 9 10 11