Imp stuck in while(true) loop, won't download new code

I have an Imp that has been running some code fine for a couple weeks, but yesterday it decided to do something different. I found that it is stuck in a while (true) {} loop because it never gets to the break; line. I have made a change to the application to resolve this, but the Imp isn’t picking up the new code. I know it is still in the loop because there is a server.log() in the loop that is flooding the log in the IDE.

I’m certain that physically power cycling the Imp will cause it to check for new code. However, I thought the Imp is supposed to check for new code when it communicates with the Imp servers, which it is doing when it sends the log messages. Does the imp only check for new code when it is idle?

This Imp is easy to get to, so physically power cycling it is simple. However, I can imagine Imps that are not easily accessible. It would be unfortunate to have one stuck like this even though it is online and communicating with the servers.

I have solve in this way - restore running version and re-power imp.

Because the squirrel code runs at high priority, the imp only services network traffic when the squirrel code has yielded - which means an infinite loop can only be broken out of by a power cycle. Server.log will send to the server but won’t check the input buffer, hence doesn’t help.

In the wishlist is an automatic reset when this happens, as imp code should never busy loop like this, so recovery is the the best option.

Thanks for the insight, Hugo.

:frowning: I just did this on accident, unfortunately the imp is a few miles away from where I am. It would be ideal if there was a way to prevent this from locking it up.

Really, you should never use things like while(true) on the imp - it’s best to stick to event-driven stuff. Check out these guides from our developer center:
http://electricimp.com/docs/resources/eventprogramming/
http://electricimp.com/docs/resources/wakeupguide/

(using imp.wakeup() gives the operating system time to process network traffic)

My use wasn’t quite as intentional as while (true). What I tried, (with immediate regret afterward) is to wrap a while loop around a global flag to tell me when data was done being received from the UART. I was thinking that the OS would still process the interrupts for incoming UART data in addition to the while loop but the while loop took all of the CPU’s attention!

Hmm maybe if i toss in a brief command to sleep…

It all relates to trying to accomplish the goal detailed in this forum posthttps://discourse.electricimp.com/discussion/3428/read-uart-via-agent-function#latest

lots of options…

On the imp, Squirrel callbacks are really event handlers, not interrupt handlers: Squirrel never “interrupts” other Squirrel. Each new event handler doesn’t start until the previous one has run to completion. In your case the imp was continuing to service UART hardware interrupts and stash the incoming data in the FIFO, but your Squirrel event handler wasn’t getting called because Squirrel was already running. Adding calls to imp.sleep() won’t help, because that’s a synchronous sleep, and Squirrel will still count as “already running”. Perhaps check out this thread for (what’s in effect) a way of emulating the multiple-Squirrel-thread technique that you’re trying to use here.

Peter

Until that day where I can program everything perfectly and foresee everything, I am looking forward to a build-in watchdog on the imp platform.

Yep, this is coming. We already have a system level watchdog, but do have plans for one to catch infinite loops that last “too long” (maybe 60 seconds?) on the imp, allowing remote recovery from a bad squirrel deploy.

Hi,
It looks that i have same problem. The agent code is running in a loop. I can not update the code anymore.
Is there a solution?

@FredSchmidt your case was unusual in that you had an agent that was running then crashing every 10 seconds, and you had deleted the model that was looping and hence had no way to correct the error (I just fixed that for you this morning).

This discussion is more about imps that are running squirrel in a tight loop and never checking the network. Both issues will be addressed in a future release.

@Hugo

I did the exact same thing as @fredschmidt. Could you please help me reset my imp back to factory?

If you tell me the model you deleted, then I can un-delete it so you can recover it (by reassigning your imp to that model, and fixing the bug in the code)