<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/120441/calls-to-mqtt-library-hang-indefinitely-when-client-is-not-connected</link><description>Hello! 
 I&amp;#39;m writing an application that uses MQTT to post some sensor data to a server. My networking code is basically a duplicate of this sample , except I set up the LTE connection before starting the network loop, have a larger volume of data to</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 28 Apr 2025 10:58:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/120441/calls-to-mqtt-library-hang-indefinitely-when-client-is-not-connected" /><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/533159?ContentTypeID=1</link><pubDate>Mon, 28 Apr 2025 10:58:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:93872f00-94f1-4cd4-a223-a612aba238f7</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;This seems to be an issue with upstream Zephyr library and/or sample. You could try to open new Zephyr issue on GitHub.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/532426?ContentTypeID=1</link><pubDate>Tue, 22 Apr 2025 17:34:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b40e64d8-4521-4a74-9e53-100fb773c843</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;I should also note I&amp;#39;m using&amp;nbsp;&lt;br /&gt;&lt;span&gt;CONFIG_NET_SOCKETS_OFFLOAD&lt;/span&gt;&lt;span&gt;=y&lt;br /&gt;&lt;/span&gt;and&lt;br /&gt;CONFIG_MODEM_SOCKET=y.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/532409?ContentTypeID=1</link><pubDate>Tue, 22 Apr 2025 14:39:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c2421c95-1048-4077-aa4d-0b2bb0e35615</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;It looks like the issue has resurfaced. Over the last week, I&amp;#39;ve been digging through the library code, and it seems the blocking call is &lt;code&gt;zsock_sendmsg()&lt;/code&gt; in &lt;code&gt;mqtt_client_tls_write_msg()&lt;/code&gt;. If a socket does not receive a close signal, or encounters some other issue, it would remain in a waiting state indefinitely according to the BSD sockets spec.&lt;/p&gt;
&lt;p&gt;However, this will never return control to the application, and in an environment where the network is guaranteed to be unreliable, this feels like an issue.&lt;/p&gt;
&lt;p&gt;How do you recommend I proceed? Should I open an issue in GitHub? I&amp;#39;ve attached the MQTT library&amp;#39;s log below. The last line &amp;quot;Sending data...&amp;quot; is a log statement I added right before the call to &lt;code&gt;zsock_sendmsg()&lt;/code&gt;. After 13 hours, it had still not returned.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[00:11:07.202,514] &amp;lt;dbg&amp;gt; net_mqtt: mqtt_publish: (net_thread): [CID 0x2000d2d8]:[State 0x06]: &amp;gt;&amp;gt; Topic size 0x0000001b, Data size 0x0000046b
[00:11:07.216,430] &amp;lt;dbg&amp;gt; net_mqtt_enc: pack_utf8_str: (net_thread): &amp;gt;&amp;gt; str_size:0000001d cur:0x20013d25, end:0x20014520
[00:11:07.228,210] &amp;lt;dbg&amp;gt; net_mqtt_enc: pack_uint16: (net_thread): &amp;gt;&amp;gt; val:001b cur:0x20013d25, end:0x20014520
[00:11:07.238,983] &amp;lt;dbg&amp;gt; net_mqtt_enc: mqtt_encode_fixed_header: (net_thread): &amp;lt;&amp;lt; msg type:0x31 length:0x00000488
[00:11:07.250,213] &amp;lt;dbg&amp;gt; net_mqtt_enc: packet_length_encode: (net_thread): &amp;gt;&amp;gt; length:0x00000488 cur:0, end:0
[00:11:07.260,986] &amp;lt;dbg&amp;gt; net_mqtt_enc: mqtt_encode_fixed_header: (net_thread): Fixed header length = 03
[00:11:07.271,301] &amp;lt;dbg&amp;gt; net_mqtt_enc: pack_uint8: (net_thread): &amp;gt;&amp;gt; val:31 cur:0x20013d22, end:0x20014520
[00:11:07.281,829] &amp;lt;dbg&amp;gt; net_mqtt_enc: packet_length_encode: (net_thread): &amp;gt;&amp;gt; length:0x00000488 cur:0x20013d23, end:0x20014520
[00:11:07.294,219] &amp;lt;dbg&amp;gt; net_mqtt: client_write_msg: (net_thread): [0x2000d2d8]: Transport writing message.
[00:11:07.304,901] &amp;lt;dbg&amp;gt; net_mqtt_sock_tls: mqtt_client_tls_write_msg: (net_thread): Sending data...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/531170?ContentTypeID=1</link><pubDate>Wed, 09 Apr 2025 11:57:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ee462739-197f-45fd-9eee-2a58912a6741</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;I am glad to hear that your issue has been resolved.&lt;br /&gt;Thank you for the update.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/531035?ContentTypeID=1</link><pubDate>Tue, 08 Apr 2025 13:01:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65385532-2988-4382-9453-43b45cff339e</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;Hello Dejan,&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve concluded the issue is with the broker&amp;mdash;it is ignoring the connection when too many are attempted within a short time. Occasionally I see the ETIMEDOUT (-116) error before it attempts another connection, and it&amp;#39;s that connection which will hang indefinitely. I&amp;#39;ve solved the issue by adding exponential backoff on the retries.&lt;/p&gt;
&lt;p&gt;Not sure why another ETIMEDOUT error wouldn&amp;#39;t be raised&amp;mdash;that seems like strange behavior for the library&amp;mdash;but it&amp;#39;s no longer affecting me, so I&amp;#39;ll move on.&lt;/p&gt;
&lt;p&gt;Thank you for your help and time!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530997?ContentTypeID=1</link><pubDate>Tue, 08 Apr 2025 09:49:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbe51fde-0755-4ea0-922a-310b43bdb19e</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Do you see the same issue when using unmodified Zephyr mqtt_publisher sample?&lt;br /&gt;&lt;br /&gt;Can you upload your project so that I can try to reproduce the issue?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530818?ContentTypeID=1</link><pubDate>Mon, 07 Apr 2025 12:48:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec5ce7f5-093a-4fd2-a41e-b6107a52b870</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;My build command is this (I&amp;#39;m using CMake directly):&lt;br /&gt;&lt;code&gt;cmake -DBOARD=nrf9160dk/nrf9160/ns -Bbuild -DDTC_OVERLAY_FILE=boards/nrf9160dk_nrf9160.overlay&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Then I call &lt;code&gt;make&lt;/code&gt; from the build directory. I have incorporated the necessary overlays into my config file, as this is only ever going to be built for one board. I&amp;#39;ll attach that as well.&lt;/p&gt;
&lt;p&gt;The full modem trace is in Wireshark pcapng format.&lt;/p&gt;
&lt;p&gt;Thanks again,&lt;/p&gt;
&lt;p&gt;&amp;mdash;Ethan B.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/05543.prj.conf"&gt;devzone.nordicsemi.com/.../05543.prj.conf&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/modemtrace.pcapng"&gt;devzone.nordicsemi.com/.../modemtrace.pcapng&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530775?ContentTypeID=1</link><pubDate>Mon, 07 Apr 2025 09:53:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:52267f17-187c-4972-8daa-7d49149d5e3d</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Can you show your build command?&lt;br /&gt;Do you use any of the&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/net/mqtt_publisher"&gt;mqtt_publisher&lt;/a&gt; sample&amp;#39;s overlays?&lt;/p&gt;
[quote user="Ethan Barry"]This is a sample of the logs.[/quote]
&lt;p&gt;It might be useful to get full log. You can upload it using Insert-&amp;gt;Image/video/file-&amp;gt;Upload.&lt;/p&gt;
[quote user="Ethan Barry"]I&amp;#39;ve examined modem traces[/quote]
&lt;p&gt;You could upload modem trace as well.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530664?ContentTypeID=1</link><pubDate>Fri, 04 Apr 2025 15:53:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7d53880-df3a-47d7-8732-de16b6c6cbe5</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;This is the last line when the MQTT log level is set to debug.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;[00:10:29.617,401] &amp;lt;dbg&amp;gt; net_mqtt_sock_tcp: mqtt_client_tcp_connect: (net_thread): Created socket 4&lt;/code&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530658?ContentTypeID=1</link><pubDate>Fri, 04 Apr 2025 15:20:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f9ff90e-3580-4593-ad91-3876d7f1de74</guid><dc:creator>Ethan Barry</dc:creator><description>&lt;p&gt;00&amp;gt; [00:04:28.296,356] &amp;lt;inf&amp;gt; tracing: ### CALLING MQTT_CONNECT().&lt;br /&gt;00&amp;gt; [00:04:34.280,670] &amp;lt;inf&amp;gt; tracing: ### CALLED MQTT_CONNECT().&lt;br /&gt;00&amp;gt; [00:04:34.281,036] &amp;lt;inf&amp;gt; tracing: ###### PASSED MQTT_CONNECT()!&lt;br /&gt;00&amp;gt; [00:04:34.281,402] &amp;lt;inf&amp;gt; tracing: ###### EXITED PREPARE_FDS().&lt;br /&gt;00&amp;gt; [00:04:34.281,738] &amp;lt;inf&amp;gt; tracing: ### CALLING WAIT().&lt;br /&gt;00&amp;gt; [00:04:34.282,073] &amp;lt;inf&amp;gt; tracing: ###### NFDS &amp;gt; 0.&lt;br /&gt;00&amp;gt; [00:04:36.282,806] &amp;lt;inf&amp;gt; tracing: ###### POLLED.&lt;br /&gt;00&amp;gt; [00:04:38.289,764] &amp;lt;inf&amp;gt; tracing: ### * MQTT DISCONNECTED WITH ERROR -113! *&lt;br /&gt;00&amp;gt; [00:04:38.290,588] &amp;lt;inf&amp;gt; tracing: ### CALLING MQTT_CONNECT().&lt;/p&gt;
&lt;p&gt;This is a sample of the logs. The last line you see here was the last line logged&amp;mdash;the call to &lt;code&gt;mqtt_connect()&lt;/code&gt; blocked for hours, until I reset the board. As far as other errors, no, I don&amp;#39;t get any. I&amp;#39;ve examined modem traces, and the network calls always seem to block after a TCP retransmit. However, the application may make many successful retransmissions before any blocking or hanging happens. The MQTT logging hasn&amp;#39;t told me anything yet.&lt;/p&gt;
&lt;p&gt;My application doesn&amp;#39;t recover from this&amp;mdash;that&amp;#39;s the issue. The library calls sometimes block indefinitely, and I can&amp;#39;t &amp;quot;recover&amp;quot; in any way, unless I preempt that operation and kill it. I&amp;#39;d rather not have to kill my networking thread, though.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Calls to MQTT library hang indefinitely when client is not connected.</title><link>https://devzone.nordicsemi.com/thread/530628?ContentTypeID=1</link><pubDate>Fri, 04 Apr 2025 12:53:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cf9a541b-9287-47ba-8ede-8e151b33b9cc</guid><dc:creator>dejans</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;&lt;br /&gt;Do you get any errors when this &amp;quot;hanging&amp;quot; takes place?&amp;nbsp;&lt;/p&gt;
[quote user=""]Occasionally the MQTT event handler function receives&amp;nbsp;&lt;code&gt;MQTT_EVT_DISCONNECT&lt;/code&gt;&amp;nbsp;with reason code 113 (&lt;code&gt;ECONNABORTED&lt;/code&gt;) right before making a network call (&lt;code&gt;mqtt_ping()&lt;/code&gt;, &lt;code&gt;mqtt_connect()&lt;/code&gt;, &amp;amp;c.).[/quote]
&lt;p&gt;Can you provide a complete log which shows ECONNABORTED?&lt;/p&gt;
&lt;p&gt;How does your application recover from indefinite hanging?&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Dejan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>