Occasional garbage data received by event handler

I occasionally see garbage in the data argument for my event handler.

I’m running with:

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

The subscribe/handler parts of the code are of the form:

Particle.subscribe("myEvent", handleNetworkEvent, MY_DEVICES);
void handleNetworkEvent(const char *event, const char *data) {
  Serial.print("got network event: ");
  Serial.print(event);
  Serial.print(": ");
  Serial.println(data);
  Serial.flush();
}

Mostly works, but occasionally (every 10 or 40 events or so) see garbage for the data argument. The Particle Console Logs show that the event data is clean (no garbage) in the cloud.

Using firmware 0.6.0

Any clues?

How frequently are you calling Particle.process()?
Are you also using Particle.publish() in your code?

If you don’t attend to the event handler often enough by calling Particle.process() before a Particle.publish() may corrupt the shared buffer you may see such results.

I believe I’m calling Particle.process quite frequently (first statement in my loop(), and also in the other places where I may have long-running loops).

However, I am occasionally making Particle.publish() calls. Code currently makes a Particle.process() call immediately after the Particle.publish() calls.

Sounds like you’re suggesting calling Particle.process() immediately before the Particle.publish()?

Your mention of a “shared buffer” is interesting/concerning. Can you elaborate? Or is this documented somewhere?

Also, in case it’s relevant, the subscribed event I’m handling occurs about once every 2 seconds. The event I’m occasionally publishing happens about once every 20 seconds.

Thanks!

I don’t think it’s documented and I haven’t seen this happen often with isolated calls of Particle.publish() and incoming events - but that might be due to most people using AUTOMATIC or SEMI_AUTOMATIC mode.
But it seems to be a common problem when you have a Particle.publish() inside your subscribe handler before you deal with data.

Maybe @jvanier can chime in on this - and also whether Particle.publish() and Particle.subscribe() will get (or have got in a more recent update) seperate buffers.

But as a quick attempt to fix the problem one (or a few) call(s) of Particle.process() before the publish might cure your immediate issue.

I’ve added a Particle.process() immediately before my occasional Particle.publish() call, but am still getting occasional garbage on the inbound event data. :frowning:

If it’s relevant, the Particle Console Logs do show the incoming event I’m handling and the event I’m occasionally publishing occurring very close to each other in time (less than a second apart – according to the Console Log).