[M120][WRTjs] Fix double dbus close issue 20/305420/2
authorDongHyun Song <dh81.song@samsung.com>
Wed, 3 Jan 2024 02:21:06 +0000 (11:21 +0900)
committerDongHyun Song <dh81.song@samsung.com>
Fri, 2 Feb 2024 05:57:37 +0000 (05:57 +0000)
Change the way to end dbus listening to fix double calls of
dbus_connection_close()

++
wrt-service application handling is not sensitive for
launch performance, so 500ms interval polling looks proper.
In fact, uv-task runner is polling as 500ms either.

Reference:
https://review.tizen.org/gerrit/#/c/platform/framework/web/chromium-efl/+/303807/

Change-Id: Icfcfc70a84333d3a8b8f1e0805bafee61b265bc8
Signed-off-by: DongHyun Song <dh81.song@samsung.com>
(cherry picked from commit d5b98c76536c01ba00086727620dbf71fcd331b0)
(cherry picked from commit 329f0ee54feb80e6c7f2af3efefb23f5a1f9f54b)

wrt/src/browser/wrt_ipc.cc
wrt/src/browser/wrt_ipc.h

index 245b840..d852218 100755 (executable)
@@ -11,16 +11,19 @@ namespace wrt {
 #define WRT_DBUS_NAME "org.tizen.wrt"
 #define WRT_DBUS_PATH "/org/tizen/wrt"
 
-InterProcessCommunication::Message::Message(DBusConnection* connection, DBusMessage* message)
-  : connection_(connection), message_(message) {}
+InterProcessCommunication::Message::Message(DBusConnection* connection,
+                                            DBusMessage* message,
+                                            InterProcessCommunication* ipc)
+    : connection_(connection), message_(message), ipc_(ipc) {}
 
 const char* InterProcessCommunication::Message::Sender() const {
   return dbus_message_get_sender(message_);
 }
 
 void InterProcessCommunication::Message::CloseDBusConnection() const {
-  if (connection_) {
-    dbus_connection_close(connection_);
+  LOG(INFO) << "Listen() will be ended and close dbus connection.";
+  if (ipc_) {
+    ipc_->will_close_ = true;
   }
 }
 
@@ -59,14 +62,20 @@ InterProcessCommunication::InterProcessCommunication(bool is_daemon) {
   }
 }
 
-InterProcessCommunication::~InterProcessCommunication() {
+void InterProcessCommunication::CloseDbus() {
   if (connection_) {
+    if (!message_handlers_.empty())
+      dbus_connection_remove_filter(connection_, OnMessage, this);
     dbus_connection_close(connection_);
     dbus_connection_unref(connection_);
     connection_ = nullptr;
   }
 }
 
+InterProcessCommunication::~InterProcessCommunication() {
+  CloseDbus();
+}
+
 // static
 DBusHandlerResult InterProcessCommunication::OnMessage(DBusConnection* connection, DBusMessage* message, void* user_data) {
   int type = dbus_message_get_type(message);
@@ -96,7 +105,7 @@ DBusHandlerResult InterProcessCommunication::OnMessage(DBusConnection* connectio
     }
 
     auto& handler = iterator->second;
-    handler(member.c_str(), argument, Message(ipc->connection_, message));
+    handler(member.c_str(), argument, Message(ipc->connection_, message, ipc));
   }
 
   return DBUS_HANDLER_RESULT_HANDLED;
@@ -117,9 +126,11 @@ void InterProcessCommunication::SetExitOnDisconnect(bool exit_on_disconnect) {
 }
 
 void InterProcessCommunication::Listen() {
-  while (dbus_connection_read_write_dispatch(connection_, -1)) {
-    ; // empty loop body
+  while (dbus_connection_read_write_dispatch(connection_, 500)) {
+    if (will_close_)
+      break;
   }
+  CloseDbus();
 }
 
 DBusMessage* InterProcessCommunication::InternalSendMessage(const char* receiver, const char* type, const char* argument, bool need_reply) {
index 3cb5f4e..3dcf8bc 100755 (executable)
@@ -19,7 +19,9 @@ class InterProcessCommunication {
 
   class Message {
    public:
-    Message(DBusConnection* connection, DBusMessage* message);
+    Message(DBusConnection* connection,
+            DBusMessage* message,
+            InterProcessCommunication* ipc);
 
     const char* Sender() const;
     void CloseDBusConnection() const;
@@ -28,6 +30,7 @@ class InterProcessCommunication {
    private:
     DBusConnection* connection_;
     DBusMessage* message_;
+    InterProcessCommunication* ipc_ = nullptr;
   };
 
   typedef std::function<void(const char* type, const char* argument, const Message& message)> MessageHandler;
@@ -35,6 +38,7 @@ class InterProcessCommunication {
   void AddMessageHandler(const char* type, MessageHandler handler);
   void SetExitOnDisconnect(bool exit_on_disconnect);
   void Listen();
+  void CloseDbus();
 
   bool HasDbusOwner(const char* dbus_name);
   bool SendMessage(const char* type, const char* argument);
@@ -49,6 +53,7 @@ class InterProcessCommunication {
 
   DBusConnection* connection_;
   std::map<const std::string, MessageHandler> message_handlers_;
+  bool will_close_ = false;
 };
 
 }  // namespace wrt