Skip to content

Get Transport timeout because of c++ try_lock_for fail spuriously #224

@ustcr7

Description

@ustcr7

In our service, we found "get timed_mutex timeout" error log some times. We add some debug log and find this question is caused by c++ try_lock_for fail spuriously .

std::shared_ptr<TcpTransport> TcpRemotingClient::CreateTransport(const string& addr, bool needResponse) {
  std::shared_ptr<TcpTransport> tts;

  {
    // try get m_tcpLock util m_tcpTransportTryLockTimeout to avoid blocking
    // long time, if could not get m_tcpLock, return NULL
    std::unique_lock<std::timed_mutex> lock(m_tcpTableLock, std::try_to_lock);
    if (!lock.owns_lock()) {
      if (!lock.try_lock_for(std::chrono::seconds(m_tcpTransportTryLockTimeout))) {
        LOG_ERROR("GetTransport of:%s get timed_mutex timeout", addr.c_str());
        std::shared_ptr<TcpTransport> pTcp;
        return pTcp;
      }
    }
}

may be we can slove this question by a thread local cache or global cache with mutex。 Or use some concurrenty map just like what java client do:

    private Channel getAndCreateChannel(final String addr) throws RemotingConnectException, InterruptedException {
        if (null == addr) {
            return getAndCreateNameserverChannel();
        }

        ChannelWrapper cw = this.channelTables.get(addr); //just get from concurreny map without lock
        if (cw != null && cw.isOK()) {
            return cw.getChannel();
        }

        return this.createChannel(addr);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdisscussSomething undering disscussingexternalmaybe some issue caused by dependency

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions