<?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>SDK13 app_sched_execute() infinite recursion</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/21225/sdk13-app_sched_execute-infinite-recursion</link><description>Hi, 
 In SDK11, it was possible to call app_sched_execute() from a function that was itself called from the scheduler: 
 void app_sched_execute(void)
{
 void * p_event_data;
 uint16_t event_data_size;
 app_sched_event_handler_t event_handler;
</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 12 Apr 2017 08:46:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/21225/sdk13-app_sched_execute-infinite-recursion" /><item><title>RE: SDK13 app_sched_execute() infinite recursion</title><link>https://devzone.nordicsemi.com/thread/83051?ContentTypeID=1</link><pubDate>Wed, 12 Apr 2017 08:46:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d7d7c40-b476-45b7-bf96-a9be6e2e2207</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Mike,&lt;/p&gt;
&lt;p&gt;I agree that the change was only about the order when we update &lt;code&gt;m_queue_start_index&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But the problem lies on the circular buffer we have on scheduling task. If we have a full queued buffer and we move &lt;code&gt;m_queue_start_index&lt;/code&gt; forward, we allow a new task to be scheduled (see app_sched_queue_full() ). And when there is a slot available, &lt;code&gt;m_queue_end_index&lt;/code&gt; is allowed to increase and buffer new data, overwriting the data that is pending to be executed by event_handler(p_event_data, event_data_size);&lt;/p&gt;
&lt;p&gt;So the issue only happens when&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Buffer is full&lt;/li&gt;
&lt;li&gt;Event happens between &lt;code&gt;m_queue_start_index = next_index(m_queue_start_index);&lt;/code&gt; and &lt;code&gt;event_handler(p_event_data, event_data_size);&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SDK13 app_sched_execute() infinite recursion</title><link>https://devzone.nordicsemi.com/thread/83052?ContentTypeID=1</link><pubDate>Tue, 11 Apr 2017 17:27:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:efbd69a1-86a1-4027-a030-98544cddebf3</guid><dc:creator>Mike Voytovich</dc:creator><description>&lt;p&gt;Thanks for the reply Hung!  But, I don&amp;#39;t quite understand what you mean by &amp;quot;if the event is freed before executing&amp;quot;.  The line of code: &lt;code&gt;m_queue_start_index = next_index(m_queue_start_index);&lt;/code&gt; is not freeing anything; it&amp;#39;s just updating the &lt;code&gt;m_queue_start_index&lt;/code&gt;, right?
So how could updating &lt;code&gt;m_queue_start_index&lt;/code&gt; before calling the handler corrupt the event data?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: SDK13 app_sched_execute() infinite recursion</title><link>https://devzone.nordicsemi.com/thread/83050?ContentTypeID=1</link><pubDate>Tue, 11 Apr 2017 12:22:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2934294c-4056-4dd4-9b42-457c851b44a7</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Mike,&lt;/p&gt;
&lt;p&gt;The app_sched_execute() was made to be called from inside main loop, not for calling recursively from within the event itself.
You can find this in the description:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/**@brief Function for executing all scheduled events.
 *
 * @details This function must be called from within the main loop. It will execute all events
 *          scheduled since the last time it was called.
 */
void app_sched_execute(void);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The information I got from our developer was that we updated the code from SDK v12 to fix a potential bug that if the event is freed before executing, the data comes with that event can be corrupted before it is executed. That&amp;#39;s why we swapped the order.&lt;/p&gt;
&lt;p&gt;Calling app_sched_execute() from inside an event handler is not recommended.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>