public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* Connect hangs for a while before returns -1 with ECONNREFUSED on 3.2 for loopback
@ 2012-02-03  6:25 Yurij M. Plotnikov
  2012-02-03 14:38 ` Eric Dumazet
  0 siblings, 1 reply; 11+ messages in thread
From: Yurij M. Plotnikov @ 2012-02-03  6:25 UTC (permalink / raw)
  To: netdev

[-- Attachment #1: Type: text/plain, Size: 777 bytes --]

On kernel 3.2.0-0.bpo.1-amd64 I see some strange behaviour of connect() 
in case of connection via loopback. Lets see the following steps (there 
are two processes on the host, and the first one with two threads)

Thread1:
1. socket(PF_INET, SOCK_STREAM, 0) ->  3
2. bind(10.27.10.1:26820) ->  0 /* The address is bound to some interface, eth1 */
3. listen(3, 1) ->  0

sleep for a while

Thread2:
4. shutdown(3, SHUT_RD) ->  0

sleep for a while

Another process:
5. socket(PF_INET, SOCK_STREAM, 0) ->  4
6. connect(4, 10.27.10.1:26820)

connect() returns -1 with ECONNREFUSED but after some time. In case of 
two peer hosts connect() returns -1 with ECONNREFUSED almost 
immediately, so does for the other kernel versions.

In attachment c program to reproduce this problem.

[-- Attachment #2: connect_loopback.c --]
[-- Type: text/x-csrc, Size: 1042 bytes --]

#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <errno.h>

main()
{
    int child_sock;
    int sock;
    struct sockaddr_in addr;
    int rc;
    int proc;

    sock = socket(PF_INET, SOCK_STREAM, 0);
    printf("socket() -> %d(%d)\n", sock, errno);

    addr.sin_family = AF_INET;
    addr.sin_port = htons(12345);
    addr.sin_addr.s_addr = inet_addr("10.0.1.1");
    rc = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
    printf("bind() -> %d(%d)\n", rc, errno);

    rc = listen(sock, 1);
    printf("listen() -> %d(%d)\n", rc, errno);

    sleep(1);

    rc = shutdown(sock, SHUT_RD);
    printf("shutdown() -> %d(%d)\n", rc, errno);

    proc = fork();
    if (proc)
    {
        child_sock = socket(PF_INET, SOCK_STREAM, 0);
        printf("Child: socket() -> %d(%d)\n", child_sock, errno);
        rc = connect(child_sock, (struct sockaddr *)&addr, sizeof(addr));
        printf("connect() -> %d(%d)\n", rc, errno);
    }
    else
    {
        printf("Waiting...\n");getchar();
    }

    return 0;
}

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-02-04 21:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-03  6:25 Connect hangs for a while before returns -1 with ECONNREFUSED on 3.2 for loopback Yurij M. Plotnikov
2012-02-03 14:38 ` Eric Dumazet
2012-02-03 15:15   ` Eric Dumazet
2012-02-04 12:26     ` Eric Dumazet
2012-02-04 15:48       ` Julian Anastasov
2012-02-04 16:58         ` Eric Dumazet
2012-02-04 17:39           ` Julian Anastasov
2012-02-04 19:43             ` Eric Dumazet
2012-02-04 20:51               ` Julian Anastasov
2012-02-04 21:26                 ` Eric Dumazet
2012-02-04 20:39       ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox