They (compiler optimizations) do result in weird things sometimes and here is one such found with FreeRTOS for ARM Cortex-M3 port. This is as result of “Tail Call Optimization” as I had mentioned it in detail in my last post here.
Such bugs can take a lot of time to debug and still keep you quite clueless because they manifest as if they are HeisenBug.
Nevertheless here is detailed summary of that bug,
To be precise above issue is actually compiler optimization magic coupled with how PendSV is handled by ARM Cortex-M3 NVIC.