From f4ca5ee2c542479e6acc990f551c4740d3ddd197 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 23 Feb 2024 19:22:28 +0100 Subject: [PATCH 1/2] fix: Properly handle WDA session startup errors (#2331) --- lib/commands/proxy-helper.js | 22 +++++++------- lib/driver.js | 58 ++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/lib/commands/proxy-helper.js b/lib/commands/proxy-helper.js index 712b60c4b..015c6813d 100644 --- a/lib/commands/proxy-helper.js +++ b/lib/commands/proxy-helper.js @@ -104,19 +104,19 @@ export default { } this.log.debug(`Setting custom timeout to ${timeout} ms for '${cmdName}' command`); - let isCommandExpired = false; - const res = await B.resolve(proxy.command(url, method, body)) - .timeout(timeout) - .catch(B.Promise.TimeoutError, () => { - isCommandExpired = true; - }); - if (isCommandExpired) { + try { + return /** @type {TRes} */ (await B.resolve(proxy.command(url, method, body)).timeout(timeout)); + } catch (e) { + if (!(e instanceof B.Promise.TimeoutError)) { + throw e; + } proxy.cancelActiveRequests(); - const errMsg = `Appium did not get any response from '${cmdName}' command in ${timeout} ms`; - await this.startUnexpectedShutdown(new errors.TimeoutError(errMsg)); - this.log.errorAndThrow(errMsg); + const error = new errors.TimeoutError( + `Appium did not get any response from '${cmdName}' command in ${timeout} ms` + ); + await this.startUnexpectedShutdown(error); + throw error; } - return /** @type {TRes} */ (res); }, }; diff --git a/lib/driver.js b/lib/driver.js index 22bbd2b31..ce7d2fd2d 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -1,7 +1,7 @@ import IDB from 'appium-idb'; import {getSimulator} from 'appium-ios-simulator'; import {WebDriverAgent} from 'appium-webdriveragent'; -import {BaseDriver, DeviceSettings} from 'appium/driver'; +import {BaseDriver, DeviceSettings, errors} from 'appium/driver'; import {fs, mjpeg, util, timing} from 'appium/support'; import AsyncLock from 'async-lock'; import {retry, retryInterval} from 'asyncbox'; @@ -773,14 +773,11 @@ class XCUITestDriver extends BaseDriver { // local helper for the two places we need to uninstall wda and re-start it const quitAndUninstall = async (msg) => { this.log.debug(msg); - if (this.opts.webDriverAgentUrl) { + const noUninstallCap = ['webDriverAgentUrl', 'usePreinstalledWDA'] + .find((x) => Boolean(this.opts[x])); + if (noUninstallCap) { this.log.debug( - 'Not quitting/uninstalling WebDriverAgent since webDriverAgentUrl capability is provided', - ); - throw new Error(msg); - } else if (this.opts.usePreinstalledWDA) { - this.log.debug( - 'Not uninstalling WebDriverAgent since this.opts.usePreinstalledWDA capability is provided', + `Not quitting/uninstalling WebDriverAgent since ${noUninstallCap} capability is provided`, ); throw new Error(msg); } @@ -810,8 +807,10 @@ class XCUITestDriver extends BaseDriver { `These values can be customized by changing wdaStartupRetries/wdaStartupRetryInterval capabilities`, ); } - let retryCount = 0; + /** @type {Error|null} */ + let shortCircuitError = null; + let retryCount = 0; await retryInterval(startupRetries, startupRetryInterval, async () => { this.logEvent('wdaStartAttempted'); if (retryCount > 0) { @@ -880,34 +879,25 @@ class XCUITestDriver extends BaseDriver { this.proxyReqRes = this.wda.proxyReqRes.bind(this.wda); this.jwpProxyActive = true; - let originalStacktrace = null; try { - await retryInterval(15, 1000, async () => { - this.logEvent('wdaSessionAttempted'); - this.log.debug('Sending createSession command to WDA'); - try { - this.cachedWdaStatus = - this.cachedWdaStatus || (await this.proxyCommand('/status', 'GET')); - await this.startWdaSession(this.opts.bundleId, this.opts.processArguments); - } catch (err) { - originalStacktrace = err.stack; - this.log.debug(`Failed to create WDA session (${err.message}). Retrying...`); - throw err; - } - }); + this.logEvent('wdaSessionAttempted'); + this.log.debug('Sending createSession command to WDA'); + this.cachedWdaStatus = this.cachedWdaStatus || (await this.proxyCommand('/status', 'GET')); + await this.startWdaSession(this.opts.bundleId, this.opts.processArguments); this.logEvent('wdaSessionStarted'); } catch (err) { - if (originalStacktrace) { - this.log.debug(originalStacktrace); + this.logEvent('wdaSessionFailed'); + this.log.debug(err.stack); + if (err instanceof errors.TimeoutError) { + // Session startup timed out. There is no point to retry + shortCircuitError = err; + return; } - let errorMsg = `Unable to start WebDriverAgent session because of xcodebuild failure: ${err.message}`; - if (this.isRealDevice()) { - errorMsg += - ` Make sure you follow the tutorial at ${WDA_REAL_DEV_TUTORIAL_URL}. ` + - `Try to remove the WebDriverAgentRunner application from the device if it is installed ` + - `and reboot the device.`; + let errorMsg = `Unable to start WebDriverAgent session because of an unexpected failure: ${err.message}`; + if (this.isRealDevice() && _.includes(err.message, 'xcodebuild')) { + errorMsg += ` Make sure you follow the tutorial at ${WDA_REAL_DEV_TUTORIAL_URL}.`; } - await quitAndUninstall(errorMsg); + throw new Error(errorMsg); } if (this.opts.clearSystemFiles && this.isXcodebuildNeeded()) { @@ -919,6 +909,10 @@ class XCUITestDriver extends BaseDriver { this.wda.fullyStarted = true; this.logEvent('wdaStarted'); }); + + if (shortCircuitError) { + throw shortCircuitError; + } }); } From 4da9ab022a222e7de423d24af18eee75ea0e0628 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 23 Feb 2024 18:24:00 +0000 Subject: [PATCH 2/2] chore(release): 7.1.2 [skip ci] ## [7.1.2](https://github.com/appium/appium-xcuitest-driver/compare/v7.1.1...v7.1.2) (2024-02-23) ### Bug Fixes * Properly handle WDA session startup errors ([#2331](https://github.com/appium/appium-xcuitest-driver/issues/2331)) ([f4ca5ee](https://github.com/appium/appium-xcuitest-driver/commit/f4ca5ee2c542479e6acc990f551c4740d3ddd197)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16a01fb2a..aafa8ff9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [7.1.2](https://github.com/appium/appium-xcuitest-driver/compare/v7.1.1...v7.1.2) (2024-02-23) + + +### Bug Fixes + +* Properly handle WDA session startup errors ([#2331](https://github.com/appium/appium-xcuitest-driver/issues/2331)) ([f4ca5ee](https://github.com/appium/appium-xcuitest-driver/commit/f4ca5ee2c542479e6acc990f551c4740d3ddd197)) + ## [7.1.1](https://github.com/appium/appium-xcuitest-driver/compare/v7.1.0...v7.1.1) (2024-02-23) diff --git a/package.json b/package.json index d7b67ee9a..face81e4a 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "xcuitest", "xctest" ], - "version": "7.1.1", + "version": "7.1.2", "author": "Appium Contributors", "license": "Apache-2.0", "repository": {