Skip to content

Commit 37180b2

Browse files
authored
Merge pull request #8418 from tannewt/fix_mdns_crash_after_user_disconnect
Fix MDNS after the user turns off wifi
2 parents 40a5313 + 8f3c642 commit 37180b2

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

ports/espressif/common-hal/mdns/Server.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,22 @@
3636
// Track whether the underlying IDF mdns has been started so that we only
3737
// create a single inited MDNS object to CircuitPython. (After deinit, another
3838
// could be created.)
39-
STATIC bool mdns_started = false;
39+
STATIC mdns_server_obj_t *_active_object = NULL;
4040

4141
void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
42-
if (mdns_started) {
42+
if (_active_object != NULL) {
43+
if (self == _active_object) {
44+
return;
45+
}
4346
// Mark this object as deinited because another is already using MDNS.
4447
self->inited = false;
4548
return;
4649
}
47-
mdns_init();
48-
mdns_started = true;
50+
esp_err_t ret = mdns_init();
51+
if (ret != ESP_OK) {
52+
return;
53+
}
54+
_active_object = self;
4955

5056
uint8_t mac[6];
5157
esp_netif_get_mac(common_hal_wifi_radio_obj.netif, mac);
@@ -86,10 +92,16 @@ void common_hal_mdns_server_deinit(mdns_server_obj_t *self) {
8692
return;
8793
}
8894
self->inited = false;
89-
mdns_started = false;
95+
_active_object = NULL;
9096
mdns_free();
9197
}
9298

99+
void mdns_server_deinit_singleton(void) {
100+
if (_active_object != NULL) {
101+
common_hal_mdns_server_deinit(_active_object);
102+
}
103+
}
104+
93105
bool common_hal_mdns_server_deinited(mdns_server_obj_t *self) {
94106
return !self->inited;
95107
}

ports/espressif/common-hal/mdns/Server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ typedef struct {
3636
// Track if this object owns access to the underlying MDNS service.
3737
bool inited;
3838
} mdns_server_obj_t;
39+
40+
void mdns_server_deinit_singleton(void);

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "components/lwip/include/apps/ping/ping_sock.h"
4545

4646
#if CIRCUITPY_MDNS
47-
#include "components/mdns/include/mdns.h"
47+
#include "common-hal/mdns/Server.h"
4848
#endif
4949

5050
#define MAC_ADDRESS_LENGTH 6
@@ -97,7 +97,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
9797
common_hal_wifi_radio_stop_scanning_networks(self);
9898
}
9999
#if CIRCUITPY_MDNS
100-
mdns_free();
100+
mdns_server_deinit_singleton();
101101
#endif
102102
ESP_ERROR_CHECK(esp_wifi_stop());
103103
self->started = false;

0 commit comments

Comments
 (0)