Page 1 of 5

Priority order violation - Asserion fails  Topic is solved

Posted: Wed Jan 31, 2018 11:00 pm
by fotis
Hi,

I have a assertion failing in Chibios code, and I am trying to troubleshoot it.

The check condition is the following:

Code: Select all

  /* The following condition can be triggered by the use of i-class functions
     in a critical section not followed by a chSchResceduleS(), this means
     that the current thread has a lower priority than the next thread in
     the ready list.*/
  chDbgAssert((ch.rlist.queue.next == (thread_t *)&ch.rlist.queue) ||
              (ch.rlist.current->prio >= ch.rlist.queue.next->prio),
              "priority order violation");



What I would expect is that somewhere, in my thread(?) code there is a missing call to chSchResceduleS().

As I understand from the documentation, I only need to call chSchResceduleS() after a call to an I-class function, when in thread code. I have triple-checked that this is NOT my problem. In fact, I reproduce this issue without even a single call to an I-class function!

Are there any other cases of needing to call chSchResceduleS()? Are there any other causes of failure for this assertion, apart from what is written in the comment? Any other way to troubleshoot this?

Re: Priority order violation - Asserion fails

Posted: Wed Jan 31, 2018 11:05 pm
by Giovanni
Hi,

Explore upward your stack trace, it will likely point to the problem. Post it here if in doubt.

Where is that chSysUnlock() called from?

Giovanni

Re: Priority order violation - Asserion fails

Posted: Wed Jan 31, 2018 11:07 pm
by fotis
As a hint:

The thread that has higher priority and gets "neglected" is a very simple one. It just blinks an LED, and feeds the watchdog.

The code of the thread is the following:

Code: Select all

void status_th(void __attribute__ ((__unused__)) * arg)
{
   while(1)
   {
      //Feed the watchdog timer.
      SoftWatchdog_tick();

      //Blink the status LED.
      Led_tick(&statusLed);

      chThdSleep(MS2ST(STATUS_LOOP_TIME));
   }
}



From the above it is clear that this thread gets in a waiting state due to a call to chThdSleep(MS2ST(STATUS_LOOP_TIME)). No semaphores, events etc, that may need rescheduling, or may cause priority-inversion etc...

I would expect this thread to run without problems, due to the SysTick timer handler. This thread is either sleeping, or running. Each time the systick timer handler is executed, the system ticks counter is incremented, and a reschedule takes place.

How it is possible for this thread to finish its sleep (due to expiration of the delay STATUS_LOOP_TIME, as counted by systick timer, and not get executed, in favor of a lower priority thread?

Re: Priority order violation - Asserion fails

Posted: Wed Jan 31, 2018 11:19 pm
by fotis
The assertion fails when another thread (of a lower priority), manipulates a mutex (either locks it or unlocks it).

If by stack trace you mean the functions call stack, as displayed in Eclipse debug view, there is nothing suspicious there. If you mean anything else, please explain me where to find it.

Re: Priority order violation - Asserion fails

Posted: Wed Jan 31, 2018 11:24 pm
by Giovanni
When a very simple piece of code does not seem to do what it is supposed to do then, probably, you are looking at the wrong thing.

Similar loops are in all demos and simply work, see blinker threads.

This could help: http://www.chibios.org/dokuwiki/doku.ph ... ebug_guide

Giovanni

Re: Priority order violation - Asserion fails

Posted: Tue Jun 26, 2018 6:52 pm
by fotis
Hi,

this still bugs me. I couldn't get a solution, and then I left it due to lack of free time.

No I am back at it. I tried again to track this down with no luck.

Can you please provide me some info on how to get a stack trace, and upload it here?

Re: Priority order violation - Asserion fails

Posted: Wed Jun 27, 2018 10:41 am
by Giovanni
It depends on the debugger you are using, is it Eclipse? if so then it is the box in the upper left corner of the debugger view.

Giovanni

Re: Priority order violation - Asserion fails

Posted: Wed Jun 27, 2018 6:33 pm
by fotis
I have checked this multiple times, but it didn't help me.

Either way, in case it is helpful for you, here you are.

Image

Re: Priority order violation - Asserion fails

Posted: Wed Jun 27, 2018 8:48 pm
by steved
Assuming that's lwIp you're running, I have a vague recollection of a similar problem. In my case it was down to insufficient stack allocation for one of the tasks; a networking-related one IIRC.
(The Chibi plugin for Eclipse will show you stack usage)

Note that you can actually use the debugger to see the states of variables at any point in the execution stack, just by highlighting the relevant level - that can sometimes give clues.

Re: Priority order violation - Asserion fails

Posted: Wed Jun 27, 2018 9:08 pm
by Giovanni
The problem could be originated in another place, are you using any I-class API (those whose name ends with an "I") from thread context?

Giovanni