CyberRT(apollo) IPC(shm)通信包重复/丢包 bug 及解决方案

这里写自定义目录标题

问题

前几年阅读了下CyberRT的源码,发现了一个进程间shm通信线程安全的问题,不过最后也没怎么用CyberRT就是了;

前段时间,apollo 发布10.0,看了下几年前的严重bug还是存在ORZ。

截止2025/02/20 Commit c48541b 仍未修复。

路径 apollo/cyber/transport/shm/condition_notifier.cc

conditon_notifier ,在CyberRT里面shm通信机制下使用,用于通知reader 监听新消息,以及writer 产生新消息进行notify;

关键代码如下

cpp 复制代码
 struct Indicator {
    std::atomic<uint64_t> next_seq = {0};
    ReadableInfo infos[kBufLength];
    uint64_t seqs[kBufLength] = {0};
  };
  
bool ConditionNotifier::Notify(const ReadableInfo& info) {
  if (is_shutdown_.load()) {
    ADEBUG << "notifier is shutdown.";
    return false;
  }

  uint64_t seq = indicator_->next_seq.fetch_add(1);
  uint64_t idx = seq % kBufLength;
  indicator_->infos[idx] = info; // BUG
  indicator_->seqs[idx] = seq; // BUG

  return true;
}

bool ConditionNotifier::Listen(int timeout_ms, ReadableInfo* info) {
  if (info == nullptr) {
    AERROR << "info nullptr.";
    return false;
  }

  if (is_shutdown_.load()) {
    ADEBUG << "notifier is shutdown.";
    return false;
  }

  int timeout_us = timeout_ms * 1000;
  while (!is_shutdown_.load()) {
    uint64_t seq = indicator_->next_seq.load();
    if (seq != next_seq_) {
      auto idx = next_seq_ % kBufLength;
      auto actual_seq = indicator_->seqs[idx];
      if (actual_seq >= next_seq_) {
        next_seq_ = actual_seq;
        *info = indicator_->infos[idx];
        ++next_seq_;
        return true;
      } else {
        ADEBUG << "seq[" << next_seq_ << "] is writing, can not read now.";
      }
    }

    if (timeout_us > 0) {
      std::this_thread::sleep_for(std::chrono::microseconds(50));
      timeout_us -= 50;
    } else {
      return false;
    }
  }
  return false;
}

// 接收线程func
void ShmDispatcher::ThreadFunc() {
  ReadableInfo readable_info;
  while (!is_shutdown_.load()) {
  	// notify 为 conditon_notifier
    if (!notifier_->Listen(100, &readable_info)) {
      ADEBUG << "listen failed.";
      continue;
    }
   	XXXXXX 省略
    ReadMessage(channel_id, block_index);
  }
}

// 发送端
template <typename M>
bool ShmTransmitter<M>::Transmit(const M& msg, const MessageInfo& msg_info) {
  XXXXXX 省略
  return notifier_->Notify(readable_info);
}

从以上代码可知,apollo中通过next_seq通知接受端接受数据,同时next_seq为原子变量,自增线程安全;

但是一下两句并未原子操作,在c++ 内存模型中,多线程存在编译乱序、cpu乱序、缓存可见性等等问题,接收端线程对于infos和seqs的可见顺序有可能是乱序的,即seqs的修改先可见,然后再是infos的赋值。

indicator_->infosidx = info; // 该两句应当是原子或者顺序不重排

indicator_->seqsidx = seq; // 重排导致接受端错误接收一场数据

在接收端的接收处理中,则会因为乱序,可能读取到next_seq以及seqsidx变化,但infosidx仍然为旧数据的情况。

复制代码
uint64_t seq = indicator_->next_seq.load();   
if (seq != next_seq_) {
  auto idx = next_seq_ % kBufLength;
  auto actual_seq = indicator_->seqs[idx];  //去读到正确数据 
  if (actual_seq >= next_seq_) {   // 判断通过
    next_seq_ = actual_seq;
    *info = indicator_->infos[idx]; // 写进程数据暂时不可见,读取到了旧数据
    ++next_seq_;
    return true;
  } else {
    ADEBUG << "seq[" << next_seq_ << "] is writing, can not read now.";
  }
}

即,发送端发送,1,2,3,4,5, 五个数据包;

接收端接收到,1,2,1,4,5, 五个数据包;

特别是在车端的常见arm架构弱内存模型会极其常见,为非常严重的bug。

解决方法

将seq变量改为原子变量,并引入的memory order里的acquire release 内存序

cpp 复制代码
struct Indicator {
    std::atomic<uint64_t> next_seq = {0};
    ReadableInfo infos[kBufLength];
    uint64_t seqs[kBufLength] = {0};
    std::atomic<uint64_t> seqs[kBufLength] = {0};
  };
bool ConditionNotifier::Notify(const ReadableInfo& info) {
  if (is_shutdown_.load()) {
    ADEBUG << "notifier is shutdown.";
    return false;
  }

  uint64_t seq = indicator_->next_seq.fetch_add(1);
  uint64_t idx = seq % kBufLength;
  indicator_->infos[idx] = info;
  indicator_->seqs[idx].store(seq, std::memory_order_release);  // infos赋值 先序于seqs赋值
  return true;
}

bool ConditionNotifier::Listen(int timeout_ms, ReadableInfo* info) {
  if (info == nullptr) {
    AERROR << "info nullptr.";
    return false;
  }

  if (is_shutdown_.load()) {
    ADEBUG << "notifier is shutdown.";
    return false;
  }

  int timeout_us = timeout_ms * 1000;
  while (!is_shutdown_.load()) {
    uint64_t seq = indicator_->next_seq.load();
    if (seq != next_seq_) {
      auto idx = next_seq_ % kBufLength;
      auto actual_seq = indicator_->seqs[idx].load(std::memory_order_acquire);
      if (actual_seq >= next_seq_) {
        next_seq_ = actual_seq;
        *info = indicator_->infos[idx];
        ++next_seq_;
        return true;
      } else {
        ADEBUG << "seq[" << next_seq_ << "] is writing, can not read now.";
      }
    }

    if (timeout_us > 0) {
      std::this_thread::sleep_for(std::chrono::microseconds(50));
      timeout_us -= 50;
    } else {
      return false;
    }
  }
  return false;
}
相关推荐
小飞在努力5 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
星恒随风9 小时前
从0实现C++ AI大模型接入SDK(四):大模型API接入、公共数据结构设计与日志库封装
linux·服务器·数据结构·c++·人工智能
炸膛坦客9 小时前
单片机/C/C++八股:(三十四)C 语言函数参数入栈顺序
c语言·开发语言·c++
码途行者10 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK12 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
星恒随风14 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
h_a_o777oah15 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
撸码猿15 小时前
Ladybird 进入 alpha:从零写的浏览器引擎现在能下载了
c++·rust·开源·浏览器·ladybird
ShineWinsu16 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
爱和冰阔落17 小时前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战
linux·c++·http·网络编程·web服务器