Copilot commented on code in PR #3650:
URL: https://github.com/apache/brpc/pull/3650#discussion_r4366207231
##########
src/brpc/selective_channel.cpp:
##########
@@ -286,11 +287,20 @@ inline int ChannelBalancer::SelectChannel(const
LoadBalancer::SelectIn& in,
}
int ChannelBalancer::CheckHealth() {
- BAIDU_SCOPED_LOCK(_mutex);
- for (ChannelToIdMap::const_iterator it = _chan_map.begin();
- it != _chan_map.end(); ++it) {
- if (!it->second->Failed() &&
- it->first->CheckHealth() == 0) {
+ 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());
+ }
+ }
+ for (size_t i = 0; i < ids.size(); ++i) {
+ SocketUniquePtr ptr;
+ if (Socket::AddressFailedAsWell(ids[i], &ptr) >= 0 &&
+ !ptr->Failed() &&
+ static_cast<SubChannel*>(ptr->user())->chan->CheckHealth() == 0) {
Review Comment:
For `DOESNT_OWN_CHANNEL` registrations, `SocketUniquePtr` retains the socket
and its `SubChannel` wrapper, but not the externally owned channel. Removal can
now return during `CheckHealth()`, allowing the owner to delete the channel
while it is still being accessed; the same issue affects verbose `Describe()`
at line 335. Preserve synchronization with removal for non-owning
registrations, or add a reader-lifetime mechanism that protects the channel
itself, with a regression test for concurrent removal and deletion.
--
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]