Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:00:47+00:00 2026-06-14T00:00:47+00:00

When using MonkeyRunner, every so often I get an error like: 120830 18:39:32.755:S [MainThread]

  • 0

When using MonkeyRunner, every so often I get an error like:

120830 18:39:32.755:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] Unable to get variable: display.density
120830 18:39:32.755:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]java.net.SocketException: Connection reset

From what I’ve read, sometimes the adb connection goes bad, and you need to reconnect. The only problem is, I’m not able to catch the SocketException. I’ll wrap my code like so:

try:
    density = self.device.getProperty('display.density')
except:
    print 'This will never print.'

But the exception is apparently not raised all the way to the caller. I’ve verified that MonkeyRunner/jython can catch Java exceptions the way I’d expect:

>>> from java.io import FileInputStream
>>> def test_java_exceptions():
...     try:
...         FileInputStream('bad mojo')
...     except:
...         print 'Caught it!'
...
>>> test_java_exceptions()
Caught it!

How can I deal with these socket exceptions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:00:48+00:00Added an answer on June 14, 2026 at 12:00 am

    Below is the workaround I ended up using. Any function that can suffer from adb failures just needs to use the following decorator:

    from subprocess import call, PIPE, Popen
    from time import sleep
    
    def check_connection(f):
        """
        adb is unstable and cannot be trusted.  When there's a problem, a
        SocketException will be thrown, but caught internally by MonkeyRunner
        and simply logged.  As a hacky solution, this checks if the stderr log 
        grows after f is called (a false positive isn't going to cause any harm).
        If so, the connection will be repaired and the decorated function/method
        will be called again.
    
        Make sure that stderr is redirected at the command line to the file
        specified by config.STDERR. Also, this decorator will only work for 
        functions/methods that take a Device object as the first argument.
        """
        def wrapper(*args, **kwargs):
            while True:
                cmd = "wc -l %s | awk '{print $1}'" % config.STDERR
                p = Popen(cmd, shell=True, stdout=PIPE)
                (line_count_pre, stderr) = p.communicate()
                line_count_pre = line_count_pre.strip()
    
                f(*args, **kwargs)
    
                p = Popen(cmd, shell=True, stdout=PIPE)
                (line_count_post, stderr) = p.communicate()
                line_count_post = line_count_post.strip()
    
                if line_count_pre == line_count_post:
                    # the connection was fine
                    break
                print 'Connection error. Restarting adb...'
                sleep(1)
                call('adb kill-server', shell=True)
                call('adb start-server', shell=True)
                args[0].connection = MonkeyRunner.waitForConnection()
    
        return wrapper
    

    Because this may create a new connection, you need to wrap your current connection in a Device object so that it can be changed. Here’s my Device class (most of the class is for convenience, the only thing that’s necessary is the connection member:

    class Device:
        def __init__(self):
            self.connection = MonkeyRunner.waitForConnection()
            self.width = int(self.connection.getProperty('display.width'))
            self.height = int(self.connection.getProperty('display.height'))
            self.model = self.connection.getProperty('build.model')
    
        def touch(self, x, y, press=MonkeyDevice.DOWN_AND_UP):
            self.connection.touch(x, y, press)
    

    An example on how to use the decorator:

    @check_connection
    def screenshot(device, filename):
        screen = device.connection.takeSnapshot()
        screen.writeToFile(filename + '.png', 'png')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the android monkeyrunner to automatically run my application. monkeyDevice.executeShellCommand(am start -a android.intent.action.MAIN
Using rubular.com as a guide, I am trying to get this expression to work
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using range() in Underscore I can make something like this: _.range(10); >> [0, 1,
I am trying to automate few screen clicks and entries using monkeyrunner(using AndroidViewClient )
Using Android TelephonyManager an application can obtain the state of data activity over the
Using Jenkins or Hudson I would like to create a pipeline of builds with
I am playing a video (.mp4 file) using monkeyrunner through the gallery3d/MovieActivity component that
Using Rails 3.2.0 with haml and sass: I Would like to link an external

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.