Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Call Sentry#close on JVM shutdown. #497

Merged
merged 6 commits into from
Jul 29, 2020

Conversation

maciejwalkowiak
Copy link
Contributor

📢 Type of change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring

📜 Description

  • Call Sentry#close on JVM shutdown - as a result flush all Sentry events that are in progress.
  • Configure SendCachedEventFireAndForgetIntegration by default only for Android as for the console/server application cache does not apply

💡 Motivation and Context

Without this change, when Sentry is used in the console application, the main thread may finish before events are sent to Sentry and as a result no events are sent.

This idea was briefly discussed with @marandaneto and it's a subject for discussion - perhaps there is a better way to achieve the same result without attaching shutdown hook.

💚 How did you test it?

📝 Checklist

  • I reviewed submitted code
  • I added tests to verify changes
  • All tests passing
  • No breaking changes

🔮 Next steps

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

:shipit:

@@ -115,6 +116,14 @@ private static void installDefaultIntegrations(
final @NotNull IBuildInfoProvider buildInfoProvider,
final @NotNull ILoadClass loadClass) {

options.addIntegration(
Copy link
Member

Choose a reason for hiding this comment

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

As discussed offline, for now it makes sense to just add the "event cache" related stuff into Android and focus on the core not having offline caching altogether.

Down the road when the need comes we can revisit and think of a better way to "opt-in" to offline mode in a simple way, that would work for Android or Java apps (say desktop could leverage that)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Just so that it doesn't get lost - I think we should expose ideally single method that turns on caching which takes cachePathDir as parameter and under the hood also turns on all cache-related integrations.

Copy link
Contributor

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

thanks for doing this.
left a few comments, and also, the base branch is master right now, should it not be feat/sentry-java?

- close Hub instead of Sentry
- pass Runtime as constructor parameter in ShutdownHookIntegration to make class testable
@maciejwalkowiak
Copy link
Contributor Author

Thanks @marandaneto for a review. Fixes are applied.

Regarding target branch, my understanding is that everything that affects Sentry Core goes to master and only other framework-specific integrations are meant to go to feat/sentry-java. In practice - next PR will contain sample console application - it would also go to master, but the Logback integration that will follow would go to feat/sentry-java.

}

@Override
public void register(@NotNull IHub hub, @NotNull SentryOptions options) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public void register(@NotNull IHub hub, @NotNull SentryOptions options) {
public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {

Copy link
Contributor

Choose a reason for hiding this comment

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

any reason to not apply the final modifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No specific reason, I've seen some methods with final and some without. I'll add them.

My personal take on final on method parameters is that they clutter code and instead all parameters should be treated as final implicitly. That's just an opinion, I will adjust to existing coding standards.

Copy link
Contributor

@marandaneto marandaneto Jul 29, 2020

Choose a reason for hiding this comment

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

got it, just asking cus it was in my first GH "suggestion".

Agreed that they should be treated as final implicitly, but IMO when you have multithread systems, having an extra final makes the compiler to help you out if you are not cloning/deep copying an object but instead messing up with the references, we had a bug like this a few months ago, so we've decided to add in all the missing ones/and new ones

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps there is a way to configure static code analysis to make sure it doesn't sneak in?

Copy link
Contributor

Choose a reason for hiding this comment

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

all the data classes are mutable by design, I've tried once but then it warned about almost everything.
so basically what I am doing is, new things I always try to make everything final if it can and if I am touching some classes, I already check the missing final and @NotNull, @Nullable to make better interoperability with Kotlin tests

@marandaneto
Copy link
Contributor

Thanks @marandaneto for a review. Fixes are applied.

Regarding target branch, my understanding is that everything that affects Sentry Core goes to master and only other framework-specific integrations are meant to go to feat/sentry-java. In practice - next PR will contain sample console application - it would also go to master, but the Logback integration that will follow would go to feat/sentry-java.

I see, that makes sense, I'm just wondering if the ShutdownHookIntegration should be also a default integration for Android? does the Dalvik/Art behave similarly? I'd rather make some tests before merging it, the idea is to have master also stable so we could publish a new version without any problems, so said that, Have you tested the ShutdownHook on Android when there's an UncaughtException? if so, happy to approve and merge it.

@maciejwalkowiak
Copy link
Contributor Author

I didn't find any documentation stating that shutdown hooks behave differently on Android but I haven't tested it and I also don't think I am the right person to do it as my Android experience is limited (I can though it will just take time). I'll leave it up to you and @bruno-garcia to decide how to proceed.

@marandaneto
Copy link
Contributor

I didn't find any documentation stating that shutdown hooks behave differently on Android but I haven't tested it and I also don't think I am the right person to do it as my Android experience is limited (I can though it will just take time). I'll leave it up to you and @bruno-garcia to decide how to proceed.

let's merge then and I test it, last case we just remove adding this integration by default, the integration itself is already a big gain.
so I don't need to fork your repo, I'll take care of it, thanks!

Copy link
Contributor

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

LGTM

@bruno-garcia bruno-garcia merged commit 410da2d into getsentry:master Jul 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants