Skip to content

Commit b47c51f

Browse files
committed
fix(mdns): Fix API races when removing all services
1 parent cb8d181 commit b47c51f

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

components/mdns/mdns.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,8 +5148,6 @@ static void _mdns_free_action(mdns_action_t *action)
51485148
*/
51495149
static void _mdns_execute_action(mdns_action_t *action)
51505150
{
5151-
mdns_srv_item_t *a = NULL;
5152-
51535151
switch (action->type) {
51545152
case ACTION_SYSTEM_EVENT:
51555153
perform_event_action(action->data.sys_event.interface, action->data.sys_event.event_action);
@@ -5169,19 +5167,6 @@ static void _mdns_execute_action(mdns_action_t *action)
51695167
_mdns_server->instance = action->data.instance;
51705168
_mdns_restart_all_pcbs_no_instance();
51715169

5172-
break;
5173-
case ACTION_SERVICES_CLEAR:
5174-
_mdns_send_final_bye(false);
5175-
a = _mdns_server->services;
5176-
_mdns_server->services = NULL;
5177-
while (a) {
5178-
mdns_srv_item_t *s = a;
5179-
a = a->next;
5180-
_mdns_remove_scheduled_service_packets(s->service);
5181-
_mdns_free_service(s->service);
5182-
free(s);
5183-
}
5184-
51855170
break;
51865171
case ACTION_SEARCH_ADD:
51875172
_mdns_search_add(action->data.search_add.search);
@@ -5226,6 +5211,7 @@ static void _mdns_execute_action(mdns_action_t *action)
52265211
free((char *)action->data.delegate_hostname.hostname);
52275212
free_address_list(action->data.delegate_hostname.address_list);
52285213
}
5214+
xSemaphoreGive(_mdns_server->action_sema);
52295215
break;
52305216
case ACTION_DELEGATE_HOSTNAME_SET_ADDR:
52315217
if (!_mdns_delegate_hostname_set_address(action->data.delegate_hostname.hostname,
@@ -5789,6 +5775,7 @@ esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t
57895775
free(action);
57905776
return ESP_ERR_NO_MEM;
57915777
}
5778+
xSemaphoreTake(_mdns_server->action_sema, portMAX_DELAY);
57925779
return ESP_OK;
57935780
}
57945781

@@ -6465,27 +6452,27 @@ esp_err_t mdns_service_remove(const char *service_type, const char *proto)
64656452

64666453
esp_err_t mdns_service_remove_all(void)
64676454
{
6468-
if (!_mdns_server) {
6469-
return ESP_ERR_INVALID_ARG;
6470-
}
64716455
MDNS_SERVICE_LOCK();
6456+
esp_err_t ret = ESP_OK;
6457+
ESP_GOTO_ON_FALSE(_mdns_server, ESP_ERR_INVALID_ARG, done, TAG, "Invalid state");
64726458
if (!_mdns_server->services) {
6473-
MDNS_SERVICE_UNLOCK();
6474-
return ESP_OK;
6459+
goto done;
64756460
}
6476-
MDNS_SERVICE_UNLOCK();
64776461

6478-
mdns_action_t *action = (mdns_action_t *)malloc(sizeof(mdns_action_t));
6479-
if (!action) {
6480-
HOOK_MALLOC_FAILED;
6481-
return ESP_ERR_NO_MEM;
6482-
}
6483-
action->type = ACTION_SERVICES_CLEAR;
6484-
if (xQueueSend(_mdns_server->action_queue, &action, (TickType_t)0) != pdPASS) {
6485-
free(action);
6486-
return ESP_ERR_NO_MEM;
6462+
_mdns_send_final_bye(false);
6463+
mdns_srv_item_t *services = _mdns_server->services;
6464+
_mdns_server->services = NULL;
6465+
while (services) {
6466+
mdns_srv_item_t *s = services;
6467+
services = services->next;
6468+
_mdns_remove_scheduled_service_packets(s->service);
6469+
_mdns_free_service(s->service);
6470+
free(s);
64876471
}
6488-
return ESP_OK;
6472+
6473+
done:
6474+
MDNS_SERVICE_UNLOCK();
6475+
return ret;
64896476
}
64906477

64916478
/*

components/mdns/private_include/mdns_private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ typedef enum {
187187
ACTION_SYSTEM_EVENT,
188188
ACTION_HOSTNAME_SET,
189189
ACTION_INSTANCE_SET,
190-
ACTION_SERVICES_CLEAR,
191190
ACTION_SEARCH_ADD,
192191
ACTION_SEARCH_SEND,
193192
ACTION_SEARCH_END,

0 commit comments

Comments
 (0)