-
-
Notifications
You must be signed in to change notification settings - Fork 443
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
Replace synchronized
withReentrantLock
#3715
Conversation
Performance metrics 🚀
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one! 🥷
@@ -25,6 +27,7 @@ public final class ActivityBreadcrumbsIntegration | |||
private final @NotNull Application application; | |||
private @Nullable IScopes scopes; | |||
private boolean enabled; | |||
private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock(); | |||
|
|||
public ActivityBreadcrumbsIntegration(final @NotNull Application application) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest I don't know why we had all the sychronized
blocks in here, it seems to stem from here. I think we can remove it in future.
public ActivityBreadcrumbsIntegration(final @NotNull Application application) { | |
// TODO check if locking is even required at all for lifecycle methods | |
public ActivityBreadcrumbsIntegration(final @NotNull Application application) { |
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | ||
// no-op | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I don't think we need the locking here
- Tell me how you automated that refactoring 😅
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { | |
// no-op | |
} | |
// no-op |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to take a look at each thing I refactored manually 😢 . My thinking was to make this more clear in case we replace the noop comment with some actual code in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the locking code with a comment that shall serve as a reminder
@@ -50,7 +52,7 @@ class SynchronizedCollection<E> implements Collection<E>, Serializable { | |||
/** The collection to decorate */ | |||
private final Collection<E> collection; | |||
/** The object to lock on, needed for List/SortedSet views */ | |||
final Object lock; | |||
final AutoClosableReentrantLock lock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be private
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because it's used in SynchronizedQueue
. The same is basically true for any non final class since you have to use the right lock instead of just relying on the synchronized keyword on methods.
…ityBreadcrumbsIntegration.java Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io>
Instructions and example for changelogPlease add an entry to Example: ## Unreleased
- Replace `synchronized` with`ReentrantLock` ([#3715](https://github.com/getsentry/sentry-java/pull/3715)) If none of the above apply, you can opt out of this check by adding |
📜 Description
synchronized
keyword in method signatures andsynchronized (...) {
blocks have been replaced by usingReentrantLock
AutoClosableReentrantLock
which offers anacquire
method that locks and returns anAutoClosable
that unlocks onclose
. This can be used intry
blocksSynchronizedCollection
andSynchronizedQueue
now expect aAutoClosableReentrantLock
instead ofObject
when passing a lock objectsynchronized
methods andsynchronized (this)
) now uselock
static synchronized
,synchronized (...class)
andsynchronized (...getInstance)
) now usestaticLock
Object
toAutoClosableReentrantLock
💡 Motivation and Context
Fixes #3312
💚 How did you test it?
📝 Checklist
sendDefaultPII
is enabled.🔮 Next steps