Skip to content

Commit

Permalink
fix: check if the instrumentation process stops by sending a request
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa committed Sep 20, 2023
1 parent 13f854f commit aa33ce1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/uiautomator2.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,26 @@ class UiAutomator2Server {
async cleanupAutomationLeftovers (strictCleanup = false) {
this.log.debug(`Performing ${strictCleanup ? 'strict' : 'shallow'} cleanup of automation leftovers`);

const waitStop = async () => {
// Wait for the process stop by sending a status request to the port.
// We observed the process stop could be delayed, then it caused the process stop
// in the middle of session preparation. It caused invalid session error response by the uia2 server,
// but that was because the process stop's delay.
const timeout = 3000;
try {
await waitForCondition(async () => {
try {
await this.jwproxy.command('/status', 'GET');
} catch (err) { return true; }
}, {
waitMs: timeout,
intervalMs: 100,
});
} catch (err) {
this.log.errorAndThrow(`The instrumentation process might fail to stop within ${timeout}ms timeout.`);
}
};

try {
const {value} = (await axios({
url: `http://${this.host}:${this.systemPort}/sessions`,
Expand All @@ -330,12 +350,14 @@ class UiAutomator2Server {
await this.adb.forceStop(SERVER_TEST_PACKAGE_ID);
} catch (ignore) {}
if (!strictCleanup) {
await waitStop();
return;
}
// https://github.com/appium/appium/issues/10749
try {
await this.adb.killProcessesByName('uiautomator');
} catch (ignore) {}
await waitStop();
}
}

Expand Down

0 comments on commit aa33ce1

Please sign in to comment.