More reliable SystemPort handling in conflict scenarios
See original GitHub issueThe problem
When doing parallel tests with Android and accidentally using the same systemPort for two different devices it is possible that Appium operates a wrong device and the client/user is not informed about this.
I know that the system port usage is documented here. But if you miss that and have conflicting system port
- In the “best” case something bad happens (e.g. like in #7745) or your tests fail and you see it (e.g. in the screenshots) that appium requests were sent to the wrong device
- In the worst case the appium request are routed to a wrong device without any visible errors (in the logs or responses to the client)
- This can happen e.g. when request is a generic one (e.g. “first switch element should be enabled”) and could cause false positives, that are close to impossible to detect/troubleshoot (because you don’t see any errors anywhere)
So, the proposed fix in short: Detect and raise errors when conflicting system port is used. More detailed fix proposal after the “Details” chapter.
Environment
- Appium version (or git revision) that exhibits the issue: v1.20.2
- Last Appium version that did not exhibit the issue (if applicable): N/A
- Desktop OS/version used to run Appium: Win 10
- Node.js version (unless using Appium.app|exe): N/A
- Npm or Yarn package manager: N/A
- Mobile platform/version under test: Android (UiAutomator2)
- Real device or emulator/simulator: yes
- Appium CLI or Appium.app|exe: no
Details
This is going to be a long one, so please bear with me 😃
I have spent several hours hunting down problems that were caused by conflicting system ports, but the errors I got were somewhat ambiguous - hence this improvement idea.
Here is also a detailed analysis of what happens. This is not really necessary to prove/describe this problem, but could be valuable for others who are looking at the logs/results and are wondering “what the heck is happening here…”
Session creation
1st device: udid=5WH6R20408005356, system port: 8206 Log snippets:
2021-05-11 00:48:51:166 - [debug] [BaseDriver] "appium:udid": "5WH6R20408005356",
...
2021-05-11 00:48:51:166 - [debug] [BaseDriver] "appium:systemPort": 8206,
...
2021-05-11 00:48:51:167 - [BaseDriver] Session created with session id: b767a518-dd03-44c4-854f-a4e2a4657797
...
2021-05-11 00:48:51:585 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 5WH6R20408005356 forward tcp:8206 tcp:6790'
...
2021-05-11 00:49:00:960 - [debug] [W3C (b767a518)] Cached the protocol value 'W3C' for the new session b767a518-dd03-44c4-854f-a4e2a4657797
2021-05-11 00:49:00:960 - [debug] [W3C (b767a518)] Responding to client with driver.createSession() result: {"capabilities":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","dontStopAppOnReset":false,"deviceName":"MsPost3","platformVersion":"9.1","udid":"5WH6R20408005356","autoLaunch":true,"noReset":true,"newCommandTimeout":1800,"skipDeviceInitialization":true,"automationName":"Appium","systemPort":8206,"appPackage":"com.android.settings","appActivity":".HWSettings","optionalIntentArguments":"-W","deviceReadyTimeout":120,"eventTimings":true,"noSign":true},"platformName":"Android","dontStopAppOnReset":false,"deviceName":"5WH6R20408005356","platformVersion":"9","udid":"5WH6R20408005356","autoLaunch":true,"noReset":true,"newCommandTimeout":1800,"skipDeviceInitialization":true,"automationName":"Appium","systemPort":8206,"appPackage":"com.android.settings","appActivity":".HWSettings","optionalIntentArguments":"-W","deviceReadyTimeout":120...
2021-05-11 00:49:00:960 - [HTTP] <-- POST /wd/hub/session 200 9796 ms - 1377
2nd device: udid=9WV4C19218015161, system port: 8206 (same system port) Log snippets:
2021-05-11 00:48:54:103 - [debug] [BaseDriver] "appium:udid": "9WV4C19218015161",
...
2021-05-11 00:48:54:103 - [debug] [BaseDriver] "appium:systemPort": 8206,
...
2021-05-11 00:48:54:105 - [BaseDriver] Session created with session id: a9b57573-202e-4ba9-b86a-4b858b5bf82b
...
2021-05-11 00:48:54:560 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 9WV4C19218015161 forward tcp:8206 tcp:6790'
...
2021-05-11 00:49:01:012 - [debug] [W3C (a9b57573)] Cached the protocol value 'W3C' for the new session a9b57573-202e-4ba9-b86a-4b858b5bf82b
2021-05-11 00:49:01:012 - [debug] [W3C (a9b57573)] Responding to client with driver.createSession() result: {"capabilities":{"platform":"LINUX","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"platformName":"Android","dontStopAppOnReset":false,"deviceName":"MsPost1","platformVersion":"9.1","udid":"9WV4C19218015161","autoLaunch":true,"noReset":true,"newCommandTimeout":1800,"skipDeviceInitialization":true,"automationName":"Appium","systemPort":8206,"appPackage":"com.android.settings","appActivity":".HWSettings","optionalIntentArguments":"-W","deviceReadyTimeout":120,"eventTimings":true,"noSign":true},"platformName":"Android","dontStopAppOnReset":false,"deviceName":"9WV4C19218015161","platformVersion":"9","udid":"9WV4C19218015161","autoLaunch":true,"noReset":true,"newCommandTimeout":1800,"skipDeviceInitialization":true,"automationName":"Appium","systemPort":8206,"appPackage":"com.android.settings","appActivity":".HWSettings","optionalIntentArguments":"-W","deviceReadyTimeout":120...
2021-05-11 00:49:01:012 - [HTTP] <-- POST /wd/hub/session 200 6911 ms - 1377
NOTE:
- No errors/warning in the logs
- No errors returned to the client
Wrong routing = operating wrong device
Based on the logs everything seems to work fine for both devices (both running on 127.0.0.1:8206) But in reality everything is now sent to device udid:9WV4C19218015161, because of these
00:48:51:585 - adb.exe -P 5037 -s 5WH6R20408005356 forward tcp:8206 tcp:6790
--> local port 8206 is forwarded to udid:5WH6R20408005356’s port 679000:48:54:560 - adb.exe -P 5037 -s 9WV4C19218015161 forward tcp:8206 tcp:6790
--> local port 8206 is reassigned to udid:9WV4C19218015161 (and above is forwarding is lost)
but for the client everything looks normal because there are neither errors nor warnings about conflicting system ports.
Even concurrent & overlapping request look okay in the logs, see below.
Some notes to the below log snippets R1 = request 1, R2 = request 2 etc.
Sessions:
- http://127.0.0.1:8206/wd/hub/session/4f700804-0a41-407d-8280-aacd54363597 - belonging to master session: 4b858b5bf82b and udid 9WV4C19218015161
- http://127.0.0.1:8206/wd/hub/session/0645c263-cb0f-427c-8c50-5407cd4b641a - belonging to master session: a4e2a4657797 and udid 5WH6R20408005356
Log snippets:
[R1.req] 2021-05-11 00:49:03:030 - [HTTP] --> POST /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b/elements
[R1.req] 2021-05-11 00:49:03:030 - [HTTP] {"using":"xpath","value":"//android.widget.TextView[starts-with(@text, 'Mobile data')]/../..//android.widget.Switch"}
[R1.req] 2021-05-11 00:49:03:030 - [debug] [W3C (a9b57573)] Calling AppiumDriver.findElements() with args: ["xpath","//android.widget.TextView[starts-with(@text, 'Mobile data')]/../..//android.widget.Switch","a9b57573-202e-4ba9-b86a-4b858b5bf82b"]
[R1.req] 2021-05-11 00:49:03:030 - [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator
[R1.req] 2021-05-11 00:49:03:030 - [debug] [BaseDriver] Waiting up to 0 ms for condition
[R1.req] 2021-05-11 00:49:03:030 - [debug] [WD Proxy] Matched '/elements' to command name 'findElements'
[R1.req] 2021-05-11 00:49:03:030 - [debug] [WD Proxy] Proxying [POST /elements] to [POST http://127.0.0.1:8206/wd/hub/session/4f700804-0a41-407d-8280-aacd54363597/elements] with body: {"strategy":"xpath","selector":"//android.widget.TextView[starts-with(@text, 'Mobile data')]/../..//android.widget.Switch","context":"","multiple":true}
[R2.req] 2021-05-11 00:49:03:377 - [HTTP] --> POST /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/elements
[R2.req] 2021-05-11 00:49:03:377 - [HTTP] {"using":"-android uiautomator","value":"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().className(\"android.widget.Switch\").enabled(true).packageName(\"com.android.settings\").resourceId(\"android:id/switch_widget\"))"}
[R2.req] 2021-05-11 00:49:03:378 - [debug] [W3C (b767a518)] Calling AppiumDriver.findElements() with args: ["-android uiautomator","new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().className(\"android.widget.Switch\").enabled(true).packageName(\"com.android.settings\").resourceId(\"android:id/switch_widget\"))","b767a518-dd03-44c4-854f-a4e2a4657797"]
[R2.req] 2021-05-11 00:49:03:378 - [debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, css selector, -android uiautomator
[R2.req] 2021-05-11 00:49:03:378 - [debug] [BaseDriver] Waiting up to 0 ms for condition
[R2.req] 2021-05-11 00:49:03:378 - [debug] [WD Proxy] Matched '/elements' to command name 'findElements'
[R2.req] 2021-05-11 00:49:03:378 - [debug] [WD Proxy] Proxying [POST /elements] to [POST http://127.0.0.1:8206/wd/hub/session/0645c263-cb0f-427c-8c50-5407cd4b641a/elements] with body: {"strategy":"-android uiautomator","selector":"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().className(\"android.widget.Switch\").enabled(true).packageName(\"com.android.settings\").resourceId(\"android:id/switch_widget\"))","context":"","multiple":true}
[R1.res] 2021-05-11 00:49:03:471 - [debug] [WD Proxy] Got response with status 200: {"sessionId":"4f700804-0a41-407d-8280-aacd54363597","value":[{"ELEMENT":"d854da18-0ee8-46a0-9a4c-a12c8d53168f","element-6066-11e4-a52e-4f735466cecf":"d854da18-0ee8-46a0-9a4c-a12c8d53168f"}]}
[R1.res] 2021-05-11 00:49:03:471 - [debug] [W3C (a9b57573)] Responding to client with driver.findElements() result: [{"element-6066-11e4-a52e-4f735466cecf":"d854da18-0ee8-46a0-9a4c-a12c8d53168f","ELEMENT":"d854da18-0ee8-46a0-9a4c-a12c8d53168f"}]
[R1.res] 2021-05-11 00:49:03:472 - [HTTP] <-- POST /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b/elements 200 442 ms - 139
[R1.res] 2021-05-11 00:49:03:472 - [HTTP]
[R3.req] 2021-05-11 00:49:03:473 - [HTTP] --> GET /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b/element/d854da18-0ee8-46a0-9a4c-a12c8d53168f/attribute/checked
[R3.req] 2021-05-11 00:49:03:473 - [HTTP] {}
[R3.req] 2021-05-11 00:49:03:473 - [debug] [W3C (a9b57573)] Calling AppiumDriver.getAttribute() with args: ["checked","d854da18-0ee8-46a0-9a4c-a12c8d53168f","a9b57573-202e-4ba9-b86a-4b858b5bf82b"]
[R3.req] 2021-05-11 00:49:03:474 - [debug] [WD Proxy] Matched '/element/d854da18-0ee8-46a0-9a4c-a12c8d53168f/attribute/checked' to command name 'getAttribute'
[R3.req] 2021-05-11 00:49:03:474 - [debug] [WD Proxy] Proxying [GET /element/d854da18-0ee8-46a0-9a4c-a12c8d53168f/attribute/checked] to [GET http://127.0.0.1:8206/wd/hub/session/4f700804-0a41-407d-8280-aacd54363597/element/d854da18-0ee8-46a0-9a4c-a12c8d53168f/attribute/checked] with no body
[R3.res] 2021-05-11 00:49:03:499 - [debug] [WD Proxy] Got response with status 200: {"sessionId":"4f700804-0a41-407d-8280-aacd54363597","value":"false"}
[R3.res] 2021-05-11 00:49:03:499 - [debug] [W3C (a9b57573)] Responding to client with driver.getAttribute() result: "false"
[R3.res] 2021-05-11 00:49:03:500 - [HTTP] <-- GET /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b/element/d854da18-0ee8-46a0-9a4c-a12c8d53168f/attribute/checked 200 26 ms - 17
[R3.res] 2021-05-11 00:49:03:500 - [HTTP]
[R2.res] 2021-05-11 00:49:08:188 - [debug] [WD Proxy] Got response with status 200: {"sessionId":"0645c263-cb0f-427c-8c50-5407cd4b641a","value":[]}
[R2.res] 2021-05-11 00:49:08:189 - [debug] [W3C (b767a518)] Responding to client with driver.findElements() result: []
[R2.res] 2021-05-11 00:49:08:189 - [HTTP] <-- POST /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/elements 200 4811 ms - 12
If my tests would stop here I would not have any indication (neither in the logs nor in the responses to the client) that all my appium request targeted to the udid:5WH6R20408005356 were actually sent to udid:9WV4C19218015161
Scenarios that produce errors
The problem with conflicting system ports becomes visible only in certain scenarios (timing and/or sequence of events)
Here are some of those scenarios:
One of the sessions is deleted (Original error: Could not proxy command to the remote server. Original error: socket hang up)
Things start to fail when one of the (conflicting) sessions is deleted
Log snippets:
[R4.req] 2021-05-11 00:49:12:990 - [HTTP] --> POST /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/elements
[R4.req] 2021-05-11 00:49:12:990 - [debug] [W3C (b767a518)] Calling AppiumDriver.findElements() with args: ["-android uiautomator","new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().className(\"android.widget.Switch\").enabled(true).packageName(\"com.android.settings\").resourceId(\"android:id/switch_widget\"))","b767a518-dd03-44c4-854f-a4e2a4657797"]
[R4.req] 2021-05-11 00:49:12:990 - [debug] [WD Proxy] Proxying [POST /elements] to [POST http://127.0.0.1:8206/wd/hub/session/0645c263-cb0f-427c-8c50-5407cd4b641a/elements] with body: {"strategy":"-android uiautomator","selector":"new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().className(\"android.widget.Switch\").enabled(true).packageName(\"com.android.settings\").resourceId(\"android:id/switch_widget\"))","context":"","multiple":true}
#
# DELETE 4b858b5bf82b (WD Proxy: aacd54363597)
#
[R5.req] 2021-05-11 00:49:17:099 - [HTTP] --> DELETE /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b
[R5.req] 2021-05-11 00:49:17:099 - [debug] [W3C (a9b57573)] Calling AppiumDriver.deleteSession() with args: ["a9b57573-202e-4ba9-b86a-4b858b5bf82b"]
[R5.req] 2021-05-11 00:49:17:099 - [Appium] Removing session a9b57573-202e-4ba9-b86a-4b858b5bf82b from our master session list
[R5.req] 2021-05-11 00:49:17:099 - [debug] [WD Proxy] Proxying [DELETE /] to [DELETE http://127.0.0.1:8206/wd/hub/session/4f700804-0a41-407d-8280-aacd54363597] with no body
[R5.res] 2021-05-11 00:49:17:136 - [debug] [WD Proxy] Got response with status 200: {"sessionId":"4f700804-0a41-407d-8280-aacd54363597","value":null}
[R5.res] 2021-05-11 00:49:17:137 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 9WV4C19218015161 shell am force-stop com.android.settings'
#
# The ongoing request is still okay (but on wrong device)
#
[R4.res] 2021-05-11 00:49:17:166 - [debug] [WD Proxy] Got response with status 200: {"sessionId":"0645c263-cb0f-427c-8c50-5407cd4b641a","value":[]}
[R4.res] 2021-05-11 00:49:17:166 - [HTTP] <-- POST /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/elements 200 4176 ms - 12
#
# Status here
# - master session 4b858b5bf82b with the WD Proxy session aacd54363597 are deleted
# - deletion of the WD Proxy session aacd54363597 also killed the WD proxy at 127.0.0.1:8206
# However
# - there's still master session a4e2a4657797 with the WD Proxy session 5407cd4b641a at 127.0.0.1:8206
# - but the WD Proxy at 127.0.0.1:8206 was just killed
#
# --> bad things start to happen
#
# Bad thing #1:
# - requests to master session a4e2a4657797 all fail with "socket hang up"
# - obviously because the WD Proxy at 127.0.0.1:8206 is not there any more (but session a4e2a4657797 still expects it to be there)
#
[R6.req] 2021-05-11 00:49:17:197 - [HTTP] --> GET /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/screenshot
[R6.req] 2021-05-11 00:49:17:197 - [debug] [W3C (b767a518)] Calling AppiumDriver.getScreenshot() with args: ["b767a518-dd03-44c4-854f-a4e2a4657797"]
[R6.req] 2021-05-11 00:49:17:198 - [debug] [WD Proxy] Proxying [GET /screenshot] to [GET http://127.0.0.1:8206/wd/hub/session/0645c263-cb0f-427c-8c50-5407cd4b641a/screenshot] with no body
[R6.res] 2021-05-11 00:49:17:200 - [WD Proxy] socket hang up
[R6.res] 2021-05-11 00:49:17:202 - [debug] [W3C (b767a518)] Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
[R6.res] 2021-05-11 00:49:17:202 - [debug] [W3C (b767a518)] at UIA2Proxy.command (c:\ta\appium\v1.20.2\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:275:13)
[R6.res] 2021-05-11 00:49:17:202 - [debug] [W3C (b767a518)] at runMicrotasks (<anonymous>)
[R6.res] 2021-05-11 00:49:17:202 - [debug] [W3C (b767a518)] at processTicksAndRejections (internal/process/task_queues.js:93:5)
[R6.res] 2021-05-11 00:49:17:202 - [debug] [W3C (b767a518)] at AndroidUiautomator2Driver.commands.getScreenshot (c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-driver\lib\commands\screenshot.js:14:10)
[R6.res] 2021-05-11 00:49:17:204 - [HTTP] <-- GET /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797/screenshot 500 7 ms - 790
# Client:
00:49:17.205 An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server.
Original error: socket hang up
#
# DELETE 4b858b5bf82b (WD Proxy: aacd54363597) - is completed and response is sent to client
#
[R5.res] 2021-05-11 00:49:17:245 - [debug] [Logcat] Stopping logcat capture
[R5.res] 2021-05-11 00:49:17:248 - [debug] [ADB] Removing forwarded port socket connection: 8206
[R5.res] 2021-05-11 00:49:17:248 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 9WV4C19218015161 forward --remove tcp:8206'
[R5.res] 2021-05-11 00:49:17:265 - [UiAutomator2] Restoring hidden api policy to the device default configuration
[R5.res] 2021-05-11 00:49:17:265 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 9WV4C19218015161 shell 'settings delete global hidden_api_policy_pre_p_apps;settings delete global hidden_api_policy_p_apps;settings delete global hidden_api_policy''
[R5.res] 2021-05-11 00:49:17:431 - [debug] [BaseDriver] Event 'quitSessionFinished' logged at 1620686957431 (00:49:17 GMT+0200 (stredoeurópsky letný čas))
[R5.res] 2021-05-11 00:49:17:431 - [debug] [W3C (a9b57573)] Received response: null
[R5.res] 2021-05-11 00:49:17:431 - [debug] [W3C (a9b57573)] But deleting session, so not returning
[R5.res] 2021-05-11 00:49:17:431 - [debug] [W3C (a9b57573)] Responding to client with driver.deleteSession() result: null
[R5.res] 2021-05-11 00:49:17:432 - [HTTP] <-- DELETE /wd/hub/session/a9b57573-202e-4ba9-b86a-4b858b5bf82b 200 333 ms - 14
Not possible to create new session (Original error: The instrumentation process cannot be initialized within 30000ms timeout.)
New sessions (using the same system port) cannot be created and log shows “socket hang up” and “ECONNREFUSED” errors
Log snippets:
# Bad thing #2:
# - everything looks good until the "[WD Proxy] Proxying [GET /status]" step which fails with "socket hang up"
#
2021-05-11 00:49:17:562 - [HTTP] --> POST /wd/hub/session
...
2021-05-11 00:49:17:566 - [BaseDriver] Session created with session id: 9fae6a30-8a1d-40c2-af74-6f678221ce23
...
2021-05-11 00:49:19:028 - [UiAutomator2] Starting UIAutomator2 server 4.17.4
2021-05-11 00:49:19:028 - [UiAutomator2] Using UIAutomator2 server from 'c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-server\apks\appium-uiautomator2-server-v4.17.4.apk' and test from 'c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-server\apks\appium-uiautomator2-server-debug-androidTest.apk'
2021-05-11 00:49:19:028 - [UiAutomator2] Waiting up to 30000ms for UiAutomator2 to be online...
2021-05-11 00:49:19:028 - [debug] [ADB] Creating ADB subprocess with args: ["-P",5037,"-s","9WV4C19218015161","shell","am","instrument","-w","io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner"]
2021-05-11 00:49:19:032 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:19:032 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:19:038 - [WD Proxy] socket hang up
2021-05-11 00:49:20:039 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:20:039 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:20:042 - [WD Proxy] socket hang up
2021-05-11 00:49:21:042 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:21:043 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:21:044 - [debug] [Instrumentation] io.appium.uiautomator2.server.test.AppiumUiAutomator2Server:
2021-05-11 00:49:21:046 - [WD Proxy] socket hang up
#
# This continues until that "zombie" session is closed:
# - closing the remaining master session a4e2a4657797 (with the WD Proxy session 5407cd4b641a at 127.0.0.1:8206)
# shows some errors, but they are handled correctly
#
2021-05-11 00:49:21:573 - [HTTP] --> DELETE /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797
2021-05-11 00:49:21:573 - [HTTP] {}
2021-05-11 00:49:21:573 - [debug] [W3C (b767a518)] Calling AppiumDriver.deleteSession() with args: ["b767a518-dd03-44c4-854f-a4e2a4657797"]
2021-05-11 00:49:21:573 - [debug] [BaseDriver] Event 'quitSessionRequested' logged at 1620686961573 (00:49:21 GMT+0200 (stredoeurópsky letný čas))
2021-05-11 00:49:21:573 - [Appium] Removing session b767a518-dd03-44c4-854f-a4e2a4657797 from our master session list
2021-05-11 00:49:21:573 - [debug] [UiAutomator2] Deleting UiAutomator2 session
2021-05-11 00:49:21:573 - [debug] [UiAutomator2] Deleting UiAutomator2 server session
2021-05-11 00:49:21:573 - [debug] [WD Proxy] Matched '/' to command name 'deleteSession'
2021-05-11 00:49:21:573 - [debug] [WD Proxy] Proxying [DELETE /] to [DELETE http://127.0.0.1:8206/wd/hub/session/0645c263-cb0f-427c-8c50-5407cd4b641a] with no body
2021-05-11 00:49:21:578 - [WD Proxy] socket hang up
2021-05-11 00:49:21:578 - [UiAutomator2] Did not get confirmation UiAutomator2 deleteSession worked; Error was: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
2021-05-11 00:49:21:578 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 5WH6R20408005356 shell am force-stop com.android.settings'
2021-05-11 00:49:21:825 - [debug] [Logcat] Stopping logcat capture
2021-05-11 00:49:21:826 - [debug] [ADB] Removing forwarded port socket connection: 8206
2021-05-11 00:49:21:826 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 5WH6R20408005356 forward --remove tcp:8206'
2021-05-11 00:49:21:842 - [UiAutomator2] Restoring hidden api policy to the device default configuration
2021-05-11 00:49:21:842 - [debug] [ADB] Running 'c:\ta\droid\sdk\platform-tools\adb.exe -P 5037 -s 5WH6R20408005356 shell 'settings delete global hidden_api_policy_pre_p_apps;settings delete global hidden_api_policy_p_apps;settings delete global hidden_api_policy''
2021-05-11 00:49:22:125 - [debug] [BaseDriver] Event 'quitSessionFinished' logged at 1620686962125 (00:49:22 GMT+0200 (stredoeurópsky letný čas))
2021-05-11 00:49:22:125 - [debug] [W3C (b767a518)] Received response: null
2021-05-11 00:49:22:125 - [debug] [W3C (b767a518)] But deleting session, so not returning
2021-05-11 00:49:22:125 - [debug] [W3C (b767a518)] Responding to client with driver.deleteSession() result: null
2021-05-11 00:49:22:126 - [HTTP] <-- DELETE /wd/hub/session/b767a518-dd03-44c4-854f-a4e2a4657797 200 553 ms - 14
2021-05-11 00:49:22:126 - [HTTP]
#
# After this the earlier "socker hang up" error changes to ECONNREFUSED
# (makes sense because we have just closed the socket = port 8026 is not open anymore)
#
2021-05-11 00:49:23:049 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:23:049 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:23:050 - [WD Proxy] connect ECONNREFUSED 127.0.0.1:8206
2021-05-11 00:49:24:051 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:24:051 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:24:052 - [WD Proxy] connect ECONNREFUSED 127.0.0.1:8206
2021-05-11 00:49:25:052 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:25:052 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:25:053 - [WD Proxy] connect ECONNREFUSED 127.0.0.1:8206
2021-05-11 00:49:26:054 - [debug] [WD Proxy] Matched '/status' to command name 'getStatus'
2021-05-11 00:49:26:054 - [debug] [WD Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8206/wd/hub/status] with no body
2021-05-11 00:49:26:055 - [WD Proxy] connect ECONNREFUSED 127.0.0.1:8206
#
# eventually the "POST /wd/hub/session" for the new session (6f678221ce23) times out
#
2021-05-11 00:49:49:390 - [debug] [BaseDriver] Event 'newSessionStarted' logged at 1620686989390 (00:49:49 GMT+0200 (stredoeurópsky letný čas))
2021-05-11 00:49:49:391 - [debug] [W3C] Encountered internal error running command: Error: The instrumentation process cannot be initialized within 30000ms timeout. Make sure the application under test does not crash and investigate the logcat output. You could also try to increase the value of 'uiautomator2ServerLaunchTimeout' capability
2021-05-11 00:49:49:391 - [debug] [W3C] at Object.errorAndThrow (c:\ta\appium\v1.20.2\node_modules\appium-support\lib\logging.js:94:35)
2021-05-11 00:49:49:391 - [debug] [W3C] at UiAutomator2Server.startSession (c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-driver\lib\uiautomator2.js:230:15)
2021-05-11 00:49:49:391 - [debug] [W3C] at runMicrotasks (<anonymous>)
2021-05-11 00:49:49:391 - [debug] [W3C] at processTicksAndRejections (internal/process/task_queues.js:93:5)
2021-05-11 00:49:49:391 - [debug] [W3C] at AndroidUiautomator2Driver.startUiAutomator2Session (c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-driver\lib\driver.js:407:5)
2021-05-11 00:49:49:391 - [debug] [W3C] at AndroidUiautomator2Driver.createSession (c:\ta\appium\v1.20.2\node_modules\appium-uiautomator2-driver\lib\driver.js:224:7)
2021-05-11 00:49:49:391 - [debug] [W3C] at AppiumDriver.createSession (c:\ta\appium\v1.20.2\lib\appium.js:371:35)
2021-05-11 00:49:49:392 - [HTTP] <-- POST /wd/hub/session 500 31830 ms - 983
2021-05-11 00:49:49:392 - [HTTP]
# Client:
00:49:49.444 An unknown server-side error occurred while processing the command.
Original error: The instrumentation process cannot be initialized within 30000ms timeout.
Make sure the application under test does not crash and investigate the logcat output.
You could also try to increase the value of 'uiautomator2ServerLaunchTimeout' capability
Some Proposals for the fix
IMHO the fix should be as simple as this: Check the result of the “adb … forward tcp:8206 tcp:6790” command. Note: it is not enough to check the exit code, because adb is notiriously bad in this ADB always returns 0, regardless of the port status, at least on Win10 For example:
> adb -s 7BKDU17722004638 forward tcp:8206 tcp:30086
8206
> echo %ERRORLEVEL%
0
> adb -s ce10171a6078523a03 forward tcp:8206 tcp:81
> echo %ERRORLEVEL%
0
But in case of a successful execution, the output contains the port number i.e.
> adb -s 7BKDU17722004638 forward tcp:8206 tcp:30086
8206
>
… and in case of a failure, the output is empty i.e.
> adb -s ce10171a6078523a03 forward tcp:8206 tcp:81
>
… but that’s a bit hackish solution (and sounds like something that could change on ADB side)
Other options:
- The WD Proxy does not seem to verify the session ID = do this and fail if there’s no such session e.g. in the above logs you can see that
- udid:5WH6R20408005356 has WD Proxy session "http://127.0.0.1:8206/wd/hub/session...5407cd4b641a
- udid:9WV4C19218015161 has WD Proxy session "http://127.0.0.1:8206/wd/hub/session...aacd54363597
- and the requests for 5WH6R20408005356 and routed to 9WV4C19218015161 which does not have a session for “5407cd4b641a” and could fail
- cache the systemPort+udid combinations and fail if there’s a session request with already used systemPort but with a different udid
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (1 by maintainers)
Top GitHub Comments
I did some improvements for several scenarios there, for example https://github.com/appium/appium-uiautomator2-driver/blob/c25a47e7932b8bb6613eabea512e80053bc2dca2/lib/driver.js#L285, but in general it’s quite hard to handle, because parallel sessions could be executed in scope of a single or multiple node/appium processes. Also, we don’t want to have too much logic on the driver side - it’s a user’s responsibility to configure one or more sessions properly and assign devices to them. Nevertheless you are welcome to create a PR. We’d be happy to review it
Published the patch to appium@beta