6

I have a spreadsheet of about 150 Mosquitto log entries. It is paired with 30 messages I published, matched in time. That is, there are about 5 times as many Mosquitto entries as published items. Many seem to indicate that things I published timed out. Others say "Socket error". But all the subscribers got the messages.

My problem is, after publishing 2 or 3 per hour for 2 or 3 days, suddenly none of the subscribers get anything I publish. But the log "looks the same" as far as I can tell. But there are thousands of entries.

EDIT: The link will bring up my spreadsheet. I edited it to just 44 rows. There are two vertical sections. Messages I published on the left and Mosquitto log entries on the right. Sorry, when I pasted them into the spreadsheet the delimiter characters spread them across many columns. The first 3 or so pubs seem fine but then the 12:13:26 pub has a socket error although the subscriber received the pub. After that things seem to get worse with timeouts also.

Mosquitto Capture Short

The rest of the 100 or so rows (that I didn't include) are very similar. Timeouts and socket errors. But the subscribers get the pubs.

The pubs come from a C program I wrote that runs on my Raspberry Pi. The subscribers are items in OpenHAB that is also on the same RPi. Here is the C code that does the publishing:

void publish(char *Topic, char *action)
{
    mosquitto_lib_init();
    mosq = mosquitto_new(NULL,true,NULL);
    mosquitto_loop_start(mosq);
    mosquitto_connect_async(mosq,MQTT_Host,MQTT_Port,1);
    mosquitto_publish(mosq,NULL,Topic,strlen(action),action,2,false);
    printf("Mosquitto Sending: %s %s to %s:%d\n",Topic,action,MQTT_Host,MQTT_Port);
}
1
  • 1
    Edit the question to actually include some examples of the logs you are talking about
    – hardillb
    Nov 21, 2019 at 7:41

1 Answer 1

2

A spreadsheet really isn't the best way to work with these logs, it makes them really hard to read properly. Next time please just post the text and use the {} option in the tool bar to format it as code.

There is nothing too obviously wrong in the log entries you've provided.

That said, the C code worries me a lot, it looks like you are creating a new MQTT client for every publish event and then not shutting them down or cleaning up the resources properly after. You are very likely leaking client structures and network threads. These are the sort of things that will cause things to crash at some point.

Firstly assuming the application that is publishing the messages is long running it should create a single MQTT client object and reuse it over the life time of the application.

This means the initialisation of the library, the creation of the client, the starting of the network loop and the connection should all be moved outside the publish function and the mosq should be variable this is in a more global scope so it can be accesses later.

Also using 1 second for a KeepAlive value is just going to generate a huge amount of extra load on your broker (especially for a client that you are currently leaking and not cleaning up). A better value would be 60.

You can see the KeepAlive value in the logs in the connect line:

12:23:31pm New client connected from 192.168.1.115 as mosq-0g6AG1QuASdsrJZqAz (p2,  c1, k1).
  • p is the protocol version where 1 -> MQTT3.1 2 -> MQTT3.11 5 -> MQTT5
  • c is if the clean session flag is set
  • k is the keepalive value

p.s. No there is no description of the log output except the src code

3
  • Sorry about the spreadsheet. I was trying to correlate my publishes with the mosquitto log.using time stamps. I couldn't find a description of the log entries. I will research your problems with my C code and correct them. It could take some time. I just copied the steps from some posting I found somewhere. I'm not sure what you mean about a "description of the log output". I hope I encounter that in my research.
    – PeteC
    Nov 22, 2019 at 17:40
  • I'm saying that the only documentation of the logs is the source code that creates them
    – hardillb
    Nov 25, 2019 at 8:53
  • hardillb, I followed your advice and moved the creation associated items to StartUp. It has been running for two weeks, solid as a rock where as before it couldn't last 3 days. Kudos to you.
    – PeteC
    Dec 9, 2019 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.