Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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