Bug 576551

Summary: close(socket) needed to avoid socket resource leakage
Product: [IoT] 4DIAC Reporter: xin zhao <zhaoxin>
Component: FORTEAssignee: Project Inbox <4diac-inbox>
Status: CLOSED FIXED QA Contact:
Severity: major    
Priority: P3 CC: alois.zoitl, martin.melik
Version: unspecified   
Target Milestone: 2.0.1   
Hardware: PC   
OS: Windows 10   
See Also: https://git.eclipse.org/r/c/4diac/org.eclipse.4diac.forte/+/186579
https://git.eclipse.org/c/4diac/org.eclipse.4diac.forte.git/commit/?id=265fb822d1bf433d1e368e741f1af0dee2356050
Whiteboard:

Description xin zhao CLA 2021-10-11 01:32:27 EDT
We are doing a FBN with a http client and there is FBs to re initialize when the server cannot be connected. (a little bit strange for http connection as the communication failure would release the protocol stack)

After long time test, we found that the socket resource are all consumed as no more connection could be initiated for all the applications in windows 10.

I dig a litte bit into the code and I think there should be closeSocket(nSocket) in the below code snippet.

Win32SocketInterface::TSocketDescriptor CWin32SocketInterface::openTCPClientConnection(char *pa_acIPAddr, unsigned short pa_nPort){
.......
    if(SOCKET_ERROR == connect(nSocket, (struct sockaddr*) &stSockAddr, sizeof(struct sockaddr))) {
      int nLastError = WSAGetLastError();
      LPSTR pacErrorMessage = getErrorMessage(nLastError);
      DEVLOG_ERROR("CWin32SocketInterface: connect() failed: %d - %s\n", nLastError, pacErrorMessage);
      LocalFree(pacErrorMessage);
      closeSocket(nSocket);
    }
    else{
      nRetVal = nSocket;
    }
.......
Comment 1 xin zhao CLA 2021-10-11 01:33:14 EDT
besides, I checked the couterpart of bsd socket realization and there is close(socket) at the similar postion.
Comment 2 Martin Melik-Merkumians CLA 2021-10-11 06:37:57 EDT
Hi.

Good spot, the socket should be closed there also.
One comment, for WinSock the function to close a socket is closesocket (all lower case) 

https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-closesocket

Best regards,
Martin
Comment 3 xin zhao CLA 2021-10-11 06:51:40 EDT
yes, (In reply to Martin Melik-Merkumians from comment #2)
> Hi.
> 
> Good spot, the socket should be closed there also.
> One comment, for WinSock the function to close a socket is closesocket (all
> lower case) 
> 
> https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-
> closesocket
> 
> Best regards,
> Martin

yes, Martin, thanks for the information. 

Somebody made a function  in win32socketinterf.cpp called closeSocket and inside it calls closesocket. :)
Comment 4 Eclipse Genie CLA 2021-10-18 03:46:17 EDT
New Gerrit change created: https://git.eclipse.org/r/c/4diac/org.eclipse.4diac.forte/+/186579