Particle.publish() from a thread returns error but still publishes

Hi,

I have a Boron running 1.5.1, publishing from a thread with this code:

    bool publish_status;
    publish_status = Particle.publish(EVENT_NAME, DATA, 2, PRIVATE, WITH_ACK);
    if (!publish_status)
    {
        Log.error("ERROR publishing message");
    }

It consistently, like 100% of the time, publishes successfully to the cloud, while 100% of the time printing:

0068541811 [app] ERROR: ERROR publishing message

However, in the same firmware but this time from the main loop(), I have the following code:

  bool publish_status;
  publish_status = Particle.publish(EVENT_NAME, DATA, 2, PRIVATE, WITH_ACK);
  if (!publish_status)
  {
      Log.info("ERROR: Publishing message");
  }

Please disregard that historical TTL=2 parameter

In this case, the device publishes 100% of the time successfully, while NEVER printing an error.

Is there anything I'm missing or could be doing wrong from the thread that the Particle.publish() reports an error back?
Thanks!

It's a bug. You can see how to work around it here by checking isDone() in a loop.

1 Like

Hi and million thanks for referencing the code with a workaround.

Follow up question:

I am observing today that the device is doing the following:

  • every publish from main is seen exactly once on the Particle console of the device
  • every publish from the thread is seen duplicated for every one of them, albeit with a difference of 10~20 msec

Example:

Published by 186867126783186723 on 2023-12-01T16:00:41.255Z
Published by 186867126783186723 on 2023-12-01T16:00:41.263Z

I have not coded yet the workaround proposed in your post, this is the first time I see this behaviour on this device (or any device for that matter).

I thought for a second that the device or my code was malfunctioning, but checking the serial console logs of the device only shows one single error per publish (as reported in my first post on this topic). So the device seems to be publishing one message as usual.

I also thought that my browser was malfunctioning and duplicating the publishes, but opening another browser or tab still shows double the publishes (and the timestamp is NOT the same, differs by 10~20 msec).

Can this "WITH_ACK from a thread bug" be playing a role in what I am observing or this is something else?

Thanks again

EDIT: I had somehow managed to duplicate the line with the publish - that's why I got those duplicated publishes - apologies.

It's hard to say. But definitely implement the fix because every publish in Tracker Edge and Monitor Edge occurs from a thread using that technique, as does PublishQueueAsyncRK and PublishQueuePosixRK, so the technique is definitely necessary and does work.