Skip to content

Commit 75da896

Browse files
fix: Update instrumentation process handling logic (#801)
1 parent fcb6790 commit 75da896

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

lib/uiautomator2.js

+35-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TEST_APK_PATH as testApkPath,
77
version as serverVersion
88
} from 'appium-uiautomator2-server';
9-
import { util, logger, timing } from 'appium/support';
9+
import { util, timing } from 'appium/support';
1010
import B from 'bluebird';
1111
import axios from 'axios';
1212

@@ -17,7 +17,6 @@ const SERVICES_LAUNCH_TIMEOUT = 30000;
1717
const SERVER_PACKAGE_ID = 'io.appium.uiautomator2.server';
1818
const SERVER_TEST_PACKAGE_ID = `${SERVER_PACKAGE_ID}.test`;
1919
const INSTRUMENTATION_TARGET = `${SERVER_TEST_PACKAGE_ID}/androidx.test.runner.AndroidJUnitRunner`;
20-
const instrumentationLogger = logger.getLogger('Instrumentation');
2120

2221
class UIA2Proxy extends JWProxy {
2322
/** @type {boolean} */
@@ -50,6 +49,9 @@ class UiAutomator2Server {
5049
/** @type {boolean|undefined} */
5150
disableSuppressAccessibilityService;
5251

52+
/** @type {import('teen_process').SubProcess|null} */
53+
instrumentationProcess;
54+
5355
constructor (log, opts = {}) {
5456
for (let req of REQD_PARAMS) {
5557
if (!opts || !util.hasValue(opts[req])) {
@@ -72,6 +74,7 @@ class UiAutomator2Server {
7274
this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);
7375
this.proxyCommand = this.jwproxy.command.bind(this.jwproxy);
7476
this.jwproxy.didInstrumentationExit = false;
77+
this.instrumentationProcess = null;
7578
}
7679

7780
/**
@@ -255,6 +258,9 @@ class UiAutomator2Server {
255258
while (retries < maxRetries) {
256259
this.log.info(`Waiting up to ${timeout}ms for UiAutomator2 to be online...`);
257260
this.jwproxy.didInstrumentationExit = false;
261+
try {
262+
await this.stopInstrumentationProcess();
263+
} catch (ign) {}
258264
await this.startInstrumentationProcess();
259265
if (!this.jwproxy.didInstrumentationExit) {
260266
try {
@@ -312,18 +318,30 @@ class UiAutomator2Server {
312318
// Disable Google analytics to prevent possible fatal exception
313319
cmd.push('-e', 'disableAnalytics', 'true');
314320
cmd.push(INSTRUMENTATION_TARGET);
315-
const instrumentationProcess = this.adb.createSubProcess(['shell', ...cmd]);
316-
instrumentationProcess.on('output', (stdout, stderr) => {
317-
const output = _.trim(stdout || stderr);
318-
if (output) {
319-
instrumentationLogger.debug(output);
320-
}
321-
});
322-
instrumentationProcess.on('exit', (code) => {
323-
instrumentationLogger.debug(`The process has exited with code ${code}`);
321+
this.instrumentationProcess = this.adb.createSubProcess(['shell', ...cmd]);
322+
for (const streamName of ['stderr', 'stdout']) {
323+
this.instrumentationProcess.on(`line-${streamName}`, (line) => this.log.debug(`[Instrumentation] ${line}`));
324+
}
325+
this.instrumentationProcess.once('exit', (code, signal) => {
326+
this.log.debug(`[Instrumentation] The process has exited with code ${code}, signal ${signal}`);
324327
this.jwproxy.didInstrumentationExit = true;
325328
});
326-
await instrumentationProcess.start(0);
329+
await this.instrumentationProcess.start(0);
330+
}
331+
332+
async stopInstrumentationProcess () {
333+
if (!this.instrumentationProcess) {
334+
return;
335+
}
336+
337+
try {
338+
if (this.instrumentationProcess.isRunning) {
339+
await this.instrumentationProcess.stop();
340+
}
341+
} finally {
342+
this.instrumentationProcess.removeAllListeners();
343+
this.instrumentationProcess = null;
344+
}
327345
}
328346

329347
async deleteSession () {
@@ -336,6 +354,11 @@ class UiAutomator2Server {
336354
this.log.warn(`Did not get confirmation UiAutomator2 deleteSession worked; ` +
337355
`Error was: ${err}`);
338356
}
357+
try {
358+
await this.stopInstrumentationProcess();
359+
} catch (err) {
360+
this.log.warn(`Could not stop the instrumentation process. Original error: ${err.message}`);
361+
}
339362
}
340363

341364
async cleanupAutomationLeftovers (strictCleanup = false) {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"singleQuote": true
5757
},
5858
"dependencies": {
59-
"appium-adb": "^12.2.0",
59+
"appium-adb": "^12.4.7",
6060
"appium-android-driver": "^9.7.0",
6161
"appium-chromedriver": "^5.6.28",
6262
"appium-uiautomator2-server": "^7.0.14",
@@ -68,7 +68,7 @@
6868
"lodash": "^4.17.4",
6969
"portscanner": "^2.2.0",
7070
"source-map-support": "^0.x",
71-
"teen_process": "^2.0.0",
71+
"teen_process": "^2.2.0",
7272
"type-fest": "^4.4.0"
7373
},
7474
"devDependencies": {

0 commit comments

Comments
 (0)