xStreamBufferReset is not interrupt-safe

lpmay wrote on Friday, May 18, 2018:


I think I may have stumbled into a bug with xStreamBufferReset, and how it interacts with xStreamBufferSendFromISR. Perhaps it’s just a misunderstanding on my, part and if so I think the documentation could probably be a little more clear.

Basically, I thought it was always safe to call xStreamBufferReset, but in fact if FromISR functions are used it is not safe, unless interrupts are disabled. Since xStreamBufferReset ultimately clears the underlying stream structure and then re-populates it with interrupts enabled, it is possible for an interrupt to fire while the structure is not in a valid state. If that interrupt tries to access the StreamBuffer, even with one of the fromISR functions that access will fail (and configASSERT).

In pseudo-code, my setup is something like this:

void ISR(void)
    xStreamBufferSendFromISR(/* new byte */);
void process_bytes(void)
/* start with a fresh stream */
... receive from stream multiple times, continue if error encountered

In my opinion, there should be a specific note in the documenation that xStreamBufferReset is not safe to use with the FromISR functions, or better yet the implementation should be changed such that xStreamBufferReset is safe to use with the FromISR functions.

Am I missing something more fundamental? Should I open a support ticket? Thanks for any help!

rtel wrote on Friday, May 18, 2018:

Thanks for pointing this out - I have added a crticial section within the xStreamBufferReset() function to make it safe from interrupt access while it is being reset. The code will be checked into the public SVN repo once it has been tested.

lpmay wrote on Saturday, May 19, 2018:

Hi Richard, thanks for your reply. What is the best way for me to get the fix for my current project? Should I wait for a new release to come out? My current work-around is that my code wraps it’s call to xStreamBufferReset() with an interrupt enable/disable, but it would be nice to delegate this responsibility back to the updated xStreamBufferReset().

rtel wrote on Sunday, May 20, 2018:

That workaround is fine - mine just has the critical section inside the
function rather than outside.