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

Fix misuses of CopyOnWriteArrayList #4247

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

lcian
Copy link
Member

@lcian lcian commented Mar 10, 2025

📜 Description

CopyOnWriteArrayList allows us to access its elements without copying and without the possibility to cause a ConcurrentModificationException.
This PR modifies its usages so that we avoid copying in order to get better performance memory-wise.

We also make sure that we are using it correctly to avoid other issues (using the iterator to avoid a possible NPE that could happen with index-based accesses, and other cases where we do things that are not thread safe).

💡 Motivation and Context

Closes #2815

💚 How did you test it?

Tested as documented in #2815 (comment)

📝 Checklist

  • 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.
  • I updated the wizard 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.

@lcian lcian marked this pull request as draft March 10, 2025 16:36
@@ -155,7 +154,10 @@ private void onDeadlineTimeoutReached() {
// abort all child-spans first, this ensures the transaction can be finished,
// even if waitForChildren is true
// iterate in reverse order to ensure leaf spans are processed before their parents
@NotNull final ListIterator<Span> iterator = children.listIterator(children.size());
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not thread safe, the list could change size after we create the iterator

Comment on lines +158 to +160
while (iterator.hasNext()) {
iterator.next();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to do it this way as there's no other way to get a reverse iterator in a thread-safe manner

Copy link
Contributor

github-actions bot commented Mar 10, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 411.64 ms 526.35 ms 114.71 ms
Size 1.58 MiB 2.21 MiB 642.07 KiB

@lcian lcian changed the title Avoid copying and iterate correctly on SentryTracer.children Fix misuses of CopyOnWriteArrayList Mar 10, 2025
@lcian
Copy link
Member Author

lcian commented Mar 10, 2025

Only found another occurrence where we are misusing CopyOnWriteArrayList.

return new CopyOnWriteArrayList<>(attachments);
return attachments;
Copy link
Member Author

Choose a reason for hiding this comment

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

It is not necessary to copy here, as we are already using a CopyOnWriteArrayList

@lcian lcian marked this pull request as ready for review March 10, 2025 17:10
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.

Prevent SentryTracer.getLatestActiveSpan from copying all child spans to avoid OOMs
1 participant