Stack Overflow Error

Hi,

I’ve been getting stack overflow errors when running the Particle Electron. I’m using the watchdog timer and have increased its stack size to 4kb with the following code:

// this is where we initialize the watchdog  
ApplicationWatchdog wd(1000, wd_reset, 4096);

// this is the function that we call when the watchdog timer is set off
void wd_reset() {
  int ret = Cellular.command(30000, "AT+CFUN=16\r\n");
  if (ret == RESP_OK) System.reset(5);
  else System.reset(1005);
}

Is it possible that I need to increase the watchdog timer stack size even more? If not, how do I go about looking for the code segments that induce the stack overflow?

Thanks!

I can’t see any instruction in that snippet that would increase the stack size for the watchdog thread.
Update: After OP was updated the comment was rendered irrelevant

@Adam42, calling Cellular.command() may be the issue. You may want to try and comment it out to see if you still get the error. The stack overflow may be in the system thread and not the wd_reset() thread.

What do these calls to System.reset with parameters mean? As far as I can tell from the documentation, System.reset() takes no parameters.
https://docs.particle.io/reference/firmware/electron/#reset--3

There is an undocumented overload that takes some additional data for extra info about the reset reason

void SystemClass::reset(uint32_t data)
{
    HAL_Core_System_Reset_Ex(RESET_REASON_USER, data, nullptr);
}
1 Like

@ScruffR: I just edited the post to include the initialization of the watchdog. The stack size is the 4th input, in this case 4096.

@peekay123: I will definitely try commenting the Cellular.command out. If the stack overflow is on the system thread, do you have any suggestions on how to figure out what exactly is the biggest contributor?

any solution to this, having same issue.

The default stack size of the application watchdog is too small in 0.7.0. This issue tracks the problem but you can work around it by doing:

ApplicationWatchdog wd(10000, System.reset, 1536);

However, you should not do what the original poster did. Cellular.command is almost certain to fail if you’ve hit the application watchdog timer, and it will require too much stack space. The way around that is to only call System.reset() from your application watchdog callback, then if you fail to connect after reset, then AT+CFUN=16 from the working state.

2 Likes