[IOT-2515] File descriptor leak 99/21899/8
authorVeeraj Khokale <veeraj.sk@samsung.com>
Mon, 14 Aug 2017 10:11:08 +0000 (15:41 +0530)
committerAshok Babu Channa <ashok.channa@samsung.com>
Fri, 8 Sep 2017 11:46:55 +0000 (11:46 +0000)
Repeatedly calling OCInit2() followed by OCStop() causes
the process to run out of file descriptors. To fix this
the following changes are made to IP adapter:

1. Close the ip global socket FDs before reinitializing them.
2. Close the shutdownFds[0] FD when receive thread is stopped.

Change-Id: I1e0f336cd3d6de2297aa3421b47c8b8d810f82fb
Signed-off-by: Veeraj Khokale <veeraj.sk@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21899
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Todd Malsbary <todd.malsbary@intel.com>
Reviewed-by: koushik girijala <g.koushik@samsung.com>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c

index 9ab4a3e..603a855 100644 (file)
@@ -388,6 +388,9 @@ CAResult_t CAInitializeIP(CARegisterConnectivityCallback registerCallback,
 
 CAResult_t CAStartIP()
 {
+    //Initializing the Globals
+    CAInitializeIPGlobals();
+
     // Specific the port number received from application.
     caglobals.ip.u6.port  = caglobals.ports.udp.u6;
     caglobals.ip.u6s.port = caglobals.ports.udp.u6s;
@@ -530,8 +533,6 @@ CAResult_t CAStopIP()
 
     CAIPStopNetworkMonitor(CA_ADAPTER_IP);
     CAIPStopServer();
-    //Re-initializing the Globals to start them again
-    CAInitializeIPGlobals();
 
     return CA_STATUS_OK;
 }
@@ -545,7 +546,6 @@ void CATerminateIP()
     CAIPSetPacketReceiveCallback(NULL);
 
 #ifndef SINGLE_THREAD
-    CADeInitializeIPGlobals();
     CAIPDeinitializeQueueHandles();
 #endif
 
index 565d888..997775e 100644 (file)
@@ -156,6 +156,18 @@ static void CAEventReturned(CASocketFd_t socket);
 
 static CAResult_t CAReceiveMessage(CASocketFd_t fd, CATransportFlags_t flags);
 
+static void CACloseFDs()
+{
+#if !defined(WSA_WAIT_EVENT_0)
+    if (caglobals.ip.shutdownFds[0] != -1)
+    {
+        close(caglobals.ip.shutdownFds[0]);
+        caglobals.ip.shutdownFds[0] = -1;
+    }
+#endif
+    CADeInitializeIPGlobals();
+}
+
 static void CAReceiveHandler(void *data)
 {
     (void)data;
@@ -164,6 +176,7 @@ static void CAReceiveHandler(void *data)
     {
         CAFindReadyMessage();
     }
+    CACloseFDs();
 }
 
 #define CLOSE_SOCKET(TYPE) \
@@ -1010,13 +1023,13 @@ CAResult_t CAIPStartServer(const ca_thread_pool_t threadPool)
 
 void CAIPStopServer()
 {
-    caglobals.ip.started = false;
     caglobals.ip.terminate = true;
 
 #if !defined(WSA_WAIT_EVENT_0)
     if (caglobals.ip.shutdownFds[1] != -1)
     {
         close(caglobals.ip.shutdownFds[1]);
+        caglobals.ip.shutdownFds[1] = -1;
         // receive thread will stop immediately
     }
     else
@@ -1030,6 +1043,12 @@ void CAIPStopServer()
         OIC_LOG_V(DEBUG, TAG, "set shutdown event failed: %d", WSAGetLastError());
     }
 #endif
+
+    if (!caglobals.ip.started)
+    { // Close fd's since receive handler was not started
+        CACloseFDs();
+    }
+    caglobals.ip.started = false;
 }
 
 void CAWakeUpForChange()