Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: unsafe double-checked locking in SofaRegistryServiceImpl #2642

Merged
merged 5 commits into from
May 6, 2020

Conversation

chenxi-null
Copy link
Contributor

Ⅰ. Describe what this PR did

This usage of double check locking is not thread-safe, the reason is present in this PR #1599 which I submitted before.

How to fix:

Because DefaultRegistryClient is in third-part jar, the init method invocation can't be moved to the constructor.

So the local variable is used to correct DCL and minimize the number of field reads.

See also: https://lgtm.com/rules/1507076816054/

Quote from the article:

// a local variable can be used to avoid reading the field more times than neccessary.

private Object lock = new Object();
private volatile MyObject f = null;

public MyObject getMyObject() {
  MyObject result = f;
  if (result == null) {
    synchronized(lock) {
      result = f;
      if (result == null) {
        result = new MyObject();
        result.init();
        f = result; // GOOD
      }
    }
  }
  return result;
}

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

Because `DefaultRegistryClient` is in third-part jar, the `init` method invocation can't be moved to constructor.

So the local variable is used to correct DCL and minimize the number of field reads.

See also: https://lgtm.com/rules/1507076816054/
@codecov-io
Copy link

codecov-io commented Apr 30, 2020

Codecov Report

Merging #2642 into develop will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #2642      +/-   ##
=============================================
- Coverage      51.25%   51.24%   -0.02%     
+ Complexity      2819     2817       -2     
=============================================
  Files            556      556              
  Lines          17803    17803              
  Branches        2101     2101              
=============================================
- Hits            9125     9123       -2     
  Misses          7818     7818              
- Partials         860      862       +2     
Impacted Files Coverage Δ Complexity Δ
...o/seata/server/coordinator/DefaultCoordinator.java 54.63% <0.00%> (-0.52%) 28.00% <0.00%> (-1.00%)
...in/java/io/seata/server/session/GlobalSession.java 83.71% <0.00%> (-0.46%) 71.00% <0.00%> (-1.00%)

@zjinlei zjinlei added this to the 1.3.0 milestone May 1, 2020
Copy link
Member

@xingfudeshi xingfudeshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@xingfudeshi
Copy link
Member

Please associate your GitHub account with your email.@chenxi-null

Copy link
Contributor Author

@chenxi-null chenxi-null left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please associate your GitHub account with your email.@chenxi-null

@xingfudeshi Is there anything wrong with my email ?
I have already associated with my GitHub account with the email chenxiCoder@gmail.com, and it can receive notification from Github.

@zjinlei
Copy link
Contributor

zjinlei commented May 6, 2020

Please associate your GitHub account with your email.@chenxi-null

@xingfudeshi Is there anything wrong with my email ?
I have already associated with my GitHub account with the email chenxiCoder@gmail.com, and it can receive notification from Github.

https://blog.csdn.net/idealcitier/article/details/82697268

jsbxyyx and others added 3 commits May 6, 2020 15:02
Because `DefaultRegistryClient` is in third-part jar, the `init` method invocation can't be moved to constructor.

So the local variable is used to correct DCL and minimize the number of field reads.

See also: https://lgtm.com/rules/1507076816054/
Copy link
Contributor

@zjinlei zjinlei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zjinlei zjinlei changed the title bugfix: unsafe double-checked locking in SofaRegistryServiceImpl bugfix: unsafe double-checked locking in SofaRegistryServiceImpl May 6, 2020
@zjinlei zjinlei merged commit d8f6e1c into apache:develop May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants