This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new b2c9ac49 Fix not end wait ns when ns fails to start (#2162)
b2c9ac49 is described below

commit b2c9ac4943876e99504a20cf5fb7e009d4b72a41
Author: Bright Chen <[email protected]>
AuthorDate: Mon Mar 13 09:56:23 2023 +0800

    Fix not end wait ns when ns fails to start (#2162)
---
 src/brpc/details/naming_service_thread.cpp | 11 +++++++++--
 src/brpc/details/naming_service_thread.h   | 15 ++++++++-------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/src/brpc/details/naming_service_thread.cpp 
b/src/brpc/details/naming_service_thread.cpp
index ecbe9f25..359ef38d 100644
--- a/src/brpc/details/naming_service_thread.cpp
+++ b/src/brpc/details/naming_service_thread.cpp
@@ -293,7 +293,7 @@ int NamingServiceThread::Start(NamingService* 
naming_service,
         int rc = bthread_start_urgent(&_tid, NULL, RunThis, this);
         if (rc) {
             LOG(ERROR) << "Fail to create bthread: " << berror(rc);
-            return -1;
+            return rc;
         }
     }
     return WaitForFirstBatchOfServers();
@@ -315,6 +315,10 @@ int NamingServiceThread::WaitForFirstBatchOfServers() {
     return 0;
 }
 
+void NamingServiceThread::EndWait(int error_code) {
+    _actions.EndWait(error_code);
+}
+
 void NamingServiceThread::ServerNodeWithId2ServerId(
     const std::vector<ServerNodeWithId>& src,
     std::vector<ServerId>* dst, const NamingServiceFilter* filter) {
@@ -469,8 +473,11 @@ int GetNamingServiceThread(
         }
     }
     if (new_thread) {
-        if (nsthread->Start(source_ns->New(), key.protocol, key.service_name, 
options) != 0) {
+        int rc = nsthread->Start(source_ns->New(), key.protocol, 
key.service_name, options);
+        if (rc != 0) {
             LOG(ERROR) << "Fail to start NamingServiceThread";
+            // Wake up those waiting for first batch of servers.
+            nsthread->EndWait(rc);
             std::unique_lock<pthread_mutex_t> mu(g_nsthread_map_mutex);
             g_nsthread_map->erase(key);
             return -1;
diff --git a/src/brpc/details/naming_service_thread.h 
b/src/brpc/details/naming_service_thread.h
index 21eef779..1dcd0f76 100644
--- a/src/brpc/details/naming_service_thread.h
+++ b/src/brpc/details/naming_service_thread.h
@@ -66,11 +66,11 @@ class NamingServiceThread : public SharedObject, public 
Describable {
     };
     class Actions : public NamingServiceActions {
     public:
-        Actions(NamingServiceThread* owner);
-        ~Actions();
-        void AddServers(const std::vector<ServerNode>& servers);
-        void RemoveServers(const std::vector<ServerNode>& servers);
-        void ResetServers(const std::vector<ServerNode>& servers);
+        explicit Actions(NamingServiceThread* owner);
+        ~Actions() override;
+        void AddServers(const std::vector<ServerNode>& servers) override;
+        void RemoveServers(const std::vector<ServerNode>& servers) override;
+        void ResetServers(const std::vector<ServerNode>& servers) override;
         int WaitForFirstBatchOfServers();
         void EndWait(int error_code);
 
@@ -90,19 +90,20 @@ class NamingServiceThread : public SharedObject, public 
Describable {
 
 public:    
     NamingServiceThread();
-    ~NamingServiceThread();
+    ~NamingServiceThread() override;
 
     int Start(NamingService* ns,
               const std::string& protocol,
               const std::string& service_name,
               const GetNamingServiceThreadOptions* options);
     int WaitForFirstBatchOfServers();
+    void EndWait(int error_code);
 
     int AddWatcher(NamingServiceWatcher* w, const NamingServiceFilter* f);
     int AddWatcher(NamingServiceWatcher* w) { return AddWatcher(w, NULL); }
     int RemoveWatcher(NamingServiceWatcher* w);
 
-    void Describe(std::ostream& os, const DescribeOptions&) const;
+    void Describe(std::ostream& os, const DescribeOptions&) const override;
 
 private:
     void Run();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to