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
…655)

* fix: check if the instrumentation process stops by sending a request

* chore: use process existence check instead

* use vanilla axios

* change to warn

* simplify a bit

* Update uiautomator2.js
  • Loading branch information
KazuCocoa authored Sep 21, 2023
1 parent 13f854f commit d42950a
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/uiautomator2.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,37 @@ class UiAutomator2Server {
async cleanupAutomationLeftovers (strictCleanup = false) {
this.log.debug(`Performing ${strictCleanup ? 'strict' : 'shallow'} cleanup of automation leftovers`);

const axiosTimeout = 500;

const waitStop = async () => {
// Wait for the process stop by sending a status request to the port.
// We observed the process stop could be delayed, thus causing unexpected crashes
// in the middle of the session preparation process. It caused an 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 axios({
url: `http://${this.host}:${this.systemPort}/status`,
timeout: axiosTimeout,
});
} catch (err) {
return true;
}
}, {
waitMs: timeout,
intervalMs: 100,
});
} catch (err) {
this.log.warn(`The ${SERVER_TEST_PACKAGE_ID} process might fail to stop within ${timeout}ms timeout.`);
}
};

try {
const {value} = (await axios({
url: `http://${this.host}:${this.systemPort}/sessions`,
timeout: 500,
timeout: axiosTimeout,
})).data;
const activeSessionIds = value.map(({id}) => id).filter(Boolean);
if (activeSessionIds.length) {
Expand All @@ -329,13 +356,13 @@ class UiAutomator2Server {
try {
await this.adb.forceStop(SERVER_TEST_PACKAGE_ID);
} catch (ignore) {}
if (!strictCleanup) {
return;
if (strictCleanup) {
// https://github.com/appium/appium/issues/10749
try {
await this.adb.killProcessesByName('uiautomator');
} catch (ignore) {}
}
// https://github.com/appium/appium/issues/10749
try {
await this.adb.killProcessesByName('uiautomator');
} catch (ignore) {}
await waitStop();
}
}

Expand Down

0 comments on commit d42950a

Please sign in to comment.