micropython / micropython/micropython-lib

_thread Issue with HTTPS

Open
#351 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

I've been using _thread to run a background task, and at one point the background task posts some data online (so, connects to the wifi, HTTP request and print the result). First time it works well, but whenever I repeat it, I tend to get a number of errors, the main one being
mbedtls_ssl_handshake error: -4290
and
File "urequests.py", line 111, in post File "urequests.py", line 96, in request File "urequests.py", line 60, in request OSError: [Errno 5] EIO
I assume this has something to do with the thread trying to access the wifi twice, as it works the first time, every time, but 2nd time around, no joy.
I have tried wlan.disconnect(), and wlan.active(False) to disable the wifi in between requests, but without much joy.

This is my function:
def postJSON(j, url, retries=0, max_retries=4): data = ujson.dumps(j) if retries < max_retries: # Only retry the connection 3 times, otherwise, give up TODO: Don't give up silently! wlan = network.WLAN(network.STA_IF) if wlan.isconnected(): # Check connection print(data) r = requests.post(url=url, json=j, headers={"Authorization": "Basic " + authkey}) if (r.status_code == 200): # Check if the response OK print("success") wlan.disconnect() return True else: # Otherwise, retry postJSON(j, url, retries+1) else: do_connect() postJSON(j, url, retries+1) else: print("Tried 4 times, and failed. Sorry\r\nGiving Up") return False

Running this on the main thread throws up no such issues.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the _thread background request using the supplied postJSON function, then read urequests.py around lines 60, 96, and 111 and inspect the network.WLAN and do_connect calls. Compare repeated requests from the main thread and background thread, including the reported mbedtls_ssl_handshake error and OSError. Done means the repeated threaded HTTPS request completes reliably or the failure is documented with a confirmed scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.