Git Product home page Git Product logo

Comments (2)

qinzuoyan avatar qinzuoyan commented on July 19, 2024

你可能认为这样的调用轨迹会造成死锁:

  1. RpcClientImpl::StopStreams() ,加锁 _stream_map_lock,然后close所有stream;
  2. RpcClientStream::on_closed()中,调用所有未完成的cntl->Done();
  3. Done()中调用用户回调函数,用户在回调函数中再次发送请求,调用RpcClientImpl::CallMethod();
  4. CallMethod()中调用RpcClientImpl::FindOrCreateStream(),对_stream_map_lock加锁——由于1中已经加锁,造成死锁。

实际上,这里不会死锁,因为:

  1. 用户callback总是在callback_thread中执行的,即1、2和3、4不会在同一个线程中完成,不会死锁,只会竞锁。可以查看RpcChannelImpl::DoneCallback()函数,这里是真正调用用户callback的地方,如果是异步调用的话(用户设置了callback函数),Done最终是调用到了这里。里面将对用户callback的调用post到了CallbackThreadGroup中。
  2. RpcClientImpl::_is_running从一定程度上避免了上述问题的发生:
  3. RpcClientImpl::CallMethod()首先会检查is_runing状态;
  4. RpcClientImpl::StopStreams()只在RpcClientImpl::Stop()中被调用,而且调用之前,已经设置了“_is_running = false”;
  5. 但这里不能完全保证,只是一种优化:如果要完全保证,需要在RpcClientImpl::CallMethod()中对_start_stop_lock加锁,与RpcClientImpl::Stop()中对_is_runing的设置形成互斥。在实现时,考虑到加锁会影响性能,在保证不出问题的情况下,CallMethod()中并没有对_start_stop_lock加锁。

谢谢提醒,是否解释了你的疑问?

from sofa-pbrpc.

gzc9047 avatar gzc9047 commented on July 19, 2024

Confirm with Zuoyan, this scene will not happened. Please close it.
Thank you.

from sofa-pbrpc.

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.