Timeout handler for agent.on() in device

Is there a way to create a timeout handler for agent.on()… code in the device.
My imp sometimes seems not to get the response from the agent and and remains online while it is supposed to get the response on time and go back to sleep.
When this happens, I see “Online” status in the IDE and green led keeps slowly blinking. The imp starts to work normally when I power cycle it or click Build on the IDE (download code again).

This afternoon, I have same situation, and the log shows the following:

2014-09-19 13:00:04 UTC+10 [Device] Time to send data…
2014-09-19 13:00:04 UTC+10 [Device] NV length: 16
2014-09-19 13:00:05 UTC+10 [Agent] {“saved”:1…}
2014-09-19 13:00:22 UTC+10 [Status] Device Booting; 17.92% program storage used
2014-09-19 14:09:22 UTC+10 [Status] Device Booting; 17.92% program storage used
2014-09-19 14:28:37 UTC+10 [Status] Device Booting; 17.92% program storage used
2014-09-19 15:41:24 UTC+10 [Status] Device disconnected
2014-09-19 15:41:35 UTC+10 [Status] Device Booting; 17.92% program storage used
Number of … Similar entres
2014-09-19 20:16:22 UTC+10 [Status] Device Booting; 17.92% program storage used

After this, I “Build” again (no power recycle) and the imp is happy.

Need a bit more info? Yes, you can use imp.wakeup to schedule a timeout which will fire if an event doesn’t happen (you make the event happening cancel the wakeup).

The Bullwinkle library demonstrates agent/device messaging with timeouts