Skip to content

Commit

Permalink
fix(mdns): Fix API races when removing all services
Browse files Browse the repository at this point in the history
Fixes **API race issue** (described in 8a69050) for API
mdns_service_remove_all()
  • Loading branch information
david-cermak committed Aug 19, 2024
1 parent 643dc6d commit c48ae39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
49 changes: 18 additions & 31 deletions components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -5148,8 +5148,6 @@ static void _mdns_free_action(mdns_action_t *action)
*/
static void _mdns_execute_action(mdns_action_t *action)
{
mdns_srv_item_t *a = NULL;

switch (action->type) {
case ACTION_SYSTEM_EVENT:
perform_event_action(action->data.sys_event.interface, action->data.sys_event.event_action);
Expand All @@ -5169,19 +5167,6 @@ static void _mdns_execute_action(mdns_action_t *action)
_mdns_server->instance = action->data.instance;
_mdns_restart_all_pcbs_no_instance();

break;
case ACTION_SERVICES_CLEAR:
_mdns_send_final_bye(false);
a = _mdns_server->services;
_mdns_server->services = NULL;
while (a) {
mdns_srv_item_t *s = a;
a = a->next;
_mdns_remove_scheduled_service_packets(s->service);
_mdns_free_service(s->service);
free(s);
}

break;
case ACTION_SEARCH_ADD:
_mdns_search_add(action->data.search_add.search);
Expand Down Expand Up @@ -5226,6 +5211,7 @@ static void _mdns_execute_action(mdns_action_t *action)
free((char *)action->data.delegate_hostname.hostname);
free_address_list(action->data.delegate_hostname.address_list);
}
xSemaphoreGive(_mdns_server->action_sema);
break;
case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname,
Expand Down Expand Up @@ -5789,6 +5775,7 @@ esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t
free(action);
return ESP_ERR_NO_MEM;
}
xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY);
return ESP_OK;
}

Expand Down Expand Up @@ -6453,27 +6440,27 @@ esp_err_t mdns_service_remove(const char *service_type, const char *proto)

esp_err_t mdns_service_remove_all(void)
{
if (!_mdns_server) {
return ESP_ERR_INVALID_ARG;
}
MDNS_SERVICE_LOCK();
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(_mdns_server, ESP_ERR_INVALID_ARG, done, TAG, "Invalid state");
if (!_mdns_server->services) {
MDNS_SERVICE_UNLOCK();
return ESP_OK;
goto done;
}
MDNS_SERVICE_UNLOCK();

mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
if (!action) {
HOOK_MALLOC_FAILED;
return ESP_ERR_NO_MEM;
}
action->type = ACTION_SERVICES_CLEAR;
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
free(action);
return ESP_ERR_NO_MEM;
_mdns_send_final_bye(false);
mdns_srv_item_t *services = _mdns_server->services;
_mdns_server->services = NULL;
while (services) {
mdns_srv_item_t *s = services;
services = services->next;
_mdns_remove_scheduled_service_packets(s->service);
_mdns_free_service(s->service);
free(s);
}
return ESP_OK;

done:
MDNS_SERVICE_UNLOCK();
return ret;
}

/*
Expand Down
1 change: 0 additions & 1 deletion components/mdns/private_include/mdns_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ typedef enum {
ACTION_SYSTEM_EVENT,
ACTION_HOSTNAME_SET,
ACTION_INSTANCE_SET,
ACTION_SERVICES_CLEAR,
ACTION_SEARCH_ADD,
ACTION_SEARCH_SEND,
ACTION_SEARCH_END,
Expand Down

0 comments on commit c48ae39

Please sign in to comment.