misc/66629: socket connecting with itself (race condition?)

Fabien MENEMENLIS fabien at iliad.fr
Fri May 14 03:50:28 PDT 2004


>Number:         66629
>Category:       misc
>Synopsis:       socket connecting with itself (race condition?)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 14 03:50:24 PDT 2004
>Closed-Date:
>Last-Modified:
>Originator:     Fabien MENEMENLIS
>Release:        4.9-STABLE
>Organization:
Iliad
>Environment:
FreeBSD huge.dir.com 4.9-STABLE FreeBSD 4.9-STABLE #2: Fri Mar  5 15:58:16 CET 2004     fabien at huge.iliad.fr:/usr/obj/usr/src/sys/DUAL  i386

>Description:
When connecting to a closed local port in PF_INET space with many clients, it happens once in a while that connect() succeeds. The output of netstat -A for the given connection is then:
ea2c0e40 tcp4       0      0  localhost.4555     localhost.4555     ESTABLISHED

I've not been able to reproduce the problem on a 5.2.1 environment.
>How-To-Repeat:
I know I shouldn't submit code here but it's pretty simple. For better readability see http://dir.com/testsock.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>


#define MAXFORK 100


int sockconnect(char *nomserv, int noport)
{
  struct sockaddr_in serv;
  struct hostent *host;
  int sock;


  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return(-1);

  if ((host = gethostbyname(nomserv)) == NULL)
    return(-1);
  serv.sin_family = AF_INET;
  serv.sin_port = htons(noport);
  memcpy(&serv.sin_addr, host->h_addr, host->h_length);

  if (connect(sock, (struct sockaddr *)&serv,
	      sizeof(struct sockaddr_in)) == -1) {
    close(sock);
    return(-1);
  }

  return(sock);
}


int main(void)
{
  int i;
  int s;


  for (i = 0; i < MAXFORK; i++) {
    if (fork() == 0) {
      for (;;) {
	s = sockconnect("localhost", 4555);
	if (s != -1) {
	  printf("connected... (s=%d)\n", s);
	}
	sleep(1);
      }
    }
  }
}

>Fix:
      
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list