linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "David Schwartz" <davids@webmaster.com>
To: <eg_nth@sinocdn.com>, <linux-kernel@vger.kernel.org>
Cc: <dicky@sinocdn.com>
Subject: RE: CLOSE_WAIT bug?
Date: Tue, 12 Jun 2001 19:07:09 -0700	[thread overview]
Message-ID: <NCBBLIEPOCNJOAEKBEAKMEAEPNAA.davids@webmaster.com> (raw)
In-Reply-To: <Pine.LNX.4.21.0106130037330.10007-200000@sinocdn.com>


> One server socket created and listening, blocking on select(),
> once a client connect to that port, there is another thread in server
> side issues a close() to the new connection.
> After the client close the connection. The connection in server side will
> stuck on CLOSE_WAIT forever until the program being killed.

	This isn't something you should ever do. There is no way the kernel can
guarantee a sane reaction to this since the 'close' could occur _before_ you
even enter 'select'.

	There is no atomic 'release mutex and select' function so you can never
know for sure whether the 'close' will occur before or after the other
thread enters 'select'. There's also the possibility that another thread
will open a new connection onto the same descriptor before the thread
blocked in 'select' gets a chance to notice that the descriptor is being
closed.

	It's also not clear what the 'close' does in this case. An attempt to close
a descriptor is not supposed to close the underlying connection unless it
closes the last reference. It's not clear whether 'select' represents a
reference or not, and it's not clear what should happen if the descriptor
table changes before the 'select' thread gets woken up even if the 'close'
call schedules it.

	One can argue that 'select' should return because a 'read' or 'write' to
the connection wouldn't block. But that's only true after 'select' returns.
While the endpoint is in use (and 'select' is using it), 'close' shouldn't
necessarily close the underlying connection. So this argument require
bootstrapping.

	For TCP, use 'shutdown'. Don't 'close' the descriptor until you are sure no
thread is using it. This is as serious an error as 'free'ing memory that
another thread is using.

	So your code is buggy. So long as the kernel doesn't lose track of the
resources entirely, it's behavior is (at least to me) acceptable. In fact, I
wish it would punish errors like this more severely, as this would reduce
the amount of code out there that has them.

	DS


  reply	other threads:[~2001-06-13  2:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-12 17:30 CLOSE_WAIT bug? eg_nth
2001-06-13  2:07 ` David Schwartz [this message]
     [not found] <Pine.LNX.4.21.0106130037330.10007-200000@sinocdn.com.suse.lists.linux.kernel>
2001-06-13  8:55 ` Andi Kleen
2002-04-01  9:59 Beng Asuncion
     [not found] <3CA82F7F.312547B8@globalsources.com.suse.lists.linux.kernel>
2002-04-01 17:05 ` Andi Kleen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=NCBBLIEPOCNJOAEKBEAKMEAEPNAA.davids@webmaster.com \
    --to=davids@webmaster.com \
    --cc=dicky@sinocdn.com \
    --cc=eg_nth@sinocdn.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).