DIGI XBee 3 Global IOT LTE-M/NB-IoT, socket file descriptor exhaust?

I noticed that each time I create a new socket, the fd in it will grow. Will it cause problem with the fd resource being exhausted?

Below is a sample test code: when the TCP connection was not successful, I saw in the exception that sock.fd has been set to -1. I don’t know the following sock.close () will correctly release all resources or not, but in the next loop, a new socket will have a higher number in its fd. I saw this code running with fd value growing up to 1024. and I did not follow up after that. My concern is, will this eventually exhaust fd or the fd number will “wrap around”?

import errno
import network
import time

import usocket as socket

def dbg_print(msg, line_end=None):
   if line_end is None:
        print(msg)
    else:
        print(msg, end=line_end)

 if __name__ == "__main__":
    count = 0
    conn = network.Cellular()
    dbg_print(f"Unit test  Waiting for cellular connectivity...", line_end='')
    # We give time to connect before giving up. TWe give iup to 5 minutes
    while not conn.isconnected() and count < 300:
        time.sleep(1)
        count += 1
        dbg_print(".", line_end='')
    if conn.isconnected():
        dbg_print("CONNECTED!")
        ret_value = True
    else:
        dbg_print("FAILED TO CONNECT!")
    n_file = 0
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
            dbg_print(f"socket created {sock}")
            sock.connect(("www.google.com", 20000))
        except OSError as ex:
            if ex.errno == errno.ENFILE:
                n_file += 1
                if n_file % 800 == 1:
                    dbg_print(f"ENFile = {n_file}")
            else:
                dbg_print(f" {sock} connect err = {ex} ")
                sock.close()
                time.sleep_ms(10)
            continue
        dbg_print(f"connection established {sock}")
        sock.close()
        time.sleep_ms(500)

What radio are you working with and what firmware version is used on that module?

Where did you get this code from?

Where in this code are you declaring the “FD” function?

Hi, mvut,

product family: XBXC3
Function set: XBC LTE-M/NB-IoT Global
Firmware version: 1141D

I wrote this code, not from others.

the “FD” can be seen in print of {sock}, i.e.
in this line , it will print something like : socket created sock.fd=5
dbg_print(f"socket created {sock}")

in below line, it would print: sock.fd=-1 connect err = …
dbg_print(f" {sock} connect err = {ex} ")

I noticed the sock.fd value keeps growing upon creating new ones.

Regards,
HL.

First, I would suggest updating to the current firmware version of 1141F.

Next, you would need to declare the value for socket.fd and how it is to function. Then you would be able to reset it if you had an Error.

Problem is that the socket is a usocket class, I have no access to the socket fd. e.g. if I try to get it with sock.fileno(), it will throw exception: 'socket' object has no attribute 'fileno', sock <socket fd=7>

The fd number you are seeing is an implementation detail of the XBee and its code. It is nothing you need to be concerned with.

You really are not giving the radio the time it needs to close the socket. Try increasing it to about 1 second.