WiFiNINA client.connect() freezes when WiFi/Internet drops (Matrix Portal M4)

Hi everyone,

I'm using an Adafruit Matrix Portal M4, which can run either CircuitPython or Arduino. I decided to use Arduino because I need to create an Access Point, and the only library that supports this on this board is WiFiNINA. CircuitPython's ESP32SPI library doesn’t seem to support AP mode (or at least I haven’t found a way to do it).

However, I'm running into a blocking issue with WiFiNINA:

Whenever there is a sudden WiFi or internet loss, and the code tries to make a request to a web server, the program gets stuck inside client.connect().
It never times out, never returns false, and even if WiFi comes back, the code stays frozen indefinitely.

What can be done in this situation?

Is there any known workaround, timeout mechanism, or alternative approach to prevent client.connect() from blocking forever?

Below is a snippet showing where the freeze happens. If the WiFi/internet drops at this point, the program never continues.

#include <WiFiNINA.h>

char ssid[] = "YourNetwork";
char pass[] = "YourPassword";

char server[] = "example.com"; 

WiFiClient client;

void setup() {
  Serial.begin(115200);

  // Connect to WiFi
  while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.println("Trying to connect...");
    delay(1000);
  }

  Serial.println("Connected to WiFi");
}

void loop() {
  Serial.println("Connecting to server...");

  // ---- Freeze happens here if WiFi drops ----
  if (client.connect(server, 80)) {
    Serial.println("Connected!");
    client.println("GET / HTTP/1.1");
    client.println("Host: example.com");
    client.println("Connection: close");
    client.println();

  } else {
    Serial.println("Connection failed.");
  }

  client.stop();
  delay(5000);
}

If WiFi is lost right before or during client.connect(), the code hangs forever.

Any ideas, recommendations, or insights into this behavior?

Thanks in advance!

you could try my fork. it has some fixes. it requires the newest nina firmware
https://github.com/JAndrassy/WiFiNINA