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

This is a discussion on Calling close(sock) in one thread doesn't make recv(sock,...) return within the Linux Networking forums, part of the Linux Forums category; Hi, I am running a library on Red Hat 8.0. It has 2 threads. In one thread I do ...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-01-2004
Boris Bak
 
Posts: n/a
Default Calling close(sock) in one thread doesn't make recv(sock,...) return

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
  #2 (permalink)  
Old 02-01-2004
Cameron Kerr
 
Posts: n/a
Default Re: Calling close(sock) in one thread doesn't make recv(sock,...) return

Boris Bak <boris.bak@ni.com> 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
cameron.kerr@paradise.net.nz : http://nzgeeks.org/cameron/
Empowered by Perl!
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:50 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0