Giter VIP home page Giter VIP logo

Comments (4)

neuskb avatar neuskb commented on June 11, 2024

知道原因了。GlobalData::RegisterService写的就有问题

from apollo.

neuskb avatar neuskb commented on June 11, 2024

多线程调用RegisterService的时候,如果service填的还是同一个,出现踩内存的场景,虽然service_id_map_是线程安全的,但是只是Get和Set接口线程安全,在通过Get拿到name后,这个name填的是二级指针,所以name本身就是service_id_map_的value,另一个线程可能会再去修改这个value,造成Get出来的name 会有多线程同时读写的风险,用我上面贴的代码去复现,基本必现。

from apollo.

daohu527 avatar daohu527 commented on June 11, 2024

Will check and feedback soon. I tested it and it will indeed report an error.

from apollo.

neuskb avatar neuskb commented on June 11, 2024

Will check and feedback soon. I tested it and it will indeed report an error.

修改方案:

uint64_t GlobalData::RegisterService(const std::string& service) {
  auto id = Hash(service);
  std::lock_guard<std::mutex> lock(service_id_map_mutex_);
  while (service_id_map_.Has(id)) {
    std::string* name = nullptr;
    service_id_map_.Get(id, &name);
    if (service == *name) {
      break;
    }
    ++id;
    AWARN << "Service name hash collision: " << service << " <=> " << *name;
  }
  service_id_map_.Set(id, service);
  return id;
}

from apollo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.