Actually clean up in case of any exception during malloc debug setup.
Bug: 242332243
Test: run StsHostTestCases
Change-Id: I3c12162f1dd2ea3bda960fe5c9160c0b0c80b59d
Merged-In: I3c12162f1dd2ea3bda960fe5c9160c0b0c80b59d
(cherry picked from commit bc73366d9a0b6a368b35ade0b8647a1f98741ec6)
diff --git a/libraries/sts-common-util/host-side/src/com/android/sts/common/MallocDebug.java b/libraries/sts-common-util/host-side/src/com/android/sts/common/MallocDebug.java
index 7afe880..116394e 100644
--- a/libraries/sts-common-util/host-side/src/com/android/sts/common/MallocDebug.java
+++ b/libraries/sts-common-util/host-side/src/com/android/sts/common/MallocDebug.java
@@ -48,7 +48,7 @@
private MallocDebug(
ITestDevice device, String mallocDebugOption, String processName, boolean isService)
- throws DeviceNotAvailableException {
+ throws DeviceNotAvailableException, TimeoutException {
this.device = device;
this.processName = processName;
@@ -61,33 +61,34 @@
// The only known failure case of this was multiple logcat -c race, so it's fine to fail.
device.executeShellV2Command("logcat -c");
- this.setMallocDebugOptionsProperty =
- SystemUtil.withProperty(MALLOC_DEBUG_OPTIONS_PROP, mallocDebugOption, device);
- this.setAttachedProgramProperty =
- SystemUtil.withProperty(MALLOC_DEBUG_PROGRAM_PROP, processName, device);
+ try {
+ this.setMallocDebugOptionsProperty =
+ SystemUtil.withProperty(MALLOC_DEBUG_OPTIONS_PROP, mallocDebugOption, device);
+ this.setAttachedProgramProperty =
+ SystemUtil.withProperty(MALLOC_DEBUG_PROGRAM_PROP, processName, device);
- // Kill and wait for the process to come back if we're attaching to a service
- this.killProcess = null;
- if (isService) {
- try {
+ // Kill and wait for the process to come back if we're attaching to a service
+ this.killProcess = null;
+ if (isService) {
this.killProcess = ProcessUtil.withProcessKill(device, processName, null);
ProcessUtil.waitProcessRunning(device, processName);
- } catch (TimeoutException e1) {
- try {
- setMallocDebugOptionsProperty.close();
- setAttachedProgramProperty.close();
- } catch (Exception e2) {
- fail(
- "Could not restart '"
- + processName
- + "' before enabling malloc debug. Additionally, there was an"
- + " exception while trying to reset device state. Tests after"
- + " this may not work as expected!\n"
- + e2);
- }
- assumeNoException(
- "Could not restart '" + processName + "' before enabling malloc debug", e1);
}
+ } catch (Throwable e1) {
+ try {
+ if (setMallocDebugOptionsProperty != null) {
+ setMallocDebugOptionsProperty.close();
+ }
+ if (setAttachedProgramProperty != null) {
+ setAttachedProgramProperty.close();
+ }
+ } catch (Exception e2) {
+ fail(
+ "Could not enable malloc debug. Additionally, there was an"
+ + " exception while trying to reset device state. Tests after"
+ + " this may not work as expected!\n"
+ + e2);
+ }
+ throw e1;
}
}
@@ -121,7 +122,7 @@
*/
public static AutoCloseable withLibcMallocDebugOnService(
ITestDevice device, String mallocDebugOptions, String processName)
- throws DeviceNotAvailableException, IllegalArgumentException {
+ throws DeviceNotAvailableException, IllegalArgumentException, TimeoutException {
if (processName == null || processName.isEmpty()) {
throw new IllegalArgumentException("Service processName can't be empty");
}
@@ -140,7 +141,7 @@
*/
public static AutoCloseable withLibcMallocDebugOnNewProcess(
ITestDevice device, String mallocDebugOptions, String processName)
- throws DeviceNotAvailableException, IllegalArgumentException {
+ throws DeviceNotAvailableException, IllegalArgumentException, TimeoutException {
if (processName == null || processName.isEmpty()) {
throw new IllegalArgumentException("processName can't be empty");
}
@@ -160,7 +161,8 @@
* debug errors when closed.
*/
public static AutoCloseable withLibcMallocDebugOnAllNewProcesses(
- ITestDevice device, String mallocDebugOptions) throws DeviceNotAvailableException {
+ ITestDevice device, String mallocDebugOptions)
+ throws DeviceNotAvailableException, TimeoutException {
return new MallocDebug(device, mallocDebugOptions, null, false);
}