<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[AWSIoTMQTTClient not cleaning up after itself]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">To reproduce:</p>
<pre><code>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(&quot;Received a new message: &quot;)
# 	print(message.payload)
# 	print(&quot;from topic: &quot;)
# 	print(message.topic)
# 	print(&quot;--------------\n\n&quot;)
   
# # Subscribe to topic
# pycomAwsMQTTClient.subscribe(&quot;dev_kwaa&quot;, 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(&quot;connected&quot;)
		time.sleep(1)
		res = pycomAwsMQTTClient.publish(config.TOPIC, &quot;New Message &quot; + str(nr_calls), 1)
		print(&quot;Send result: {}&quot;.format(res))
		nr_calls += 1
		print(&quot;%d messages posted&quot; % nr_calls)
		pycom.rgbled(0x00aa00)

		time.sleep(1)
		res = pycomAwsMQTTClient.disconnect()
		print(&quot;Disconnect Result: {}&quot;.format(res))
		pycom.rgbled(0xaa0000)
	
	print('still alive, free heap: %s' % gc.mem_free())
	gc.collect()

</code></pre>
]]></description><link>https://forum.pycom.io/topic/3128/awsiotmqttclient-not-cleaning-up-after-itself</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 06:15:31 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/3128.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 Apr 2018 04:34:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to AWSIoTMQTTClient not cleaning up after itself on Thu, 26 Apr 2018 04:35:00 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">To reproduce:</p>
<pre><code>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(&quot;Received a new message: &quot;)
# 	print(message.payload)
# 	print(&quot;from topic: &quot;)
# 	print(message.topic)
# 	print(&quot;--------------\n\n&quot;)
   
# # Subscribe to topic
# pycomAwsMQTTClient.subscribe(&quot;dev_kwaa&quot;, 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(&quot;connected&quot;)
		time.sleep(1)
		res = pycomAwsMQTTClient.publish(config.TOPIC, &quot;New Message &quot; + str(nr_calls), 1)
		print(&quot;Send result: {}&quot;.format(res))
		nr_calls += 1
		print(&quot;%d messages posted&quot; % nr_calls)
		pycom.rgbled(0x00aa00)

		time.sleep(1)
		res = pycomAwsMQTTClient.disconnect()
		print(&quot;Disconnect Result: {}&quot;.format(res))
		pycom.rgbled(0xaa0000)
	
	print('still alive, free heap: %s' % gc.mem_free())
	gc.collect()

</code></pre>
]]></description><link>https://forum.pycom.io/post/19012</link><guid isPermaLink="true">https://forum.pycom.io/post/19012</guid><dc:creator><![CDATA[InverseEffect]]></dc:creator><pubDate>Thu, 26 Apr 2018 04:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to AWSIoTMQTTClient not cleaning up after itself on Thu, 26 Apr 2018 13:10:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/2616">@inverseeffect</a><br />
Thank you for this problem report, we'll review it internally</p>
<p dir="auto">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</p>
]]></description><link>https://forum.pycom.io/post/19031</link><guid isPermaLink="true">https://forum.pycom.io/post/19031</guid><dc:creator><![CDATA[jmarcelino]]></dc:creator><pubDate>Thu, 26 Apr 2018 13:10:22 GMT</pubDate></item><item><title><![CDATA[Reply to AWSIoTMQTTClient not cleaning up after itself on Thu, 26 Apr 2018 22:08:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/212">@jmarcelino</a> Hi, I used a WiPy 3.0.</p>
]]></description><link>https://forum.pycom.io/post/19063</link><guid isPermaLink="true">https://forum.pycom.io/post/19063</guid><dc:creator><![CDATA[InverseEffect]]></dc:creator><pubDate>Thu, 26 Apr 2018 22:08:30 GMT</pubDate></item></channel></rss>