Description
The BlockCanary class initializes a private static final Executor named fileIoExecutor for handling log zipping and writing. However, this executor is never shut down, even when BlockCanary.stop() is invoked.
Root Cause
-
Static Resource: The executor is defined as private static final at line 133.
-
Missing Lifecycle Management: There is no mechanism to terminate the fileIoExecutor. The stop() method (Line 103) only stops the CPU and Stack samplers but leaves the IO thread pool active.
-
Process-Level Scope: In Android, this static thread persists as long as the application process is alive, which is broader than the lifecycle of the monitoring session.
Impact
-
Thread Leak: The thread named "File-IO" persists indefinitely, leaking resources.
-
StrictMode Violations: This causes StrictMode thread leak warnings (e.g., Detecting explicit GC) during integration testing or when the app attempts to exit cleanly.
Relevant Code
File: com/github/moduth/blockcanary/BlockCanary.java
// Line 133
private static final Executor fileIoExecutor = newSingleThreadExecutor("File-IO");
// Line 103: stop() does not handle the executor
public void stop() {
if (mMonitorStarted) {
// ...
mBlockCanaryCore.stackSampler.stop();
mBlockCanaryCore.cpuSampler.stop();
// fileIoExecutor is left running
}
}
Suggested Fix
Modify the lifecycle of fileIoExecutor so it can be shut down inside the stop() method, or provide an explicit release() API to terminate the underlying thread pool.
Description
The BlockCanary class initializes a private static final Executor named fileIoExecutor for handling log zipping and writing. However, this executor is never shut down, even when BlockCanary.stop() is invoked.
Root Cause
Static Resource: The executor is defined as private static final at line 133.
Missing Lifecycle Management: There is no mechanism to terminate the fileIoExecutor. The stop() method (Line 103) only stops the CPU and Stack samplers but leaves the IO thread pool active.
Process-Level Scope: In Android, this static thread persists as long as the application process is alive, which is broader than the lifecycle of the monitoring session.
Impact
Thread Leak: The thread named "File-IO" persists indefinitely, leaking resources.
StrictMode Violations: This causes StrictMode thread leak warnings (e.g., Detecting explicit GC) during integration testing or when the app attempts to exit cleanly.
Relevant Code
File: com/github/moduth/blockcanary/BlockCanary.java
// Line 133
private static final Executor fileIoExecutor = newSingleThreadExecutor("File-IO");
// Line 103: stop() does not handle the executor
public void stop() {
if (mMonitorStarted) {
// ...
mBlockCanaryCore.stackSampler.stop();
mBlockCanaryCore.cpuSampler.stop();
// fileIoExecutor is left running
}
}
Suggested Fix
Modify the lifecycle of fileIoExecutor so it can be shut down inside the stop() method, or provide an explicit release() API to terminate the underlying thread pool.