[WiFiServer/WiFiClient] Connection doesn't persist when client object goes out of scope

According to Arduino’s docs for server.available():

The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().

However, we observed that the current behavior of WiFiServer and WiFiClient as it has been implemented into the Arduino SDK for the AMB82-Mini seems to contradict this.

See these examples:

Working:

#include <WiFi.h>

char ssid[] = "";    // your network SSID (name)
char pass[] = "";        // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup()
{
    Serial.begin(115200);
    while (status != WL_CONNECTED) {
        status = WiFi.begin(ssid, pass);
        delay(5000);
    }
   
    server.begin();
}


void loop()
{
    WiFiClient client = server.available();
    if (client) {
        Serial.println("new client connected");
        String currentLine = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        //do something
                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
        client.stop();
        Serial.println("client disconnected");
    } else {
        Serial.println("waiting for client connection");
        delay(1000);
    }
}

Not working:

#include <WiFi.h>

char ssid[] = "";    // your network SSID (name)
char pass[] = "";        // your network password
int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient client;

void setup()
{
    Serial.begin(115200);
    while (status != WL_CONNECTED) {
        status = WiFi.begin(ssid, pass);
        delay(5000);
    }
   
    server.begin();
}

void loop()
{
    client = server.available();
    if (client) {
        Serial.println("new client connected");
        String currentLine = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        //do something
                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
        client.stop();
        Serial.println("client disconnected");
    } else {
        Serial.println("waiting for client connection");
        delay(1000);
    }
}

We saw the same non-working behavior whether the WiFiClient was scoped globally (as in the example above), as a class member, or when passed by referenced, value, or pointer. Simply, WiFiClient fails to work as soon as it leaves the local scope where it was returned from server.available().

We can try to find ways to work around this, but it would help a ton if things like this would work reliably or at least be documented (especially since the above issue makes working asynchronously, where we have different things being sent to the client at different times based on state, much tricker).

I created a PR that fixes this issue as well as another issue I found while working on a fix for this one. See PR #259 on GitHub.