Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel usage results in a "socket hang up" error. systemPort have set but still have the problem. #903

Open
Underson888 opened this issue Aug 16, 2023 · 7 comments

Comments

@Underson888
Copy link

Here's my code:

python

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import threading

Device 1's configuration

desired_caps1 = {
"appium:appActivity": ".Settings",
"appium:appPackage": "com.android.settings",
"appium:automationName": "uiautomator2",
"appium:udid": "xxxx",
"platformName": "Android",
"appium:systemPort": 8310
}

Device 2's configuration

desired_caps2 = {
"appium:appActivity": ".Settings",
"appium:appPackage": "com.android.settings",
"appium:automationName": "uiautomator2",
"appium:udid": "192.168.137.58:5555",
"platformName": "Android",
"appium:systemPort": 8322
}

def run_test_on_device(desired_caps):
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # Corrected URL

width = driver.get_window_size()["width"]
height = driver.get_window_size()["height"]

action = TouchAction(driver)
action.tap(x=width // 2, y=height // 2).perform()

driver.quit()

Use threading for parallel execution

thread1 = threading.Thread(target=run_test_on_device, args=(desired_caps1,))
thread2 = threading.Thread(target=run_test_on_device, args=(desired_caps2,))

thread1.start()
thread2.start()

thread1.join()
thread2.join()

I have set the different systemPort for each devices, but I still have the problem.

How can I sove that, I'm very need to be solved, thanks.

@Underson888
Copy link
Author

The error information is as follows:
Exception in thread Thread-2:
Traceback (most recent call last):
File "D:\allAi\environments\tiktok\lib\threading.py", line 980, in _bootstrap_inner
self.run()
File "D:\allAi\environments\tiktok\lib\threading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "D:\tiktok\control\testParallel\test.py", line 33, in run_test_on_device
driver = webdriver.Remote('http://127.0.0.1:4723', desired_caps)
File "D:\allAi\environments\tiktok\lib\site-packages\appium\webdriver\webdriver.py", line 257, in init
super().init(
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in init
self.start_session(capabilities)
File "D:\allAi\environments\tiktok\lib\site-packages\appium\webdriver\webdriver.py", line 346, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, w3c_caps)
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
self.error_handler.check_response(response)
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: 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
Stacktrace:
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
at UIA2Proxy.command (C:\Users\12624\AppData\Roaming\npm\node_modules\appium\node_modules@appium\base-driver\lib\jsonwp-proxy\proxy.js:355:13)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at AndroidUiautomator2Driver.commands.getDevicePixelRatio (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\commands\viewport.js:14:10)
at AndroidUiautomator2Driver.fillDeviceDetails (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\driver.js:256:28)
at AndroidUiautomator2Driver.createSession (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\driver.js:242:7)
at AppiumDriver.createSession (C:\Users\12624\AppData\Roaming\npm\node_modules\appium\lib\appium.js:352:35)
Exception in thread Thread-1:
Traceback (most recent call last):
File "D:\allAi\environments\tiktok\lib\threading.py", line 980, in _bootstrap_inner
self.run()
File "D:\allAi\environments\tiktok\lib\threading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "D:\tiktok\control\testParallel\test.py", line 33, in run_test_on_device
driver = webdriver.Remote('http://127.0.0.1:4723', desired_caps)
File "D:\allAi\environments\tiktok\lib\site-packages\appium\webdriver\webdriver.py", line 257, in init
super().init(
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 206, in init
self.start_session(capabilities)
File "D:\allAi\environments\tiktok\lib\site-packages\appium\webdriver\webdriver.py", line 346, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, w3c_caps)
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 345, in execute
self.error_handler.check_response(response)
File "D:\allAi\environments\tiktok\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: 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
Stacktrace:
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
at UIA2Proxy.command (C:\Users\12624\AppData\Roaming\npm\node_modules\appium\node_modules@appium\base-driver\lib\jsonwp-proxy\proxy.js:355:13)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at AndroidUiautomator2Driver.commands.getDevicePixelRatio (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\commands\viewport.js:14:10)
at AndroidUiautomator2Driver.fillDeviceDetails (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\driver.js:256:28)
at AndroidUiautomator2Driver.createSession (C:\Users\12624.appium\node_modules\appium-uiautomator2-driver\lib\driver.js:242:7)
at AppiumDriver.createSession (C:\Users\12624\AppData\Roaming\npm\node_modules\appium\lib\appium.js:352:35)

@Underson888 Underson888 changed the title Parallel usage results in a "socket hang up" error. Parallel usage results in a "socket hang up" error. systemPort have set but still have the problem. Aug 16, 2023
@mykola-mokhnach mykola-mokhnach transferred this issue from appium/appium Aug 16, 2023
@KazuCocoa
Copy link
Member

Could you share the appium log as well? (as GIST)

@Underson888
Copy link
Author

Could you share the appium log as well? (as GIST)

Ok,bro. This is the link:https://gist.github.com/Underson888/8871bca07b3241d7aa6f810e9a7bc16c

@KazuCocoa
Copy link
Member

Did they work with a single thread? The port config itself looks working

@Underson888
Copy link
Author

Did they work with a single thread? The port config itself looks working

Yes,each device is controlled by a single thread,just like the code mentioned.

@Underson888
Copy link
Author

Did they work with a single thread? The port config itself looks working

thread1 = threading.Thread(target=run_test_on_device, args=(desired_caps1,))
thread2 = threading.Thread(target=run_test_on_device, args=(desired_caps2,))

thread1.start()
thread2.start()

thread1.join()
thread2.join()

Is it correct for the driver?

@molokov-klim
Copy link

molokov-klim commented Dec 9, 2024

I understand that the question was asked a long time ago. But maybe I can help someone with my comment. At least it would have been very useful to me earlier.
Below I have provided the contents of the project that deploys selenium server and nodes for parallel testing (sorry that yml is not github, but you can easily remake it).
It is precisely multiple instances of appium that allow you to avoid the socket hang up error.

I reduced the number of nodes for text compression and changed some variables (for example, username and IP addresses)

File structure

appium
  - appium-1.service
  - appium-1.yml
  - appium-2.service
  - appium-2.yml
hub
  - hub.service
node
  - node-1.service
  - node-1.toml
  - node-2.service
  - node-2.toml
.gitlab-ci.yml
selenium-server-4.26.0.jar

gitlab-ci.yml

stages:
  - deploy

deploy:
  image: "alpine"
  stage: deploy
  when: manual
  before_script:
    - apk add --no-cache openssh-client
    - apk add --no-cache sshpass
  script:
    # dont tuch
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S rm -rf /home/user/appium_grid/*"
    - sshpass -p "$SSH_PASSWORD" scp -o StrictHostKeyChecking=no -r appium $SSH_USER@$SSH_HOST:/home/user/appium_grid/
    - sshpass -p "$SSH_PASSWORD" scp -o StrictHostKeyChecking=no -r node $SSH_USER@$SSH_HOST:/home/user/appium_grid/
    - sshpass -p "$SSH_PASSWORD" scp -o StrictHostKeyChecking=no -r hub $SSH_USER@$SSH_HOST:/home/user/appium_grid/
    - sshpass -p "$SSH_PASSWORD" scp -o StrictHostKeyChecking=no selenium-server-4.26.0.jar $SSH_USER@$SSH_HOST:/home/user/appium_grid/selenium-server-4.26.0.jar
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "chmod 777 /home/user/appium_grid/*"

    # add here your appium-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S cp -f /home/user/appium_grid/appium/appium-1.service /etc/systemd/system/appium-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S cp -f /home/user/appium_grid/appium/appium-2.service /etc/systemd/system/appium-2.service"

    # add here your node-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S cp -f /home/user/appium_grid/node/node-1.service /etc/systemd/system/node-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S cp -f /home/user/appium_grid/node/node-2.service /etc/systemd/system/node-2.service"

    # dont tuch
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S cp -f /home/user/appium_grid/hub/hub.service /etc/systemd/system/hub.service"
    # dont tuch
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl daemon-reload"

    # add here your appium-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl enable appium-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl enable appium-2.service"

    # add here your node-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl enable node-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl enable node-2.service"

    # dont tuch
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl enable hub.service"

    # add here your appium-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl restart appium-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl restart appium-2.service"

    # add here your node-N.service
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl restart node-1.service"
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl restart node-2.service"

    # dont tuch
    - sshpass -p "$SSH_PASSWORD" ssh -o StrictHostKeyChecking=no $SSH_USER@$SSH_HOST "echo $SSH_PASSWORD | sudo -S systemctl restart hub.service"
    - sleep 30  # dont ask

hub.service

[Unit]
Description=Selenium Hub
After=network.target

[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
ExecStart=java -jar /home/user/appium_grid/selenium-server-4.26.0.jar hub --port 3334
User=user
Restart=always
RestartSec=3
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=selenium-hub
WorkingDirectory=/home/user/appium_grid/node

[Install]
WantedBy=multi-user.target

appium-1.service

[Unit]
Description=Appium Daemon 1
After=network.target

[Service]
Environment="ANDROID_HOME=/usr/lib/android-sdk"
Environment="ANDROID_SDK_ROOT=/usr/lib/android-sdk"
ExecStart=/usr/local/bin/appium --allow-insecure=adb_shell --relaxed-security --config /home/user/appium_grid/appium/appium-1.yml
User=user
Restart=always
RestartSec=3
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=appium-server-1
WorkingDirectory=/home/user/appium_grid/appium

[Install]
WantedBy=multi-user.target

appium-1.yml

server:
  port: 4714
  use-drivers:
    - uiautomator2
  default-capabilities:
    appium:wdaLocalPort: 8110
    appium:mjpegServerPort: 9110
    appium:mjpegScreenshotUrl: "http://localhost:9110"
    appium:allow-insecure:
      - adb_shell
    appium:relaxed-security: true
    appium:log-level: debug
    appium:udid: 10.11.111.52:5554

appium-2.service

[Unit]
Description=Appium Daemon 2
After=network.target

[Service]
Environment="ANDROID_HOME=/usr/lib/android-sdk"
Environment="ANDROID_SDK_ROOT=/usr/lib/android-sdk"
ExecStart=/usr/local/bin/appium --allow-insecure=adb_shell --relaxed-security --config /home/user/appium_grid/appium/appium-2.yml
User=user
Restart=always
RestartSec=3
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=appium-server-2
WorkingDirectory=/home/user/appium_grid/appium

[Install]
WantedBy=multi-user.target

appium-2.yml

server:
  port: 4724
  use-drivers:
    - uiautomator2
  default-capabilities:
    appium:wdaLocalPort: 8120
    appium:mjpegServerPort: 9120
    appium:mjpegScreenshotUrl: "http://localhost:9120"
    appium:allow-insecure:
      - adb_shell
    appium:relaxed-security: true
    appium:log-level: debug
    appium:udid: 10.22.222.200:5554

node-1.service

[Unit]
Description=Selenium Node Daemon 1
After=network.target

[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
ExecStart=java -jar /home/user/appium_grid/selenium-server-4.26.0.jar node --config /home/user/appium_grid/node/node-1.toml
User=user
Restart=always
RestartSec=3
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=selenium-node-1
WorkingDirectory=/home/user/appium_grid/node

[Install]
WantedBy=multi-user.target

node-1.toml

[server]
port = 5415

[node]
detect-drivers = false

[relay]
url = "http://localhost:4714"
status-endpoint = "/status"
configs = [
    "1", "{\"appium:udid\": \"10.11.111.52:5554\", \"platformName\": \"Android\", \"appium:platformVersion\": \"15.5\", \"appium:automationName\": \"uiautomator2\"}"
]

node-2.service

[Unit]
Description=Selenium Node Daemon 2
After=network.target

[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
ExecStart=java -jar /home/user/appium_grid/selenium-server-4.26.0.jar node --config /home/user/appium_grid/node/node-2.toml
User=user
Restart=always
RestartSec=3
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=selenium-node-2
WorkingDirectory=/home/user/appium_grid/node

[Install]
WantedBy=multi-user.target

node-2.toml

[server]
port = 5425

[node]
detect-drivers = false

[relay]
url = "http://localhost:4724"
status-endpoint = "/status"
configs = [
    "1", "{\"appium:udid\": \"10.22.222.200:5554\", \"platformName\": \"Android\", \"appium:platformVersion\": \"15.5\", \"appium:automationName\": \"uiautomator2\"}"
]

Usage

import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.webdriver import WebDriver

base_url = 192.168.0.12   # Remote adress of appium hub
options = UiAutomator2Options()
options.udid = "123456789099999"
driver = webdriver.Remote(command_executor=f"http://{base_url}:3334",
                          options=options)

time.sleep(3)
driver.quit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants