From: Nick Pelly <npelly@google.com>
To: linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org,
davem@davemloft.net
Subject: Re: Expected behavior of shutdown() in multi-threaded socket programming
Date: Thu, 11 Jun 2009 17:24:21 -0700 [thread overview]
Message-ID: <35c90d960906111724w4794b40aw1c51d64586ecaeb6@mail.gmail.com> (raw)
In-Reply-To: <35c90d960905191429x1c3b6104qca2a672d50f0364c@mail.gmail.com>
Any comments on this one? I would like to correct the behavior of
shutdown() on AF_BLUETOOTH sockets, but I have been advised by Marcel
Holtmann that we need to agree on the correct behavior first.
How should shutdown() behave when other threads are blocked on the same socket?
More detail in the original mail below.
Thanks,
Nick
On Tue, May 19, 2009 at 2:29 PM, Nick Pelly<npelly@google.com> wrote:
> Hi,
>
> I am interested in the expected behavior of shutdown() on a socket
> that is also blocked on connect(), accept(), read(), write(), poll()
> or select() in another thread.
>
> For example:
>
> THREAD 1
> fd = socket()
> listen(fd)
> bind(fd)
> accept(fd) <--- blocks
>
> THREAD 2
> shutdown(fd) <--- what is meant to happen to accept() in thread 1?
>
> If thread 2 is run after thread 1, what should happen to the blocked
> accept() call when shutdown() is called in thread 2?
>
>
> My observations are that
> TCP sockets: accept() immediately returns with errno EINVAL
> unix domain sockets: accept() immediately returns with return errno EINVAL
> RFCOMM sockets: accept() continues to block
> L2CAP sockets: accept() continues to block
>
> I tested on 2.6.18, 2.6.28 and 2.6.29 and results were the same.
>
> Included is a sample program sock_shutdown_test.c that can easily
> exhibit this behavior. For example
> sock_shutdown_test unix accept_shutdown # accept() returns
> sock_shutdown_test tcp accept_shutdown # accept() returns
> sock_shutdown_test rfcomm accept_shutdown # accept() blocks forever
> sock_shutdown_test l2cap accept_shutdown # accept() blocks forever
>
> I also have similar results for other blocking syscalls such as
> connect(), read(), write(), poll() etc, but the test program is not as
> simple.
>
> So my question is: What is the correct behavior for sockets here?
>
>
> It is desirable for Android that shutdown() to force other threads
> blocked on that socket to return. In fact, if they don't it makes
> multi-threaded socket programming very hard since there is no other
> simple way to abort a blocked I/O operation. We have to resort to
> using poll() in combination with a selfpipe that we can write a byte
> to in order to abort the poll(). But this is quite inefficient as it
> triples the number of fd's needed for every socket not to mention the
> 4k buffer space needed in the kernel for the selfpipe. A global
> selfpipe per process is an improvement but it is quite messy getting
> this correct without race conditions.
>
>
> Input as to the correct behavior is appreciated,
> Nick
>
next prev parent reply other threads:[~2009-06-12 0:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-19 21:29 Expected behavior of shutdown() in multi-threaded socket programming Nick Pelly
2009-06-12 0:24 ` Nick Pelly [this message]
2009-06-12 8:46 ` Iain Hibbert
2009-06-15 21:20 ` Nick Pelly
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=35c90d960906111724w4794b40aw1c51d64586ecaeb6@mail.gmail.com \
--to=npelly@google.com \
--cc=davem@davemloft.net \
--cc=linux-bluetooth@vger.kernel.org \
--cc=netdev@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