Re: socket close

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 04 Jul 2007 19:56:29 +1000
skaller <skaller@xxxxxxxxxxxxxxxxxxxxx> wrote:

> I wonder if someone can tell me what Linux does here.
> 
> First: it seems to me Posix specification are such that
> the behaviour on closing a socket is unspecified, except
> if the socket is blocking and SO_LINGER is used to specify
> a timeout. In that case the close blocks until written data
> is sent down the wire or the nominated timeout expires.
> 
> IMHO this specification is unacceptable because it makes
> reliable non-blocking socket operations impossible.
> However I may misunderstand the spec.
> 
> Second: I have observed that in non-blocking mode,
> Linux discards written data without sending it down the wire.
> As above: this seems unacceptable, although it conforms to
> Posix 'in vaccuuo' :)
> 
> Can anyone tell me what Linux *actually* does? 
> 

In my understanding, the behavior at close() time depends if you still have data in receive queue.

If you dont have data wating in receive queue  (but possibly some data in output queue), then stack sends data and FIN, no loss of data.

However, If you have some unread data, then a RST is sent (and data in output queue is lost).
So maybe your app is actually closing a socket with unread data (this would be an application error)

> A related question: is there any reason to suppose closing
> for a different thread to the writing (assuming a proper
> locked exchange of control) would make any difference?
> [We use a separate epoll-based writer thread]

linux is safe on this. you can close() a socket while a send() on this same socket is done by another thread.

But the real close() (fd deallocation, and socket dismantle ) will be delayed at the point the other thread returns to user space. You might try to use shutdown() before close().

> 
> What *should* happen IMHO is that SO_LINGER should cause
> a non-blocking socket close to linger for the timeout
> specified trying to write the data down the wire, after
> which it is free to give up. 

SO_LINGER should be avoided if possible, especially if you use non blocking sockets.
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux