micropython / micropython/micropython-lib

requests: guard against getaddrinfo() hang when WiFi not connected

Open
#1,078 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

Summary

The request() function in requests/__init__.py calls socket.getaddrinfo() (line 81) without verifying network connectivity. On platforms where getaddrinfo() blocks indefinitely when the WiFi interface is active but not connected (see micropython/micropython#18797), this causes the device to freeze with no way to recover except a hard reset.

The timeout parameter passed to requests.get() has no effect because socket.settimeout() is applied to the socket object (line 93) — which is created after getaddrinfo() returns. So the hang occurs before any timeout takes effect.

Suggested fix

Add a connectivity check before getaddrinfo():

    # Guard: getaddrinfo() blocks indefinitely on RP2040/RP2350 when the
    # CYW43 WiFi interface is active but has no IP address.
    try:
        import network
        _wlan = network.WLAN(network.STA_IF)
        if not _wlan.isconnected():
            raise OSError(-1, "WiFi not connected")
    except ImportError:
        pass  # Non-WiFi platform, skip guard

    ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)

This is wrapped in try/except ImportError so it's safe on non-WiFi platforms (ESP32 Ethernet, Unix port, etc.).

Also consider

A default socket timeout (e.g., 30s) applied before getaddrinfo() would provide defense-in-depth, though it wouldn't help here since settimeout() only affects socket operations, not DNS resolution.

Environment

  • Board: Raspberry Pi Pico 2 W (RP2350 + CYW43)
  • MicroPython: v1.26.1
  • Upstream issue: micropython/micropython#18797

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 in requests/init.py at request(), especially the socket.getaddrinfo() call around line 81 and socket timeout setup around line 93. Reproduce or review the RP2040/RP2350 WiFi case, then verify that disconnected WiFi no longer hangs, while non-WiFi platforms continue past the guard; the issue names no test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.