Best way to interrupt socket send()?

Best way to interrupt socket send()?

Post by Fredrik L?nnergre » Tue, 25 Jun 2002 16:12:38



Hi all,

Like the title says, I'm trying to figure out the cleanest way to interrupt
a blocking socket send() (in case it takes too long, for example). Can you
simply do a close() on the descriptor in another thread and be done with it?

TIA,
  Fredrik

 
 
 

Best way to interrupt socket send()?

Post by David Schwart » Tue, 25 Jun 2002 17:47:54



> Like the title says, I'm trying to figure out the cleanest way to interrupt
> a blocking socket send() (in case it takes too long, for example). Can you
> simply do a close() on the descriptor in another thread and be done with it?

        There are unavoidable race conditions involved with issuing a 'close'
in one thread when another thread might or might not be in a blocking
'send'. Your question, by the way, starts from a false premise. There is
no way you could possibly know that the other thread was in a blocking
send, since there is no atomic 'set this variable and block in send'
function.

        This is important because the thread could always be *about* to call
'send' when you close the socket. In this case, if another thread calls
'socket' and gets the same descriptor, original thread will wind up
calling 'send' on a different connection entirely! Ouch.

        So, don't do that.

        The easiest way to solve this problem is to have avoided it in the
first place. Don't do a blocking 'send' unless you want to block until
the 'send' can complete.

        DS

 
 
 

Best way to interrupt socket send()?

Post by Fredrik L?nnergre » Tue, 25 Jun 2002 20:03:15




> > Like the title says, I'm trying to figure out the cleanest way to
interrupt
> > a blocking socket send() (in case it takes too long, for example). Can
you
> > simply do a close() on the descriptor in another thread and be done with

it?
*snip*

Quote:> Don't do a blocking 'send' unless you want to block until
> the 'send' can complete.

Thanks for the answer.
I suppose I'll have to do a timeout of my own with a sleeping for-loop.

/Fredrik

 
 
 

Best way to interrupt socket send()?

Post by Filip Sielimowic » Tue, 25 Jun 2002 21:04:00


Quote:> Hi all,

> Like the title says, I'm trying to figure out the cleanest way to
interrupt
> a blocking socket send() (in case it takes too long, for example). Can you
> simply do a close() on the descriptor in another thread and be done with
it?

> TIA,
>   Fredrik

Use select (or poll) functions instead of waiting on a blocking read.
There you may set some timeout in an easy way.

You may also (no close !!!) send some signal from other thread (SIG_INT ?
SIG_HUP ? SIG_TERM ?
try to find proper one) and read function should break with EINTR return
code. Just ignore the signal.

--
Filip Sielimowicz
http://panda.bg.univ.gda.pl/~sielim
----
eby zrozumie? rekursj, trzeba krok wcze?niej zrozumie? rekursj.

 
 
 

Best way to interrupt socket send()?

Post by David Schwart » Wed, 26 Jun 2002 02:08:40



> Thanks for the answer.
> I suppose I'll have to do a timeout of my own with a sleeping for-loop.

> /Fredrik

        The 'select' and 'poll' functions are your friends. You can either use
a timeout and check a flag on each pass or add another file descriptor
to the select/poll set and signal by making that file descriptor active
(pipes are handy).

        DS

 
 
 

1. What's the best way to send/recvs integer values trought sockets ?

Let's say I have to send integer values and recive those values...
What's the best way ?

(Non working, bad example):

void net_send_int (int value, int socket )
{
???int data;
???data = htons ( value );
???write (socket, &data, sizeof ( data ) );
???return;

int net_recv_int ( int socket )
{
???int data;
???write (socket, &data,/* how do I know how many bytes to read here ?*/ );
???
???return ntohs ( data );

2. Media failure when using RAID

3. Solaris on an 80x86 portable?

4. kcontrol problems

5. Best ways to increase the security of my linux box

6. Hackers Guide

7. Best ways to handle function keys?

8. Q: The Best Ways to Have 4GB Linux Box

9. Best ways to optimise Linux

10. Best ways of searching web pages - wais, swish etc

11. The best ways to connect a Win95 box to a Linux box ???????