Change socket mode before watching it 46/309046/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 4 Apr 2024 07:24:32 +0000 (16:24 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 4 Apr 2024 07:24:32 +0000 (16:24 +0900)
Before watching the fd, the fd should be non-blocking mode.
If it's not, it can make blocking the thread.

Change-Id: I46c2f7e094228b239dd193cf2dd04cfe2b926637
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/proxy-internal.cc

index c35ff8c..265a010 100644 (file)
@@ -70,23 +70,33 @@ int SendRequest(ClientSocket* client, const Request& request) {
 }
 
 int ReceiveResponse(ClientSocket* client, Response** response) {
+  int flags = fcntl(client->GetFd(), F_GETFL, 0);
+  fcntl(client->GetFd(), F_SETFL, flags & ~O_NONBLOCK);
+
   size_t size = 0;
   int ret = client->Receive(reinterpret_cast<void*>(&size), sizeof(size));
   if (ret != 0) {
-    _E("Receive() is failed. error(%d)", ret);  // LCOV_EXCL_LINE
-    return -1;  // LCOV_EXCL_LINE
+    // LCOV_EXCL_START
+    _E("Receive() is failed. error(%d)", ret);
+    fcntl(client->GetFd(), F_SETFL, flags);
+    return -1;
+    // LCOV_EXCL_STOP
   }
 
   uint8_t* buf = static_cast<uint8_t*>(malloc(size));
   if (buf == nullptr) {
-    _E("Out of memory");  // LCOV_EXCL_LINE
-    return -1;  // LCOV_EXCL_LINE
+    // LCOV_EXCL_START
+    _E("Out of memory");
+    fcntl(client->GetFd(), F_SETFL, flags);
+    return -1;
+    // LCOV_EXCL_STOP
   }
 
   ret = client->Receive(buf, size);
   if (ret != 0) {
     // LCOV_EXCL_START
     _E("Receive() is failed. error(%d)", ret);
+    fcntl(client->GetFd(), F_SETFL, flags);
     free(buf);
     return -1;
     // LCOV_EXCL_STOP
@@ -95,10 +105,14 @@ int ReceiveResponse(ClientSocket* client, Response** response) {
   tizen_base::Parcel parcel(buf, size, false);
   *response = new (std::nothrow) Response();
   if (*response == nullptr) {
-    _E("Out of memory");  // LCOV_EXCL_LINE
-    return -1;  // LCOV_EXCL_LINE
+    // LCOV_EXCL_START
+    _E("Out of memory");
+    fcntl(client->GetFd(), F_SETFL, flags);
+    return -1;
+    // LCOV_EXCL_STOP
   }
 
+  fcntl(client->GetFd(), F_SETFL, flags);
   parcel.ReadParcelable(*response);
   return 0;
 }
@@ -155,6 +169,7 @@ int Proxy::Connect(bool sync) {
     main_client_->SetNonblock();
     fds_[0] = main_client_->RemoveFd();
   } else {
+    main_client_->SetNonblock();
     ret = main_client_->Watch();
     if (ret != 0)
       return RPC_PORT_ERROR_IO_ERROR;  // LCOV_EXCL_LINE
@@ -185,6 +200,7 @@ int Proxy::Connect(bool sync) {
     delegate_client_->SetNonblock();
     fds_[1] = delegate_client_->RemoveFd();
   } else {
+    delegate_client_->SetNonblock();
     ret = delegate_client_->Watch();
     if (ret != 0)
       return RPC_PORT_ERROR_IO_ERROR;  // LCOV_EXCL_LINE