Problem with OTA - 13 red blinks

Hi everyone.
I need some help with OTA firmware updates.
I already have a product created, added devices, uploaded firmware versions.
Everything went well so far.
The problem happens when I send a firmware update (flash and lock now or flash and lock on reset).

The Photon starts to try to update himself, but a red SOS happens, followed by 13 red blinks, immediately another SOS happens, followed by 13 red LED blinks, then the device restart and all happen again. It never update.

I’m more than worried about this because I plan to distribute firmware updates to the final devices and I cannotfigure out what’s wrong. The code otherwise works like a charm. Never had a problem in 2 months of constant operation.

I’m using:

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

Any help will be really appreciated.
Thank you,
Diego

A quick update, if I manually put the device in safe mode (breathing magenta), the update occurs immediately and without any issues…

Another update: disabled SYSTEM_THREAD and it’s working

The SOS+13 indicates a stack overflow which can happen if you keep calling functions asynchronously that won’t return (in time) for some resaon but pile up data on the stack.
Other reasons are recursions or “huge” amounts of automatic variables that don’t get “cleaned up” in time.

Hi @ScruffR and thank you for your reply.
This only happen when I send an OTA update and with SYSTEM_THREAD(ENABLED).

What I could do?
Thank you

That would fit the bill.
Multithreading does allow for async function calls and OTA does use some stack and impacts timing too.