appium: Failed to launch wda because of "Couldn't start port forwarding on port 8100. Please provide a different port using 'wdaLocalPort' capability" without delete session

This issue happened the current master. 1.14.2 worked.

The problem

Appium fails to create a new session with useNewWDA after the first launching on a real device.

I mean:

  1. send a create session with useNewWDA: true
    • ok
  2. send a create session with useNewWDA: true
[debug] [BaseDriver] Event 'wdaStartFailed' logged at 1566837388273 (01:36:28 GMT+0900 (Japan Standard Time))
[debug] [XCUITest] Unable to launch WebDriverAgent because of xcodebuild failure: Couldn't start port forwarding on port 8100. Please provide a different port using 'wdaLocalPort' capability. Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md. Try to remove the WebDriverAgentRunner application from the device if it is installed and reboot the device.
[XCUITest] Quitting and uninstalling WebDriverAgent
[XCUITest] Shutting down sub-processes
[debug] [XCUITest] Removing WDA application from device
[XCUITest] Error: Unable to launch WebDriverAgent because of xcodebuild failure: Couldn't start port forwarding on port 8100. Please provide a different port using 'wdaLocalPort' capability. Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md. Try to remove the WebDriverAgentRunner application from the device if it is installed and reboot the device.
[XCUITest]     at quitAndUninstall (/Users/kazu/GitHub/appium-xcuitest-driver/lib/driver.js:505:15)
[debug] [XCUITest] Not clearing log files. Use `clearSystemFiles` capability to turn on.
[debug] [BaseDriver] Event 'newSessionStarted' logged at 1566837388558 (01:36:28 GMT+0900 (Japan Standard Time))
[debug] [W3C] Encountered internal error running command: Error: Unable to launch WebDriverAgent because of xcodebuild failure: Couldn't start port forwarding on port 8100. Please provide a different port using 'wdaLocalPort' capability. Make sure you follow the tutorial at https://github.com/appium/appium-xcuitest-driver/blob/master/docs/real-device-config.md. Try to remove the WebDriverAgentRunner application from the device if it is installed and reboot the device.
[debug] [W3C]     at quitAndUninstall (/Users/kazu/GitHub/appium-xcuitest-driver/lib/driver.js:505:15)
[HTTP] <-- POST /wd/hub/session 500 1716 ms - 1293

Another error in step2

[debug] [XCUITest] Killing running processes 'xcodebuild.*242bfcc998fd156df0bd0ad1dde8ab8e0a032114' for the device 242bfcc998fd156df0bd0ad1dde8ab8e0a032114...
[XCUITest] xcodebuild exited with code '75' and signal 'null'
[debug] [XCUITest] 'pgrep -ifn xcodebuild.*242bfcc998fd156df0bd0ad1dde8ab8e0a032114' didn't detect any matching processes. Return code: 1
uncaughtException: listen EADDRINUSE: address already in use :::8100
Error: listen EADDRINUSE: address already in use :::8100
    at Server.setupListenHandle [as _listen2] (net.js:1279:14)
    at listenInCluster (net.js:1327:12)
    at Server.listen (net.js:1414:7)
    at iProxy.listen [as start] (/Users/kazu/GitHub/appium/node_modules/appium-xcuitest-driver/lib/wda/iproxy.js:33:23)
    at WebDriverAgent.start [as createIProxy] (/Users/kazu/GitHub/appium/node_modules/appium-xcuitest-driver/lib/wda/webdriveragent.js:249:14)
    at WebDriverAgent.createIProxy [as launch] (/Users/kazu/GitHub/appium/node_modules/appium-xcuitest-driver/lib/wda/webdriveragent.js:228:28)
[debug] [XCUITest] Successfully updated '/Users/kazu/GitHub/appium/node_modules/appium-webdriveragent/WebDriverAgent.xcodeproj/project.pbxproj' with bundle id 'com.trident.WebDriverAgent'
[debug] [XCUITest] Generating xcode config file for orgId '9U4PT397Q4' and signingId 'iPhone Developer'
[debug] [XCUITest] Writing xcode config file to /var/folders/34/2222sh8n27d6rcp7jqlkw8km0000gn/T/2019727-20152-xx57uh.t0rw/appium-temp.xcconfig

Environment

  • Appium version (or git revision) that exhibits the issue: master (xcuitest-driver was 7f3e52f03b9726eba8a6173de09db5644a3998f9)
  • Last Appium version that did not exhibit the issue (if applicable): 1.14.2
  • Desktop OS/version used to run Appium: macOS 10.14.6
  • Node.js version (unless using Appium.app|exe): 10.16.0
  • Npm or Yarn package manager: npm
  • Mobile platform/version under test:
  • Real device or emulator/simulator: real device
  • Appium CLI or Appium.app|exe:

Details

https://github.com/appium/appium-xcuitest-driver/blob/master/lib/wda/iproxy.js#L42-L48 has regression? (I haven’t dug much yet)

Link to Appium logs

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 28 (18 by maintainers)

Most upvoted comments

With the .NET client you can use:

private static int GetAvailablePort()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);

            listener.Start();
            int port = ((IPEndPoint)listener.LocalEndpoint).Port;
            listener.Stop();

            return port;
        }

I’m sure something similar can be done for the other clients as well (Java, etc)

@KazuCocoa We should just do random port allocation. That would fix the wdaLocalPort issue

@KazuCocoa I will work on a hacky solution to fix it. However, the webdriveragent.js class needs a proper refactor

mmm. https://gist.github.com/KazuCocoa/1aaa162cafdd26c0738f4be94a907c55 is the gist in step2.

I inserted some logs which had ======= in iproxy.js tho.

Someone did not release the port?, so next running iproxy failed to establish the connection, it seemed. After killing appium process and re-launching it, it worked once. destroy probably worked since 8100 looked listening as below.

kazu$ lsof -i :8100
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    27795 kazu   25u  IPv6 0x15860d395258063f      0t0  TCP *:xprint-server (LISTEN)
node    27795 kazu   31u  IPv4 0x15860d39492ab3ff      0t0  TCP localhost:61037->localhost:xprint-server (ESTABLISHED)
node    27795 kazu   32u  IPv6 0x15860d39590b6d3f      0t0  TCP localhost:xprint-server->localhost:61037 (ESTABLISHED)
kazu$ lsof -i :8100 # after this.serverSocket.close()
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    27795 kazu   25u  IPv6 0x15860d395258063f      0t0  TCP *:xprint-server (LISTEN)
kazu$ lsof -i :8100