Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slove the coredump for monitor #1804

Merged
merged 1 commit into from
Jul 24, 2023
Merged

fix: slove the coredump for monitor #1804

merged 1 commit into from
Jul 24, 2023

Conversation

Mixficsol
Copy link
Collaborator

@Mixficsol Mixficsol commented Jul 24, 2023

fix #1689

截屏2023-07-24 13 08 27

把原先处于上面的解锁步骤改到了下面,防止多并发情况下对reply消息的组装导致协议返回错误
pika/src/pika_server.cc +1111
截屏2023-07-24 13 10 18

for (const auto& cli : clients) {
cli->WriteResp(msg);
cli->SendReply();
}
lock.unlock(); // SendReply without lock
}

void PikaServer::AddMonitorClient(const std::shared_ptr<PikaClientConn>& client_ptr) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the provided code patch, here's a brief code review:

  1. Potential Bug:
    The lock.unlock() call after sending replies to clients can lead to a race condition. It seems like the intention is to release the lock and allow other threads to acquire it. However, since the loop continues to process clients after unlocking, another thread might gain ownership of the lock and modify shared resources concurrently. Consider moving the lock.unlock() call to the end of the loop or using a different synchronization mechanism to ensure thread safety.

  2. Improvement Suggestion:
    Instead of calling lock.unlock() explicitly, consider using std::lock_guard<std::mutex> to acquire and automatically release the lock. This approach ensures that the lock is always released correctly, even if an exception is thrown within the loop.

Here's an updated version of the code with the suggested improvements:

void PikaServer::AddMonitorMessage(const std::string& monitor_message) {
  std::lock_guard<std::mutex> lock(pika_monitor_clients_mutex_);
  
  for (auto it = pika_monitor_clients_.begin(); it != pika_monitor_clients_.end();) {
    const auto& cli = *it;
    if (!cli->IsPubSub()) {
      it = pika_monitor_clients_.erase(it);
    } else {
      cli->WriteResp(msg);
      cli->SendReply();
      ++it;
    }
  }
}

By using std::lock_guard<std::mutex> and moving the lock outside the loop, the code ensures proper locking and unlocks automatically without the need for manual unlock() calls.

@Mixficsol Mixficsol changed the title fix: slove the cordump for monitor fix: slove the coredump for monitor Jul 24, 2023
@chejinge chejinge merged commit eab8849 into OpenAtomFoundation:unstable Jul 24, 2023
9 checks passed
@Mixficsol Mixficsol deleted the dev branch July 28, 2023 02:26
bigdaronlee163 pushed a commit to bigdaronlee163/pika that referenced this pull request Jun 8, 2024
cheniujh pushed a commit to cheniujh/pika that referenced this pull request Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

monitor 命令异常退出
3 participants