Skip to content

Commit bac40e0

Browse files
committed
Try to fix BackgroundServiceStartNotAllowedException for Android stop
We see quite a few of these exceptions. We're allowed to send the intent to the Android app, but once it receives it, it's still in the background, and it's not allowed to send an intent to the background VPN service, so the service never actually shuts down. Doesn't seem to happen all the time, but just on some devices. This fix should clean that up nicely.
1 parent b74fdfb commit bac40e0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/interceptors/android/android-adb-interceptor.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,20 @@ export class AndroidAdbInterceptor implements Interceptor {
168168
const deviceIds = this.deviceProxyMapping[port] || [];
169169

170170
return Promise.all(
171-
deviceIds.map(deviceId => {
171+
deviceIds.map(async (deviceId) => {
172172
const deviceClient = new DeviceClient(this.adbClient, deviceId);
173-
deviceClient.startActivity({
173+
174+
// Bring app to the front, as otherwise it can't run intents (to tell
175+
// the background service to stop the VPN)
176+
await bringToFront(
177+
deviceClient,
178+
'tech.httptoolkit.android.v1/tech.httptoolkit.android.MainActivity'
179+
).catch(logError); // Not that important, so we continue if this fails somehow
180+
181+
await deviceClient.startActivity({
174182
wait: true,
175183
action: 'tech.httptoolkit.android.DEACTIVATE'
176-
})
184+
});
177185
})
178186
);
179187
}

0 commit comments

Comments
 (0)