Netdev List
 help / color / mirror / Atom feed
From: Kirill Tkhai <ktkhai@virtuozzo.com>
To: Alexander Kurtz <alexander@kurtz.be>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: Expected result when racing listen(2) on two sockets bound to the same address
Date: Wed, 23 May 2018 15:44:22 +0300	[thread overview]
Message-ID: <4887534b-cac3-65b5-8c9f-f736c1e56ba3@virtuozzo.com> (raw)
In-Reply-To: <a3ecf04de4fded7c71284a9695b7146d284d1f22.camel@kurtz.be>

Hi,

On 23.05.2018 14:15, Alexander Kurtz wrote:
> [Please keep me CC'ed; I'm not subscribed to the list]
> 
> Hi!
> 
> The program shown below (also available at [0]) does the following:
> 
>  * Create two sockets
>  * Enable SO_REUSEADDR on both
>  * Bind both sockets to [::1]:12345
>  * Spawn two threads which both call listen(2) on one socket each
>  * Check that at least one thread succeeded
> 
> Unfortunately, when running this program on Linux 4.16, it is sometimes
> possible that neither thread succeeds in calling listen(2):
> 
>     $ uname -a
>     Linux shepard 4.16.0-1-amd64 #1 SMP Debian 4.16.5-1 (2018-04-29) x86_64 GNU/Linux
>     $ time make
>     cc -Wall -Wextra -pedantic -Werror -O3    listenrace.c  -lpthread -o listenrace
>     for i in `seq 10000`; do ./listenrace; done
>     listenrace: listenrace.c:58: main: Assertion `result1 == 0 || result2 == 0' failed.
>     Aborted
>     listenrace: listenrace.c:58: main: Assertion `result1 == 0 || result2 == 0' failed.
>     Aborted
>     listenrace: listenrace.c:58: main: Assertion `result1 == 0 || result2 == 0' failed.
>     Aborted
> 
>     real	0m8.201s
>     user	0m6.801s
>     sys	0m2.141s
>     $ 
> 
> As can be seen, on 3 runs (out of 10000) calling listen(2) failed in
> *both* threads. Is this to be expected (i.e. "don't do this then") or
> could this be some race condition in the Linux kernel?

At the first sight, it is in kernel and is expected:

inet_csk_listen_start()
{
	/* There is race window here: we announce ourselves listening,
	 * but this transition is still not validated by get_port().
	 * It is OK, because this socket enters to hash table only
	 * after validation is complete.
	 */
        inet_sk_state_store(sk, TCP_LISTEN);
        if (!sk->sk_prot->get_port(sk, inet->inet_num)) {

}

CC netdev.

Kirill

           reply	other threads:[~2018-05-23 12:44 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <a3ecf04de4fded7c71284a9695b7146d284d1f22.camel@kurtz.be>]

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=4887534b-cac3-65b5-8c9f-f736c1e56ba3@virtuozzo.com \
    --to=ktkhai@virtuozzo.com \
    --cc=alexander@kurtz.be \
    --cc=linux-kernel@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