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

Replace synchronized withReentrantLock #3715

Merged
merged 5 commits into from
Sep 30, 2024

Conversation

adinauer
Copy link
Member

📜 Description

  • Most usages of synchronized keyword in method signatures and synchronized (...) { blocks have been replaced by using ReentrantLock
  • Adds AutoClosableReentrantLock which offers an acquire method that locks and returns an AutoClosable that unlocks on close. This can be used in try blocks
  • SynchronizedCollection and SynchronizedQueue now expect a AutoClosableReentrantLock instead of Object when passing a lock object
  • Instance locks (previously synchronized methods and synchronized (this)) now use lock
  • Static locks (previously static synchronized, synchronized (...class) and synchronized (...getInstance)) now use staticLock
  • If there was a lock object before, the type has been changed from Object to AutoClosableReentrantLock
  • Changing Android only classes wasn't totally necessary but IMO it's easier to maintain if there's only one way of synchronizing that's being used

💡 Motivation and Context

Fixes #3312

💚 How did you test it?

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

github-actions bot commented Sep 25, 2024

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 436.66 ms 493.14 ms 56.48 ms
Size 1.70 MiB 2.35 MiB 668.82 KiB

Previous results on branch: fix/replace-synchronized-with-lock

Startup times

Revision Plain With Sentry Diff
4a331fa 408.77 ms 477.06 ms 68.29 ms

App size

Revision Plain With Sentry Diff
4a331fa 1.70 MiB 2.35 MiB 668.79 KiB

Copy link
Member

@markushi markushi left a 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) {
Copy link
Member

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.

Suggested change
public ActivityBreadcrumbsIntegration(final @NotNull Application application) {
// TODO check if locking is even required at all for lifecycle methods
public ActivityBreadcrumbsIntegration(final @NotNull Application application) {

Comment on lines 481 to 483
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
// no-op
}
Copy link
Member

Choose a reason for hiding this comment

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

  1. I don't think we need the locking here
  2. Tell me how you automated that refactoring 😅
Suggested change
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
// no-op
}
// no-op

Copy link
Member Author

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.

Copy link
Member Author

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;
Copy link
Member

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?

Copy link
Member Author

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.

@adinauer adinauer merged commit f1b7116 into 8.x.x Sep 30, 2024
3 of 4 checks passed
@adinauer adinauer deleted the fix/replace-synchronized-with-lock branch September 30, 2024 06:27
Copy link
Contributor

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

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 #skip-changelog to the PR description.

Generated by 🚫 dangerJS against 2dfe2ae

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.

2 participants