Skip to content

fetch method of PortalBase tries to keep connecting to WiFi forever if connection cannot be established #45

Closed
@javiser

Description

@javiser

Hi,
I noticed something during my project with a PyPortal: when I use the fetch method from a PyPortal object, if the connection with the Wifi cannot be established, the program execution will be stopped there forever. So I decided to have a look at the code of the fetch method in the PortalBase class and I saw that the fetch method supports a timeout parameter, which is even set to 10 by default. So I was wondering why the connection tries would keep going forever.
The timeout parameter is indeed used by the call to the method self._wifi.requests.get in the fetch method of adafruit_portalbase/network.py. But before that, there is a call to the method connect in the same file. And that's where the problem appears to be: there is a loop which becomes infinite:

        while not self._wifi.is_connected:
            ...

There is not an alternative exit point for that loop: either it connects, or it connects. No maximum number of retries, no timeout, just nothing.
I believe that this is a bug or a least a serious candidate for improvement. By understanding this I have implemented this workaround:

try:
    pyportal.network._wifi.connect(pyportal.network._secrets["ssid"], pyportal.network._secrets["password"])
except RuntimeError as error:
    print("Error connecting to WiFi - ", error)
    # Do whatever you want to do with this situation

Only if I don't get an error by this connect attempt, I will call the fetch method, otherwise I won't

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions