Very peculiar on-off issue with urequests

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
theodox
Posts: 7
Joined: Sat Apr 13, 2019 11:55 pm

Very peculiar on-off issue with urequests

Post by theodox » Fri Nov 15, 2019 1:48 am

I've got a pyboard D which talks to a web service using `urequests`. The communications aren't that complicated; it sends a get request and receives a short bit of json in in retun, something like this:

Code: Select all

{"remaining":  123, "total": 456}
This all works.... slightly less than 50% of the time. One pattern I have been able to repro is that it works on alternating runs, ie, if i power up the pyboard it will work (as far as I can tell, for that entire session) but if the board is powered off and then powered back on it won't work at all in the next session: all requests will fail with an

Code: Select all

OSError
, i think always with a -2 error code.

Restart again, and it works.

The truly puzzling thing is that the code which runs this is basically stateless (it does not save information to

Code: Select all

/flash
so I don't have the faintest idea how I could be inadvertently setting up an even-odd error situation. However the repro is vey solid, I've verified it dozens of times on... off... on... off....

I've tried:
  • waiting in between runs, in cases there's some kind of sticky state. Waits up to the limits of my patience (5 mins or so) seem to have no effect
  • looking at the logs for the server to see if there anomalies in the requests. They appear to be well-formed

    Code: Select all

    75.172.128.250 - - [15/Nov/2019:01:22:25 +0000] "GET /check/Dad HTTP/1.1" 200 32 "-" "-" "75.172.128.250" response-time=0.002
  • adding code to make sure the NIC is still connected. It is.
So.... i'm truly stumped as to how I can be getting these request failures on timeouts. Any ideas on what might or might not be the relevant issue?

The part of the loop which is trying to talk to the server is like this;

Code: Select all

if LOGGED_IN is not None and len(LOGGED_IN):

    req = 'http://theodox.pythonanywhere.com/check/{}'.format(LOGGED_IN)
    print("checking status")

    response = None
    try:

        response = requests.get(req)
        info = response.json()
    except OSError:
        error = "request timeout"
        print(error)

        
        LOGGED_IN = None
        REMAIN = 0
        TOTAL = 0
        time.sleep(1)
    else:
        REMAIN = info['remaining']
        TOTAL = info['total']

    finally:
        del response
AFAICT all of the failure are OSError -2 in either the get or the json() line.

User avatar
jimmo
Posts: 890
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Very peculiar on-off issue with urequests

Post by jimmo » Mon Nov 18, 2019 2:14 am

Wow that is super weird.

I don't understand why this would lead to the alternating behavior, but have you confirmed that it's successfully making the wifi connection on every power-up? What does your code that verifies the network connection look like?

Edit: it sounds like you're saying that the request always goes through to the webserver?

So to clarify, every second time you power up the board, every request you send successfully makes it to the remote server, but will always fail with OSError(-2) in processing the response?

Is there a way you can package this up so I can try and repro here?

ptranter
Posts: 13
Joined: Mon Dec 03, 2018 8:39 pm

Re: Very peculiar on-off issue with urequests

Post by ptranter » Tue Nov 19, 2019 1:03 pm

Have you tried using Wireshark to look at the interactions?
There may be some clues there as to what is happening

Good luck

Post Reply