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

Prevent NPE by checking SentryTracer.timer for null again inside synchronized #2200

Merged
merged 2 commits into from Aug 3, 2022

Conversation

adinauer
Copy link
Member

@adinauer adinauer commented Aug 3, 2022

📜 Description

💡 Motivation and Context

NPE caused by timer becoming null after acquiring the synchronized lock. Now we check again after acquiring the lock to ensure this doesn't happen.

Fixes #2196

💚 How did you test it?

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • No breaking changes

🔮 Next steps

@adinauer adinauer requested a review from romtsn as a code owner August 3, 2022 06:42
Copy link

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter
Copy link

codecov-commenter commented Aug 3, 2022

Codecov Report

Merging #2200 (7cab097) into main (2759ef9) will decrease coverage by 0.00%.
The diff coverage is 75.00%.

@@             Coverage Diff              @@
##               main    #2200      +/-   ##
============================================
- Coverage     80.91%   80.90%   -0.01%     
  Complexity     3313     3313              
============================================
  Files           236      236              
  Lines         12154    12155       +1     
  Branches       1615     1616       +1     
============================================
  Hits           9834     9834              
  Misses         1725     1725              
- Partials        595      596       +1     
Impacted Files Coverage Δ
sentry/src/main/java/io/sentry/SentryTracer.java 91.36% <75.00%> (-0.42%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2759ef9...7cab097. Read the comment docs.

@@ -344,8 +344,10 @@ public void finish(@Nullable SpanStatus status) {

if (timer != null) {
synchronized (timerLock) {
timer.cancel();
timer = null;
if (timer != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this make the L345 redundant? Since every other operation is guarded by timerLock.
The benefit is that you spare the synchronized contention if timer is null, so makes sense to keep it.

Please do the same for LifecycleWatcher.

Choose a reason for hiding this comment

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

Double check lock, in order to not lock if timer is already null. Looks like a good pattern to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, as @brustolin said, it's there to avoid the lock if it isn't needed.

@marandaneto created #2202 to track the double checked locking improvement.

Copy link
Contributor

Choose a reason for hiding this comment

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

The double lock is a bad pattern in Java and is broken by default @brustolin
In this case, it works, since you made the field volatile as well, and the first lock is just for the avoidance of contention

Copy link
Member

Choose a reason for hiding this comment

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

That seems specific to the JVM. Does it affect ART? Also, there are some 'theoreticals' in there (I just skimmed though)
Also, we're not initializing the Timer in this method, so what he's talking about doesn't seem to apply here.

Copy link
Contributor

Choose a reason for hiding this comment

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

The shared article is more about how you can do the double-checked locking wrong, and that's why it's considered an anti-pattern, the example indeed isn't the same, but I shared it just for context since @brustolin mentioned "good pattern".

In this case, it works, since you made the field volatile as well

Copy link
Contributor

Choose a reason for hiding this comment

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

@adinauer adinauer merged commit 4eb446a into main Aug 3, 2022
@adinauer adinauer deleted the feat/fix-npe-timer-cancel branch August 3, 2022 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants