SystemSleepConfiguration move assignment appears to leak wake sources (6.1.1 – 6.4.1, develop)

I am starting to get back into the swing of things here is balmy Singapore and I have seen some great progress in deviceOS in the past year. I did not see a place to open an issue on the deviceOS GitHub repo, so I am posting this here. We run a fleet of Borons doing occupancy counting on battery and solar, and the sleep API has made a big difference to what these devices can do on a small battery. I wanted to share something we ran into, in case it's useful to the team or to anyone else seeing slow memory loss on sleepy devices. I may well have missed something, so corrections are welcome.

What we saw

Free memory on our devices fell steadily while they were awake and busy, about 117–141 bytes per wake cycle. On a busy site that's thousands of wake cycles a day, so a device could go from about 77 KB free to about 24 KB in a couple of hours. It tracked the number of sleep/wake cycles rather than anything else the device was doing.

What we think is happening

We kept one global SystemSleepConfiguration and reset it before each sleep:

cpp

SystemSleepConfiguration config;   // global, reused

// before each sleep:
config = SystemSleepConfiguration();
config.mode(SystemSleepMode::ULTRA_LOW_POWER)
      .gpio(D2, RISING)
      .duration(30s);
System.sleep(config);

Reading system/inc/system_sleep_configuration.h, the move assignment operator copies the new configuration's hal_sleep_config_t over the existing one and takes ownership of its wakeup_sources list, but it doesn't appear to free the list the object was already holding. Only the destructor frees it. A global is never destroyed, so each reassignment seems to leave the previous wake-source list allocated.

As far as we can tell from the source, this is the same in 6.1.1, 6.3.3, 6.3.5, 6.4.1 and current develop.

A minimal sketch that shows it

cpp

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SerialLogHandler logHandler;

SystemSleepConfiguration config;   // reused across sleeps

void setup() {
}

void loop() {
    config = SystemSleepConfiguration();     // move-assign a fresh configuration
    config.mode(SystemSleepMode::ULTRA_LOW_POWER)
          .gpio(D2, RISING)
          .duration(5s);
    System.sleep(config);

    Log.info("free memory: %lu", System.freeMemory());
    delay(500);
}

On our devices, free memory drops on every cycle. The size of the drop depends on how many wake sources are configured.

Workaround

Using a fresh local SystemSleepConfiguration for each sleep, as the examples in the docs do, stops it completely:

cpp

void goToSleep() {
    SystemSleepConfiguration config;   // local: freed when it goes out of scope
    config.mode(SystemSleepMode::ULTRA_LOW_POWER)
          .gpio(D2, RISING)
          .duration(30s);
    System.sleep(config);
}

After that change, our busiest device went from losing well over 100 bytes per cycle to about zero over 4,000 cycles. So we're fine now. I'm posting mainly in case reusing a configuration object is something other people do too.

A possible fix

If this reading is right, having the move assignment free (or swap) the wake-source list it already holds before taking over the other object's would make reuse safe. I'm happy to put that together as a pull request if it would help, or to test a build.

Thanks again for all the work on Device OS, and for taking a look.

This is a bug and should be fixed in 6.6.0. Your current local variable solution is a good workaround that will work now and in the future as well.

Thank you for the confirmation @rickkas7 !

This is fixed in Device OS 6.6.0 (just released) via PR 2968.