Skip to content

Commit

Permalink
Fix unref'd messages being saved to the persistence file.
Browse files Browse the repository at this point in the history
This was leaving dangling messages that were never freed.

Closes #389. Thanks to pjchx.
  • Loading branch information
ralight committed Feb 13, 2019
1 parent f9f3fdb commit 321e566
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Broker:
- Fix include_dir not sorting config files before loading. This was partially
fixed in 1.5 previously.
- Improve documentation around the `include_dir` option. Closes #1154.
- Fix case where old unreferenced msg_store messages were being saved to the
persistence file, bloating its size unnecessarily. Closes #389.

Library:
- Fix `mosquitto_topic_matches_sub()` not returning MOSQ_ERR_INVAL for
Expand Down
20 changes: 15 additions & 5 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **s
}


void db__msg_store_compact(struct mosquitto_db *db)
{
struct mosquitto_msg_store *store, *next;

store = db->msg_store;
while(store){
next = store->next;
if(store->ref_count < 1){
db__msg_store_remove(db, store);
}
store = next;
}
}


static void db__message_remove(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto_client_msg **msg, struct mosquitto_client_msg *last)
{
if(!context || !msg || !(*msg)){
Expand Down Expand Up @@ -1019,8 +1034,3 @@ void db__limits_set(int inflight, unsigned long inflight_bytes, int queued, unsi
max_queued_bytes = queued_bytes;
}

void db__vacuum(void)
{
/* FIXME - reimplement? */
}

2 changes: 1 addition & 1 deletion src/mosquitto_broker_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ void db__msg_store_add(struct mosquitto_db *db, struct mosquitto_msg_store *stor
void db__msg_store_remove(struct mosquitto_db *db, struct mosquitto_msg_store *store);
void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **store);
void db__msg_store_clean(struct mosquitto_db *db);
void db__msg_store_compact(struct mosquitto_db *db);
int db__message_reconnect_reset(struct mosquitto_db *db, struct mosquitto *context);
void db__vacuum(void);
void sys_tree__init(struct mosquitto_db *db);
void sys_tree__update(struct mosquitto_db *db, int interval, time_t start_time);

Expand Down
8 changes: 8 additions & 0 deletions src/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ static int persist__message_store_write(struct mosquitto_db *db, FILE *db_fptr)

stored = db->msg_store;
while(stored){
if(stored->ref_count < 1){
stored = stored->next;
continue;
}

if(stored->topic && !strncmp(stored->topic, "$SYS", 4)){
if(stored->ref_count <= 1 && stored->dest_id_count == 0){
/* $SYS messages that are only retained shouldn't be persisted. */
Expand Down Expand Up @@ -982,6 +987,9 @@ int persist__restore(struct mosquitto_db *db)
HASH_DELETE(hh, db->msg_store_load, load);
mosquitto__free(load);
}

db__msg_store_compact(db);

return rc;
error:
err = strerror(errno);
Expand Down

0 comments on commit 321e566

Please sign in to comment.