Skip to content

Commit

Permalink
QueryCache: Fix flushing on server list changes
Browse files Browse the repository at this point in the history
The query cache wasn't properly flushing on server list changes
and was attempting to flush even when the server list didn't
actually change.

Fix By: Brad House (@bradh352)
  • Loading branch information
bradh352 committed May 30, 2024
1 parent 1dff8f6 commit a6c8fe6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/lib/ares_qcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ static void ares__qcache_expire(ares__qcache_t *cache,

while ((node = ares__slist_node_first(cache->expire)) != NULL) {
const ares__qcache_entry_t *entry = ares__slist_node_val(node);
if (entry->expire_ts > now->sec) {

/* If now is NULL, we're flushing everything, so don't break */
if (now != NULL && entry->expire_ts > now->sec) {
break;
}

Expand All @@ -158,9 +160,7 @@ static void ares__qcache_expire(ares__qcache_t *cache,

void ares__qcache_flush(ares__qcache_t *cache)
{
ares_timeval_t now;
memset(&now, 0, sizeof(now));
ares__qcache_expire(cache, &now);
ares__qcache_expire(cache, NULL /* flush all */);
}

void ares__qcache_destroy(ares__qcache_t *cache)
Expand Down
20 changes: 15 additions & 5 deletions src/lib/ares_update_servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,10 @@ static ares_bool_t ares__server_in_newconfig(const struct server_state *server,
return ARES_FALSE;
}

static void ares__servers_remove_stale(ares_channel_t *channel,
ares__llist_t *srvlist)
static ares_bool_t ares__servers_remove_stale(ares_channel_t *channel,
ares__llist_t *srvlist)
{
ares_bool_t stale_removed = ARES_FALSE;
ares__slist_node_t *snode = ares__slist_node_first(channel->servers);

while (snode != NULL) {
Expand All @@ -683,9 +684,11 @@ static void ares__servers_remove_stale(ares_channel_t *channel,
/* This will clean up all server state via the destruction callback and
* move any queries to new servers */
ares__slist_node_destroy(snode);
stale_removed = ARES_TRUE;
}
snode = snext;
}
return stale_removed;
}

static void ares__servers_trim_single(ares_channel_t *channel)
Expand All @@ -702,6 +705,7 @@ ares_status_t ares__servers_update(ares_channel_t *channel,
ares__llist_node_t *node;
size_t idx = 0;
ares_status_t status;
ares_bool_t list_changed = ARES_FALSE;

if (channel == NULL) {
return ARES_EFORMERR;
Expand Down Expand Up @@ -747,13 +751,17 @@ ares_status_t ares__servers_update(ares_channel_t *channel,
if (status != ARES_SUCCESS) {
goto done;
}

list_changed = ARES_TRUE;
}

idx++;
}

/* Remove any servers that don't exist in the current configuration */
ares__servers_remove_stale(channel, server_list);
if (ares__servers_remove_stale(channel, server_list)) {
list_changed = ARES_TRUE;
}

/* Trim to one server if ARES_FLAG_PRIMARY is set. */
if (channel->flags & ARES_FLAG_PRIMARY) {
Expand All @@ -765,8 +773,10 @@ ares_status_t ares__servers_update(ares_channel_t *channel,
channel->optmask |= ARES_OPT_SERVERS;
}

/* Clear any cached query results */
ares__qcache_flush(channel->qcache);
/* Clear any cached query results only if the server list changed */
if (list_changed) {
ares__qcache_flush(channel->qcache);
}

status = ARES_SUCCESS;

Expand Down

0 comments on commit a6c8fe6

Please sign in to comment.