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

Bug 1910449 - Use daemon-threads with the MPS timer instead of the default user-threads #2930

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

travis79
Copy link
Member

@travis79 travis79 commented Aug 16, 2024

Changing the MPS java.util.Timer to use daemon-threads instead of the default user-threads because user-threads can potentially prevent the application from terminating properly since the OS requires all user-threads to be joined before the app can terminate. Daemon-threads are terminated by the OS once all user-threads have completed and are not awaited before the application can terminate.

@travis79
Copy link
Member Author

travis79 commented Aug 16, 2024

This requires a bit of communication with product and DS folks before landing and release to ensure no one is surprised by any changes to the data, expected or unexpected.

@travis79 travis79 force-pushed the Bug1910449 branch 2 times, most recently from 6608a21 to 7e74f7f Compare August 19, 2024 12:40
Copy link
Member

@badboy badboy left a comment

Choose a reason for hiding this comment

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

As called out in the bug what about this trivial patch:

diff --git glean-core/android/src/main/java/mozilla/telemetry/glean/scheduler/MetricsPingScheduler.kt glean-core/android/src/main/java/mozilla/telemetry/glean/scheduler/MetricsPingScheduler.kt
index 6387ceff5..0ed70bed8 100644
--- glean-core/android/src/main/java/mozilla/telemetry/glean/scheduler/MetricsPingScheduler.kt
+++ glean-core/android/src/main/java/mozilla/telemetry/glean/scheduler/MetricsPingScheduler.kt
@@ -107,7 +107,9 @@ internal class MetricsPingScheduler(
         // currently-running task.
         cancel()
 
-        timer = Timer("glean.MetricsPingScheduler")
+        if (timer == null) {
+            timer = Timer("glean.MetricsPingScheduler", true)
+        }
         timer?.schedule(MetricsPingTimer(this, reason), millisUntilNextDueTime)
     }

Not sure how you tested this change, but if you have a strategy for that maybe with the above daemon thread we can test again?

@travis79
Copy link
Member Author

Not sure how you tested this change, but if you have a strategy for that maybe with the above daemon thread we can test again?

Well... it involved an actual device and about a week of letting it sit for about 2 days at a time waiting for things to happen naturally 😅! It was very manual and time consuming due to the 1/day scheduling, but it seemed to behave as I intended. I'm not even sure all devices and Android versions behave the same with regards to apps running in the background, making this even more "fun" to tinker with.

I'm still set up to test this so I'll try the daemon thread approach as an option to this, since it seems like it would be an even cleaner approach than relying on the lifecycle detection & cancellation.

@travis79
Copy link
Member Author

timer = Timer("glean.MetricsPingScheduler")
        if (timer == null) {
            timer = Timer("glean.MetricsPingScheduler", true)
        }

Turns out the timer is always going to be null here because of the call to cancel() right before (which nulls out the timer. So, I don't think this is necessary to add the check before creating a new Timer anyway.

@travis79 travis79 force-pushed the Bug1910449 branch 2 times, most recently from 4af2424 to e300026 Compare August 21, 2024 19:15
@travis79 travis79 changed the title Bug 1910449 - Add explicit call to cancel for MPS (java.util.Timer) in Glean Android bindings Bug 1910449 - Use daemon-threads with the MPS timer instead of the default user-threads Aug 22, 2024
@travis79 travis79 requested a review from badboy September 3, 2024 13:12
@travis79 travis79 marked this pull request as ready for review September 3, 2024 13:12
@travis79 travis79 requested a review from a team as a code owner September 3, 2024 13:12
Use a daemon-thread instead of the default user-thread in the `java.util.Timer` used to schedule pings within the MetricsPingScheduler. User-threads can prevent the application from terminating gracefully while daemon-threads do not.
@travis79 travis79 merged commit 59d57a0 into main Sep 5, 2024
29 of 30 checks passed
@travis79 travis79 deleted the Bug1910449 branch September 5, 2024 12:46
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