Skip to content

There is a potential deadlock in DubboProtocol#getSharedClient #677

Closed

Description

@aCoder2013

Synchronized on String#intern is a bad practice, interning a string will make it a global object .Which means if you synchronized the same interned strings in different parts of your code , some shit'll happen ,eg : deadlock

https://github.com/alibaba/dubbo/blob/9cf27a36848c22fbfd00877449fbe0eaf53e040c/dubbo-rpc/dubbo-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java#L333

Activity

  1. added
    type/need-triage
    on Sep 26, 2017
  2. removed
    type/need-triage
    on Oct 9, 2017
  3. added this to the 2.5.7 milestone on Oct 9, 2017
  4. modified the milestones: 2.5.7, 2.5.8 on Nov 3, 2017
  5. chickenlj commented on Nov 30, 2017

    @chickenlj
    Contributor

    If we put all export/refer actions into a single serial process, we can get rid locks.
    We've decided to solve this problem that way.

  6. modified the milestones: 2.5.8, some day on Dec 8, 2017
  7. beiwei30 commented on May 23, 2018

    @beiwei30
    Member

    dead lock will not happen since this is the only place to use str.intern() in dubbo code base, but to be safe, I decide to change it to:

            synchronized ((key + ".lock").intern()) {
                ...
           }

    to solve the potential issue.

  8. aCoder2013 commented on May 23, 2018

    @aCoder2013
    Author

    @beiwei30 how about a lock pool, so basically this pool always return the same lock instance with the same key, and use something like access time to collect unused instance

  9. beiwei30 commented on May 23, 2018

    @beiwei30
    Member

    @aCoder2013 a lock pool certainly works, but it make no difference with str.intern() if recycling is not considered, furthermore, it should occupy more memory. Are we facing overhead design here? :)

  10. aCoder2013 commented on May 23, 2018

    @aCoder2013
    Author

    @beiwei30 interning a string will make it a global object, but each service can have its own lock pool .
    I didn't read all the source code here ,do we need to consider memory consumption?

  11. beiwei30 commented on May 24, 2018

    @beiwei30
    Member

    @aCoder2013 how about this:

        ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
    
        /**
         * Get shared connection
         */
        private ExchangeClient getSharedClient(URL url) {
            String key = url.getAddress();
            ReferenceCountExchangeClient client = referenceClientMap.get(key);
            if (client != null) {
                if (!client.isClosed()) {
                    client.incrementAndGetCount();
                    return client;
                } else {
                    referenceClientMap.remove(key);
                }
            }
            locks.putIfAbsent(key, new Object());
            synchronized (locks.get(key)) {
                if (referenceClientMap.containsKey(key)) {
                    return referenceClientMap.get(key);
                }
                
                ExchangeClient exchangeClient = initClient(url);
                client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap);
                referenceClientMap.put(key, client);
                ghostClientMap.remove(key);
                locks.remove(key);
                return client;
            }
        }
  12. aCoder2013 commented on May 24, 2018

    @aCoder2013
    Author

    LGTM!

  13. beiwei30 commented on May 24, 2018

    @beiwei30
    Member

    :)

  14. added a commit that references this issue on Sep 9, 2019
    1f63dc4
  15. added a commit that references this issue on Sep 10, 2019
    116c9c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions