Request failed with status 500 due to unknown error:

Hello,

I am using grid/appium setup with real android devices running via jenkins on nodejs. And noticed errors like following:

ERROR webdriver: Request failed with status 500 due to unknown error: 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
ERROR webdriver: unknown error: 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

When i restarted real devices for whole day I could run tests without issues, however next day they came back. I feel like something might getting cached in the phones ?
The thing with each test I am installing app, and once it ends uninstall it, due to needs of tests.

I read a lot on forums and stackoverflow, and the only solution that helped was restarting devices, unfortunately it was only for a day :confused:

I have noticed that our test run much more smoothly if we reboot the device as well. We use the libimobiledevice libraries (some are required by Appium) to reboot the device before testing, programatically:

I have written a Ruby script that uses ‘idevice_id’ to get udid, then ‘idevicediagnostics’ to reboot, and ‘idevicesyslog’ to verify that the device is running again by checking device log. Please feel free to use it, or as the basis for writing one in whatever language you are using:

#!/usr/bin/env ruby
require 'timeout'
# 25 seconds seems like just enough to see reboot of device in logs
timeout_in_seconds = 25

udid = `/usr/local/bin/idevice_id`.split()[0]

# stdout, stderr pipes
rout, wout = IO.pipe
rerr, werr = IO.pipe

@pid = nil
@status = nil
begin
  Timeout::timeout(timeout_in_seconds) {
    # Restart device by udid
    Process.spawn("/usr/local/bin/idevicediagnostics -u #{udid} restart")
    # get syslog in separate process to see reboot
    @pid = Process.spawn("/usr/local/bin/idevicesyslog", :out => wout, :err => werr)
    _, @status = Process.wait2(@pid)
  }
rescue Timeout::Error
  Process.kill('KILL', @pid)
end

# close write ends so we could read them
wout.close
werr.close

@stdout = rout.readlines.join("")
@stderr = rerr.readlines.join("")

# dispose the read ends of the pipes
rout.close
rerr.close

# print log showing reboot
puts @stdout

Note that if you have multiple devices connected to the machine this script may need to be adjusted–I have not tested this. Also, the script will fail if the libimobile device utilities are not installed.