songgaoye commented on code in PR #3650:
URL: https://github.com/apache/brpc/pull/3650#discussion_r4362248583


##########
src/brpc/selective_channel.cpp:
##########
@@ -299,17 +309,31 @@ int ChannelBalancer::CheckHealth() {
 
 void ChannelBalancer::Describe(std::ostream& os,
                                const DescribeOptions& options) {
-    BAIDU_SCOPED_LOCK(_mutex);
     if (!options.verbose) {
+        BAIDU_SCOPED_LOCK(_mutex);
         os << _chan_map.size();
         return;
     }
-    for (ChannelToIdMap::const_iterator it = _chan_map.begin();
-         it != _chan_map.end(); ++it) {
-        if (it != _chan_map.begin()) {
+    std::vector<SocketId> ids;
+    {
+        BAIDU_SCOPED_LOCK(_mutex);
+        ids.reserve(_chan_map.size());
+        for (ChannelToIdMap::const_iterator it = _chan_map.begin();
+             it != _chan_map.end(); ++it) {
+            ids.push_back(it->second->id());
+        }
+    }
+    bool first = true;
+    for (size_t i = 0; i < ids.size(); ++i) {
+        SocketUniquePtr ptr;
+        if (Socket::AddressFailedAsWell(ids[i], &ptr) < 0) {
+            continue;
+        }

Review Comment:
   This is safe because the IDs come only from _chan_map, where every Socket is 
created with a SubChannel as its user data. AddressFailedAsWell() validates the 
versioned ID and SocketUniquePtr keeps the Socket alive. user() is cleared only 
during recycling, which cannot happen while ptr holds a reference.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to