Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

WiFiUdp stop() does not release UDP socket

Open fpoto opened this issue 13 years ago • 7 comments

This is apparently a bug, described in some detail at http://forum.arduino.cc//index.php?topic=194991.0.

In short, if I have no problems with this pseudo-code:

begin();
for (;;) {
  sendrequest();
  getreply();
  delay();
}

which has worked reliably for hours with a request every ten seconds, but this one does not work:

for (;;) {
  begin();
  sendrequest();
  getreply();
  stop();
  delay();
}

In this second case, the program correctly receives the first reply, but fails from the second time on. Additionally, it seems that stop() does not deallocate the internal structures, because begin() fails from the fifth time on, meaning that there are no more sockets available.

I can provide an example trimmed down sketch if necessary.

fpoto avatar Oct 24 '13 08:10 fpoto

That would be great, you can make a gist https://gist.github.com/ if it makes uploading easier

ffissore avatar Oct 24 '13 10:10 ffissore

I am not accustomed to the Github, so I do not know what is easiest. I just followed your suggestion and creataed a gist at https://gist.github.com/fpoto/7135314.

This is the expected serial output with initonce set to true:

Attempting to connect to SSID: xxxxxxxx connected
UTC time: 1382565606
UTC time: 1382565608
UTC time: 1382565610
UTC time: 1382565612
UTC time: 1382565614

and this is the output with initonce set to false:

Attempting to connect to SSID: xxxxxxxx connected
UTC time: 1382565771
request failed
request failed
request failed
begin failed

fpoto avatar Oct 24 '13 11:10 fpoto

This should fix the problem with the UDP.stop(). Same fix as issue #1720 above. A one (edit: two) line code omission.

/* Release any resources being used by this WiFiUDP instance */
void WiFiUDP::stop()
{
      if (_sock == NO_SOCKET_AVAIL)
        return;

      ServerDrv::stopClient(_sock);

// Add this
            WiFiClass::_state[_sock] = -1;
            WiFiClass::_server_port[_sock] = 0;
// end of add

      _sock = NO_SOCKET_AVAIL;
}

Here is the test function I use to determine which sockets are available.

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.println(WiFi._server_port[x]);    
  }
}
WiFi._state[x] = -1 if socket available.
WiFi._state[x] = socket number (0 to 3) if socket used.

WiFi._server_port[x] = 0 if not listening.
WiFi._server_port[x] = listening port if used.

Add this line to begin also.

/* Start WiFiUDP socket, listening at local port PORT */
uint8_t WiFiUDP::begin(uint16_t port) {

    uint8_t sock = WiFiClass::getSocket();
    if (sock != NO_SOCKET_AVAIL)
    {
        ServerDrv::startServer(port, sock, UDP_MODE);

// add this. Sets status to used rather than available.
// Otherwise a client request could get it too.
        WiFiClass::_state[sock] = sock;
// end of add

        WiFiClass::_server_port[sock] = port;
        _sock = sock;
        _port = port;
        return 1;
    }
    return 0;

}

SurferTim avatar Dec 07 '13 10:12 SurferTim

To me, this problem is caused by a discrepancy in the way sockets are allocated. Here is how a server or udp socket gets a socket. This is in WiFi.cpp. It uses the _server_port array

uint8_t WiFiClass::getSocket()
{
    for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
    {
        if (WiFiClass::_server_port[i] == 0)
        {
             return i;
        }
    }
    return NO_SOCKET_AVAIL;
}

This is in WiFiClient.cpp. It is how a client gets an available socket. It uses the _state array.

// Private Methods
uint8_t WiFiClient::getFirstSocket()
{
    for (int i = 0; i < MAX_SOCK_NUM; i++) {
      if (WiFiClass::_state[i] == NA_STATE)
      {
          return i;
      }
    }
    return SOCK_NOT_AVAIL;
}

To maintain compatibility, you should set both correctly.

I believe both should use the _state array to get an available socket, and use the server_port array to find the used listening port for that socket.

SurferTim avatar Dec 07 '13 15:12 SurferTim

I applied your patch to the 1.5.4 sources but unfortunately it does not solve the problem exposed by my test case :(

The only change in behaviour is that now I get four request failed messages rather than three ones followed by a begin failed message:

Attempting to connect to SSID: xxxxxxxx connected
UTC time: 1386924569
request failed
request failed
request failed
request failed

fpoto avatar Dec 13 '13 08:12 fpoto

You are correct. I corrected the socket depletion, but it did not correct the internal restart fail of the udp socket. It reserves the socket and sets the correct port, but does not start the udp socket listening again. Here is the function I use to check:

#include <utility/server_drv.h>

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.print(WiFi._server_port[x]);    
    Serial.print("  s=");
    Serial.print(serverDrv.getServerState(x));    
    Serial.print("  c=");
    Serial.print(serverDrv.getClientState(x));    
    Serial.print("  d=");
    Serial.println(serverDrv.availData(x));    

  }
}

SurferTim avatar Dec 13 '13 10:12 SurferTim

This issue was moved to arduino-libraries/WiFi#12

agdl avatar Jul 12 '16 13:07 agdl