question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Appium 1.22.1] - Android bootstrap socket crashed: Error: connect ECONNREFUSED 127.0.0.1:4724

See original GitHub issue

The problem

While running Appium inside a Docker container to run tests on Android devices using UiAutomator1, Appium seems to fail to connect the bootstrap port. I think maybe it’s using the wrong host address?

Environment

  • Appium version (or git revision) that exhibits the issue: 1.22.1
  • Last Appium version that did not exhibit the issue (if applicable): N/A (Didn’t try with other versions)
  • Desktop OS/version used to run Appium: Ubuntu 18.04.6 (alpine:edge in Docker)
  • Node.js version (unless using Appium.app|exe): v17.1.0
  • Npm or Yarn package manager: npm 8.1.3
  • Mobile platform/version under test: Android (SDK level 22)
  • Real device or emulator/simulator: Real device (FireTV)
  • Appium CLI or Appium.app|exe: Appium CLI

Details

I’ve started appium in the container with these args and it seems to fail at the same section in the logs for all of them:

In Docker container:
appium --address 127.0.0.1 --allow-insecure=adb_shell
appium --address 127.0.0.1 --callback-address host.docker.internal --allow-insecure=adb_shell
appium --address 0.0.0.0 --allow-insecure=adb_shell

I have ADB running on the host with:

adb -a nodaemon server

Section from Appium logs where it fails (full logs linked below):

[AndroidDriver] No app sent in, not parsing package/activity
[debug] [AndroidDriver] No app capability. Assuming it is already on the device
[debug] [AndroidBootstrap] Watching for bootstrap disconnect
[debug] [ADB] Forwarding system: 4724 to device: 4724
[debug] [ADB] Running '/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 forward tcp:4724 tcp:4724'
[debug] [UiAutomator] Starting UiAutomator
[debug] [UiAutomator] Moving to state 'starting'
[debug] [UiAutomator] Parsing uiautomator jar
[debug] [UiAutomator] Found jar name: 'AppiumBootstrap.jar'
[debug] [ADB] Running '/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 shell mkdir -p /data/local'
[debug] [ADB] Running '/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 push /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/bootstrap/bin/AppiumBootstrap.jar /data/local/tmp/'
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting IDs of all 'uiautomator' processes
[debug] [ADB] Using ps-based PID detection
[debug] [ADB] Running '/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 shell ps'
[ADB] No 'uiautomator' process has been found
[debug] [UiAutomator] Starting UIAutomator
[debug] [ADB] Creating ADB subprocess with args: ["-H","host.docker.internal","-P",5037,"-s","G010L80779330494","shell","uiautomator","runtest","AppiumBootstrap.jar","-c","io.appium.android.bootstrap.Bootstrap","-e","pkg","com.amazon.tv.launcher","-e","disableAndroidWatchers",false,"-e","acceptSslCerts",false]
[debug] [UiAutomator] Moving to state 'online'
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Registered crash watchers.
uncaughtException: Android bootstrap socket crashed: Error: connect ECONNREFUSED 127.0.0.1:4724
Error: Android bootstrap socket crashed: Error: connect ECONNREFUSED 127.0.0.1:4724
    at Socket.<anonymous> (/usr/local/lib/node_modules/appium/node_modules/appium-android-driver/lib/bootstrap.js:97:21)
    at Socket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:164:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Output of sudo netstat -lptn | grep 4724 after the error occurs:

tcp6       0      0 :::4724                 :::*                    LISTEN      3530/adb

I tried to debug further by running the following commands manually:

/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 forward tcp:4724 tcp:4724
/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 shell mkdir -p /data/local
/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 push /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/bootstrap/bin/AppiumBootstrap.jar /data/local/tmp/
/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 shell ps
/opt/android-sdk/platform-tools/adb -H host.docker.internal -P 5037 -s G010L80779330494 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.amazon.tv.launcher -e disableAndroidWatchers false -e acceptSslCerts false &

And the output of that was:

INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
io.appium.android.bootstrap.Bootstrap:
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testRunServer
INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1
[APPIUM-UIAUTO] [debug] Socket opened on port 4724[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Appium Socket Server Ready[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Loading json...[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Registered crash watchers.[/APPIUM-UIAUTO]

I also tried wget 127.0.0.1:4724 from within the Docker container:

Connecting to 127.0.0.1:4724 (127.0.0.1:4724)
wget: can't connect to remote host (127.0.0.1): Connection refused

However, if I try wget host.docker.internal:4724, it seems to work?

Connecting to host.docker.internal:4724 (240.10.0.1:4724)
[APPIUM-UIAUTO] [debug] Client connected[/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Got data from client: GET / HTTP/1.1
Host: host.docker.internal:4724
User-Agent: Wget
Connection: close
[/APPIUM-UIAUTO]


^C (pressed Ctrl+C here)
/ # [APPIUM-UIAUTO] [debug] Got data from client: [/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [debug] Got data from client: [/APPIUM-UIAUTO]
[APPIUM-UIAUTO] [error] Error processing data to/from socket (java.net.SocketException: sendto failed: EPIPE (Broken pipe))[/APPIUM-UIAUTO]

The command used to start the Docker container:

docker run -d --rm --init --add-host host.docker.internal:host-gateway --env ANDROID_SERIAL=G010L80779330494 --env SYSTEM_PORT=8200 --name=G010L80779330494 --mount type=bind,source=$PWD/G010L80779330494,target=/execution_directory -it automation-container:1.0

Link to Appium logs

Link to Gist with full logs

Thank you so much. Any help with this would be appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mykola-mokhnachcommented, Nov 22, 2021

No changes have been done to this driver for quite a long time. It is considered obsolete and the Appium team does not maintain it. Consider switching to UiAutomator2/Espresso instead.

0reactions
1oh1commented, Nov 26, 2021

Unfortunately I’m not at liberty to change the driver since I don’t own the tests. However, I managed to find a way around the issue. Listing the solution here in case someone else also runs into the same issue.

Resolution:

  1. Have iptables available in the container
  2. Start Docker container with --privileged --cap-add=NET_ADMIN --add-host host.docker.internal:host-gateway
  3. Once container is started, redirect localhost:4724 to host.docker.internal:4724 with these commands:
iptables -t nat -A OUTPUT -m addrtype --src-type LOCAL --dst-type LOCAL -p tcp --dport 4724 -j DNAT --to-destination `netstat -rn | grep ^0.0.0.0 | awk '{print $2}'`
iptables -t nat -A POSTROUTING -m addrtype --src-type LOCAL --dst-type UNICAST -j MASQUERADE

sysctl -w net.ipv4.conf.all.route_localnet=1
  1. Start Appium with appium --address 0.0.0.0 --suppress-adb-kill-server --allow-insecure=adb_shell
Read more comments on GitHub >

github_iconTop Results From Across the Web

Appium Android bootstrap socket crashed: Error: connect ...
Solved the problem by making a fresh install of linux mint 18.1. Still don't know why this showed up in ubuntu 16.04.
Read more >
Android bootstrap socket crashed: Error: connect ...
Error : Android bootstrap socket crashed: Error: connect ECONNREFUSED ::1:4724 ... hi, anyone help? when i run the test sample, it throw exception ......
Read more >
Unable to launch apk using appium. error: Unhandled error
Tried with 1.5.3 version and I am getting error as “Error: Android bootstrap socket crashed: Error: connect ECONNREFUSED 10.145.64.175:4724”.
Read more >
Android bootstrap socket crashed: Error: read ECONNRESET ...
I installed appium from npm and have I am running my test for android on 3 real devices. Using windows OS. My tests...
Read more >
Android bootstrap socket crashed when PC offline - Support
Make sure you have the entry for localhost in /etc/hosts · Node through which appium is launched will resolve the localhost through nscd...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found