File table overflow during MQTT reconnect

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
agners
Posts: 6
Joined: Thu Jul 25, 2019 9:48 pm

File table overflow during MQTT reconnect

Post by agners » Mon Aug 03, 2020 8:17 pm

Hello,

I noticed that my ESP32 board running a pre-release of MicroPython 1.12 (27th of July) stops sending messages via MQTT after a WiFi disconnect. I am able to reproduce the problem by manually disconnect (from the routers Web interface). The following code shows the behavior:

Code: Select all

import network
wl = network.WLAN(network.STA_IF)
wl.active(1)
wl.connect(...)

from umqtt.robust import MQTTClient
mqttc = MQTTClient(...)
mqttc.DEBUG = True
mqttc.connect()

<disconnnect>

mqttc.publish("/test", "1")
This leads to the following output:

Code: Select all

I (2936506) wifi: STA_DISCONNECTED, reason:205
I (2936626) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2936626) wifi:state: init -> auth (b0)
I (2936636) wifi:state: auth -> init (1100)
I (2936636) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2936636) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(118,)
I (2938696) wifi: STA_DISCONNECTED, reason:205
I (2938816) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2938816) wifi:state: init -> auth (b0)
I (2938826) wifi:state: auth -> init (1100)
I (2938826) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2938826) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(118,)
I (2940886) wifi: STA_DISCONNECTED, reason:205
I (2941006) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2941016) wifi:state: init -> auth (b0)
I (2941016) wifi:state: auth -> init (1100)
I (2941016) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2941016) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(23,)
I (2943076) wifi: STA_DISCONNECTED, reason:205
I (2943206) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2943206) wifi:state: init -> auth (b0)
I (2943206) wifi:state: auth -> init (1100)
I (2943206) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (2943216) wifi: STA_DISCONNECTED, reason:202
authentication failed
mqtt reconnect: OSError(23,)

At first there are a couple of OSError 118 (which probably is ENOTNAM, not sure) those seem normal. However, then OSError 23 messages appear (ENFILE, File table overflow), and the board is stuck with those OSErrors even after WiFi reconnects...

What I found is that triggering the garbage collector does fix the problem, so adding something like this in the reconnect() exception handler can work around the problem:

Code: Select all

if e.args[0] == 23:
    gc.collect()
However, this is rather ad-hoc. Hence the question: Is this a known problem? Any proper/nice solution for this?

--
Stefan

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: File table overflow during MQTT reconnect

Post by pythoncoder » Tue Aug 04, 2020 5:54 am

The official MQTT library does not handle WiFi outages. See this repo for a library which does.
Peter Hinch
Index to my micropython libraries.

Post Reply