Search code examples
appiumadbportappium-android

How do I make sure ports 8200..8299 for UIAutmator2 are free'd up after killing Appium?


I'm running Appium 24/7 on a Windows machine to perform automated tests. I'm running these tests via AVD on an emulator with Android 10. For my test, I give the correct Capabilities and runs functionally great. But after a few hours/days I get the follow error:

UnknownError: 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 at getResponseForW3CError at asyncHandler.

When my code is executed, I make sure the finally in try executes the AppiumDriverLocalService.stop(); method to kill Appium. My feeling says this does somehow not free up any ports for UIAutomator2 (which you can configure, but I have not configured, through systemPort capability.

    } finally {
        driver.closeApp();
        service.stopServer();
        System.exit(0);
    }

Why does Appium not free up any ports for UIAutomator2 in the range 8000-8299 after stopping the service?

  • Android Debug Bridge version 1.0.40
  • Appium version 1.20.2
  • Android 10

PS I know of the adb command adb -s $UDID forward --remove-all but does this solve the real issue, I don't want to execute a Java Process each time

Update - Fix 2-3

I have fixed the issue by changing the finally clause to execute the following methods in order:

    } finally {
        AndroidDriver.quit();
        AppiumDriverLocalService.stopServer();

In order to free up the port 8200..8299, AndroidDriver must be quit as UIAutomator2 is linked to this. The AppiumDriverLocalService will kill/forward/free this port and UIAutomator2, after this I can freely stop the server:

[ADB] Removing forwarded port socket connection: 8203


Solution

  • This issue is a good example of your problem. As you can see, Appium team is closing it as a Not A Bug.

    Reason is obvious: Appium is not responsible for managing ports, it relies that your system has some free in required (by Appium code) range.

    systemPort is used to connect to appium-uiautomator2-server, by default is 8200. Basically Appium selects one port from 8200 to 8299 for appium-uiautomator2-server. However, when you run tests in parallel, you must adjust the port to avoid conflicts. If you are not, you should be safe in 99% cases with the default.

    Restart the system (assuming it will clear ports) and run tests. Check Appium server logs for any errors on closing session. It could be that issue is related to ADB setup or permission issue, and UI2 server is not stopped properly.

    If so, port remains in use. And it is not Appium to deal with cleanup. I'm not sure that adb command you mentioned is a good 100% guaranteed way to do it. I would just add a shell script that is executed before Appium server start to clean ports in 8200-8299 range and forget this problem.