Skip to content

Commit

Permalink
Fix issue with functions scheduled from scheduled functions (#6770)
Browse files Browse the repository at this point in the history
Calling schedule_function() from a scheduled function would result in an infinite loop, because the list traversal would never end.
  • Loading branch information
dok-net authored and devyte committed Nov 14, 2019
1 parent 2f26d94 commit 6f3c57b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,33 @@ void run_scheduled_functions()
{
esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms

while (sFirst)
// prevent scheduling of new functions during this run
auto stop = sLast;
bool done = false;
while (sFirst && !done)
{
done = sFirst == stop;

sFirst->mFunc();

{
// remove function from stack
esp8266::InterruptLock lockAllInterruptsInThisScope;

auto to_recycle = sFirst;
sFirst = sFirst->mNext;
if (!sFirst)

// removing rLast
if (sLast == sFirst)
sLast = nullptr;

sFirst = sFirst->mNext;

recycle_fn_unsafe(to_recycle);
}

if (yieldNow)
{
// because scheduled function are allowed to last:
// because scheduled functions might last too long for watchdog etc,
// this is yield() in cont stack:
esp_schedule();
cont_yield(g_pcont);
Expand Down

0 comments on commit 6f3c57b

Please sign in to comment.