testingautomated-testsappiumui-automationappium-android

Appium Error: Cannot find any free port in range 8200..8299


I'm running about 90 tests in Appium (Android emulator on iMac) and it was all fine until suddenly I started observing this error:

Starting logs capture with command: /Users/username/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 logcat -v threadtime
     E       selenium.common.exceptions.WebDriverException: Message: An unknown server-side
 error occurred while processing the command. Original error: Cannot find any free port in range
 8200..8299}. Please set the available port number by providing the systemPort capability or
 double check the processes that are locking ports within this range and terminate these which
 are not needed anymore

I did a few things to fix this problem but nothing worked:

1.

adb kill-server    
adb reconnect
  1. I did clean the emulator and restarted too.

  2. Apart from this, I didn't find any port from 8200-8299 which is already being used in the system.

  3. I did add systemPort capability as well but still I see the same error.

I have no idea how to fix this.

UPDATE:

Found some more logs and figured out that the port forwarding isn't being cleared by UIAutomator 2 (or adb) that's why I don't have issues with iOS but have issued in Android only. Here are the logs which is at the end of the appium server:

[debug] [35m[WD Proxy] [39m Proxying [DELETE /] to [DELETE http://127.0.0.1:8200/wd/hub/session/d1f94433-2c44-4dac-a836-461ab7f41130] with no body
[debug] [35m[UiAutomator2] [39m Deleting UiAutomator2 server session
[debug] [35m[WD Proxy] [39m Matched '/' to command name 'deleteSession'
[debug] [35m[WD Proxy] [39m Proxying [DELETE /] to [DELETE http://127.0.0.1:8201/wd/hub/session/37137b29-a9a6-4d83-b2d9-ce510f601a2d] with no body
[debug] [35m[UiAutomator2] [39m Deleting UiAutomator2 server session

where 127.0.0.1:8201 goes upto 127.0.0.1:8299 and deletes 100 active sessions which I don't expect.

Also, in netstat output I do see that TCP ports 127.0.0.1:8200 - 127.0.0.1:8299 (LISTEN) are open


Solution

  • Execute:

    adb -s $UDID forward --remove-all
    

    just before launching appium and after to ensure that the ports used by the adb are free

    see How do I stop an adb port forward?