Bug in HTTCP.c

Hi there,

I found a serious bug in HTTCP.c for Win32 systems.

In HTDoConnect(), the connect command is done before the WSAAsyncSelect() that is called when HTHost_register(host, net, HTEvent_CONNECT); is executed. Although that is in line with the WinSock2 and Microsoft documentation, it does _not_ work all the time. I have done extensive tests on Win2000 and Win 4.0 SP5. In very rare cases, the connect is finished between the connect() command itself and the WSAAsyncSelect(). In this unlikely case, WinSock does not (always) send the FD_CONNECT message. As a result, when using the Async mode, the event loop hangs because there is no timeout procedure registered for FD_CONNECT.

Thus, I moved the HTHost_register in front of the connect command in HTDoConnect():

case TCP_NEED_CONNECT:

#ifdef _WINSOCKAPI_
     HTHost_register(host, net, HTEvent_CONNECT);
#endif
     status = connect(HTChannel_socket(host->channel), (struct sockaddr *) &host->sock_addr,
        sizeof(host->sock_addr));

In Addition, I had to remove the same command in HTDoConnect() in case of WSA_WOULDBLOCK:

  if (NETCALL_WOULDBLOCK(socerrno))
  {
      HTTRACE(PROT_TRACE, "HTDoConnect. WOULD BLOCK `%s'\n" _ hostname);
#ifndef _WINSOCKAPI_
      HTHost_register(host, net, HTEvent_CONNECT);
#endif
   return HT_WOULD_BLOCK;
  }

After this change, I had never a hanging event loop again, even when looping for a few 1000 requests.

best regards,

Jens

Received on Friday, 7 July 2000 13:44:19 UTC