Fix fd leak 51/201551/3
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 15 Mar 2019 11:02:24 +0000 (20:02 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 18 Mar 2019 01:35:40 +0000 (10:35 +0900)
Change-Id: I0a405972403b36cfb90e66ff73b2f36746c9a672
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/stub-internal.cc
src/stub-internal.h

index 696eb6b..d6c85a0 100644 (file)
@@ -81,6 +81,18 @@ std::shared_ptr<Port> Stub::FindDelegatePort(
   return {};
 }
 
+void Stub::RemoveAcceptedPorts(std::string instance) {
+  auto iter = ports_.begin();
+  while (iter != ports_.end()) {
+    if ((*iter)->GetInstance().compare(instance) == 0) {
+      LOGI("Close: fd(%d)", (*iter)->GetFd());
+      iter = ports_.erase(iter);
+    } else {
+      iter++;
+    }
+  }
+}
+
 gboolean Stub::OnDataReceived(GIOChannel *gio, GIOCondition cond,
                               gpointer data) {
   Stub* stub = static_cast<Stub*>(data);
@@ -92,7 +104,7 @@ gboolean Stub::OnDataReceived(GIOChannel *gio, GIOCondition cond,
       if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
         LOGW("Socket was disconnected from proxy. fd(%d)", fd);
         stub->listener_->OnDisconnected(p->GetId(), p->GetInstance());
-        stub->ports_.remove(p);
+        stub->RemoveAcceptedPorts(p->GetInstance());
 
         if (aul_rpc_port_notify_rpc_finished() != AUL_R_OK)
           LOGW("Failed to notify rpc finished");
@@ -106,7 +118,7 @@ gboolean Stub::OnDataReceived(GIOChannel *gio, GIOCondition cond,
       if (ret != 0) {
         LOGW("Invalid protocol");
         stub->listener_->OnDisconnected(p->GetId(), p->GetInstance());
-        stub->ports_.remove(p);
+        stub->RemoveAcceptedPorts(p->GetInstance());
 
         if (aul_rpc_port_notify_rpc_finished() != AUL_R_OK)
           LOGW("Failed to notify rpc finished");
@@ -134,7 +146,7 @@ gboolean Stub::OnSocketDisconnected(GIOChannel *gio, GIOCondition cond,
         if (aul_rpc_port_notify_rpc_finished() != AUL_R_OK)
           LOGW("Failed to notify rpc finished");
       }
-      stub->ports_.remove(p);
+      stub->RemoveAcceptedPorts(p->GetInstance());
       break;
     }
   }
index e3408f2..cff2e7b 100644 (file)
@@ -81,6 +81,7 @@ class Stub : private FdBroker::IEventListener {
                                        gpointer data);
 
   void OnFdReceived(const std::string& sender, int fds[2]) override;
+  void RemoveAcceptedPorts(std::string instance);
 
  private:
   std::list<std::shared_ptr<AcceptedPort>> ports_;