Skip to content

Commit 36482b9

Browse files
committed
Do not leak HandlerThread in SystemEventsReceiver
1 parent c2447df commit 36482b9

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public final class SystemEventsBreadcrumbsIntegration
6666
private volatile boolean isClosed = false;
6767
private volatile boolean isStopped = false;
6868
private volatile IntentFilter filter = null;
69+
private volatile HandlerThread handlerThread = null;
6970
private final @NotNull AtomicBoolean isReceiverRegistered = new AtomicBoolean(false);
7071
private final @NotNull AutoClosableReentrantLock receiverLock = new AutoClosableReentrantLock();
7172
// Track previous battery state to avoid duplicate breadcrumbs when values haven't changed
@@ -141,13 +142,16 @@ private void registerReceiver(
141142
filter.addAction(item);
142143
}
143144
}
144-
try {
145-
// registerReceiver can throw SecurityException but it's not documented in the
146-
// official docs
147-
final @NotNull HandlerThread handlerThread =
145+
if (handlerThread == null) {
146+
handlerThread =
148147
new HandlerThread(
149148
"SystemEventsReceiver", Process.THREAD_PRIORITY_BACKGROUND);
150149
handlerThread.start();
150+
}
151+
try {
152+
// registerReceiver can throw SecurityException but it's not documented in the
153+
// official docs
154+
151155
// onReceive will be called on this handler thread
152156
final @NotNull Handler handler = new Handler(handlerThread.getLooper());
153157
ContextUtils.registerReceiver(context, options, receiver, filter, handler);
@@ -204,6 +208,8 @@ public void close() throws IOException {
204208
try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) {
205209
isClosed = true;
206210
filter = null;
211+
handlerThread.quit();
212+
handlerThread = null;
207213
}
208214

209215
AppState.getInstance().removeAppStateListener(this);

sentry/src/main/java/io/sentry/ISentryExecutorService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Future<?> schedule(final @NotNull Runnable runnable, final long delayMillis)
4646
*/
4747
boolean isClosed();
4848

49-
/** Pre-warms the executor service by increasing the initial queue capacity. SHOULD be called
49+
/**
50+
* Pre-warms the executor service by increasing the initial queue capacity. SHOULD be called
5051
* directly after instantiating this executor service.
5152
*/
5253
void prewarm();

0 commit comments

Comments
 (0)