<?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>Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/49511/call-to-mqtt_publish-blocking</link><description>I have a hard-to-reproduce bug in my MQTT application that I have investigated enough to realize that my MQTT thread is stuck in a call to mqtt_publish(). I&amp;#39;ve verified that no other threads make any calls into MQTT API functions or reference the MQTT</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 02 Apr 2020 13:48:34 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/49511/call-to-mqtt_publish-blocking" /><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/243212?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 13:48:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c8927d9d-f091-43c0-bd59-9fb083167a1b</guid><dc:creator>Johan</dc:creator><description>&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/243206?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 13:30:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e01089ad-ac3f-441b-ae80-4c6b51f4a2b8</guid><dc:creator>jbrzozoski</dc:creator><description>&lt;p&gt;You need to enable both in prj.conf:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_NET_LOG=y
CONFIG_MQTT_LOG_LEVEL_DBG=y&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/243107?ContentTypeID=1</link><pubDate>Thu, 02 Apr 2020 08:16:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8a1489a-c0cc-48c5-869c-57960b9ee1ae</guid><dc:creator>Johan</dc:creator><description>&lt;p&gt;H&amp;aring;kan, how do you enable&amp;nbsp;MQTT_TRC so you get printouts of that kind?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/199319?ContentTypeID=1</link><pubDate>Thu, 18 Jul 2019 13:45:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1686a23-7f0d-4a55-97ed-e77af922f690</guid><dc:creator>jbrzozoski</dc:creator><description>&lt;p&gt;I haven&amp;#39;t had the issue at all since I upgraded the NRFX BSD library to 1.0.&amp;nbsp; It was likely due to either a mis-match between the BSD library and modem firmware, or simply a bug that was fixed in the newer BSD library.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/197647?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2019 14:08:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:594b1001-9601-4e6f-baee-b9325bb8bec4</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;That&amp;#39;s a large delay.&lt;/p&gt;
&lt;p&gt;Looks like you&amp;#39;re stuck in the bsd call to send (mqtt_transport_write).&amp;nbsp;The transport sends in no flags to the send() function, meaning that it would block (unless setsockopt() with non-blocking has been enabled), and it looks like it then uses just shy of 1 hour to complete. Let me know if you still see issues after upgrading bsdlib.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/197420?ContentTypeID=1</link><pubDate>Tue, 09 Jul 2019 13:48:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:52b09d54-a625-4eca-96dd-40621340718d</guid><dc:creator>jbrzozoski</dc:creator><description>&lt;p&gt;Still not sure how I end up in this scenario.&amp;nbsp; It&amp;#39;s just a routine publish like any other.&amp;nbsp; Our application generates them maybe every couple minutes, and there&amp;#39;s nothing special about the data in this one.&amp;nbsp; I just leave the system running long enough and eventually one of them blocks.&lt;/p&gt;
&lt;p&gt;I will upgrade to v1.0.0 and try to reproduce this again sometime this week, but I&amp;#39;m reasonably sure that I was having the same issue while on v0.4.0 and the prior modem firmware as well.&lt;/p&gt;
&lt;p&gt;I added some extra LOG_INF through mqtt_publish so that a clean run looks like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[00:01:40.986,450] &amp;lt;inf&amp;gt; net_mqtt: [CID 0x20020300]:[State 0x06]: &amp;gt;&amp;gt; Topic size 0x00000023, Data size 0x000001fe
[00:01:40.986,480] &amp;lt;inf&amp;gt; net_mqtt: Got lock!
[00:01:40.986,511] &amp;lt;inf&amp;gt; net_mqtt: publish_encode=0
[00:01:40.987,243] &amp;lt;inf&amp;gt; net_mqtt: client_write=0
[00:01:40.988,342] &amp;lt;inf&amp;gt; net_mqtt: client_write=0
[00:01:40.988,372] &amp;lt;inf&amp;gt; net_mqtt: [CID 0x20020300]:[State 0x06]: &amp;lt;&amp;lt; result 0x00000000&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I was mostly trying to discover which step it was blocking on.&lt;/p&gt;
&lt;p&gt;I got it to die again overnight, and it was not at the mutex.&amp;nbsp; Even more curious, it apparently did eventually return from mqtt_publish about an hour later.&amp;nbsp; Usually when I&amp;#39;m at the bench I only give it a few minutes before I power cycle.&amp;nbsp; Note the time stamps in this log:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[05:46:12.394,348] &amp;lt;inf&amp;gt; net_mqtt: [CID 0x20020300]:[State 0x06]: &amp;gt;&amp;gt; Topic size 0x00000028, Data size 0x00000015
[05:46:12.394,378] &amp;lt;inf&amp;gt; net_mqtt: Got lock!
[05:46:12.394,409] &amp;lt;inf&amp;gt; net_mqtt: publish_encode=0
[... all of my debug code triggered here for the stuck thread ...]
[05:54:15.280,029] &amp;lt;inf&amp;gt; net_mqtt: client_write=0
[06:51:55.478,759] &amp;lt;inf&amp;gt; net_mqtt: client_write=0
[06:51:55.478,759] &amp;lt;inf&amp;gt; net_mqtt: [CID 0x20020300]:[State 0x06]: &amp;lt;&amp;lt; result 0x00000000&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Let me bring everything up to the latest BSD libraries and see if that changes anything.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to mqtt_publish blocking?</title><link>https://devzone.nordicsemi.com/thread/197362?ContentTypeID=1</link><pubDate>Tue, 09 Jul 2019 11:52:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0da762c4-754f-4810-a654-8886637ef83b</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]Are there situations where a call to mqtt_publish&amp;nbsp;are *expected* to block?&amp;nbsp; I read into the library a bit and the only thing I see obviously is the&amp;nbsp;mqtt_mutex_lock which shouldn&amp;#39;t be my problem since all MQTT library calls are coming from one thread.[/quote]
&lt;p&gt;Yes, there are certain scenarios where it will block,&amp;nbsp;but only if there&amp;#39;s a bug in the mutex handling.&lt;/p&gt;
&lt;p&gt;If you look into the function:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int mqtt_publish(struct mqtt_client *client,
		 const struct mqtt_publish_param *param)
{
	int err_code;
	const u8_t *packet;
	u32_t packetlen;

	NULL_PARAM_CHECK(client);
	NULL_PARAM_CHECK(param);

	MQTT_TRC(&amp;quot;[CID %p]:[State 0x%02x]: &amp;gt;&amp;gt; Topic size 0x%08x, &amp;quot;
		 &amp;quot;Data size 0x%08x&amp;quot;, client, client-&amp;gt;state,
		 param-&amp;gt;message.topic.topic.size,
		 param-&amp;gt;message.payload.len);

	mqtt_mutex_lock();

	err_code = verify_tx_state(client);
	if (err_code == 0) {
		err_code = publish_encode(client, param, &amp;amp;packet, &amp;amp;packetlen);

		if (err_code == 0) {
			err_code = client_write(client, packet, packetlen);
		}
	}

	mqtt_mutex_unlock();

	MQTT_TRC(&amp;quot;[CID %p]:[State 0x%02x]: &amp;lt;&amp;lt; result 0x%08x&amp;quot;,
			 client, client-&amp;gt;state, err_code);

	return err_code;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The two interesting calls here are mqtt_mutex_lock and _unlock, which expand to:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Acquire lock on the module specific mutex, if any.
 *
 * @details This is assumed to be a blocking method until the acquisition
 *          of the mutex succeeds.
 */
static inline void mqtt_mutex_lock(void)
{
	(void)k_mutex_lock(&amp;amp;mqtt_mutex, K_FOREVER);
}

/**@brief Release the lock on the module specific mutex, if any.
 */
static inline void mqtt_mutex_unlock(void)
{
	k_mutex_unlock(&amp;amp;mqtt_mutex);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Locking a mutex that is already locked will make you hang tight until it&amp;#39;s unlocked:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/1.9.0/kernel/synchronization/mutexes.html#locking-a-mutex"&gt;https://docs.zephyrproject.org/1.9.0/kernel/synchronization/mutexes.html#locking-a-mutex&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I do not see any scenarios where you&amp;#39;re potentially deadlocked (but cannot rule it out).&lt;/p&gt;
&lt;p&gt;Do you know how you end up in this scenario?&lt;/p&gt;
&lt;p&gt;Using v0.4.0 together with modem fw 1.0.0 does not sound like a good match. You should atleast update bsdlib to the matching revision, as modem and bsdlib is very tightly coupled.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>