AWSIoTMQTTClient not cleaning up after itself



  • I wrote a script to 'emulate' what would happen if a long running WiPy was to make several connects-disconnects to AWS MQTT server. After about 7 - 9 connect-send-disconnect cycles I get the following error: MemoryError: can't create thread. I suspect that calling the AWSIoTMQTTClient .disconnect() method does not destroy the threads and the mutex locks associated with the thread. The problem with most of these examples and libraries is that it works nice when you play nice but goes pear shaped otherwise. The whole reason for going the micropython route is to be able to develop rapidly in a high level language and not to worry about the complexities one would normally have when developing in C for baremetal systems.

    To reproduce:

    from MQTTLib import AWSIoTMQTTClient
    from network import WLAN
    import time
    import config
    import pycom
    from machine import Timer
    import gc
    
    pycom.heartbeat(False)
    
    pycom.rgbled(0xff000)
    # Connect to wifi
    wlan = WLAN(mode=WLAN.STA)
    if not wlan.isconnected():
    	wlan.connect(config.WIFI_SSID, auth=(None, config.WIFI_PASS), timeout=10000)
    	while not wlan.isconnected():
    		time.sleep(0.5)
    print('WLAN connection succeeded!')
    pycom.rgbled(0x0a0a00)
    
    # user specified callback function
    # def customCallback(client, userdata, message):
    # 	print("Received a new message: ")
    # 	print(message.payload)
    # 	print("from topic: ")
    # 	print(message.topic)
    # 	print("--------------\n\n")
       
    # # Subscribe to topic
    # pycomAwsMQTTClient.subscribe("dev_kwaa", 1, customCallback)
    # time.sleep(2)
    	
    gc.enable()
    
    # Send message to host
    nr_calls = 0
    pycomAwsMQTTClient = None
    while True:
    	time.sleep(2)
    	pycomAwsMQTTClient = AWSIoTMQTTClient(config.CLIENT_ID)
    	pycomAwsMQTTClient.configureEndpoint(config.AWS_HOST, config.AWS_PORT)
    	pycomAwsMQTTClient.configureCredentials(config.AWS_ROOT_CA, config.AWS_PRIVATE_KEY, config.AWS_CLIENT_CERT)
    
    	pycomAwsMQTTClient.configureOfflinePublishQueueing(config.OFFLINE_QUEUE_SIZE)
    	pycomAwsMQTTClient.configureDrainingFrequency(config.DRAINING_FREQ)
    	pycomAwsMQTTClient.configureConnectDisconnectTimeout(config.CONN_DISCONN_TIMEOUT)
    	pycomAwsMQTTClient.configureMQTTOperationTimeout(config.MQTT_OPER_TIMEOUT)
    	pycomAwsMQTTClient.configureLastWill(config.LAST_WILL_TOPIC, config.LAST_WILL_MSG, 1)		
    	if pycomAwsMQTTClient.connect():
    		print("connected")
    		time.sleep(1)
    		res = pycomAwsMQTTClient.publish(config.TOPIC, "New Message " + str(nr_calls), 1)
    		print("Send result: {}".format(res))
    		nr_calls += 1
    		print("%d messages posted" % nr_calls)
    		pycom.rgbled(0x00aa00)
    
    		time.sleep(1)
    		res = pycomAwsMQTTClient.disconnect()
    		print("Disconnect Result: {}".format(res))
    		pycom.rgbled(0xaa0000)
    	
    	print('still alive, free heap: %s' % gc.mem_free())
    	gc.collect()
    
    


  • @jmarcelino Hi, I used a WiPy 3.0.



  • @inverseeffect
    Thank you for this problem report, we'll review it internally

    Can you share which board you are using? The new models (WiPy3, LoPy4, FiPy and GPY) with 4MB of RAM tend to be more tolerant to memory issues



Pycom on Twitter