fix(stability): disable dangerous mbedtls free configs, place panic handler in IRAM, and make prefetch safe and on-demand
All checks were successful
ESP32 Build & Release / build (push) Successful in 1m11s

This commit is contained in:
2026-09-12 18:10:32 +02:00
parent d1a4c18967
commit 27482fef01
4 changed files with 42 additions and 15 deletions

View File

@@ -87,6 +87,7 @@ typedef struct {
size_t cap;
esp_err_t err;
FILE *file; /* when downloading to a file */
const char *file_path;
int status;
} http_sink_t;
@@ -153,11 +154,22 @@ static esp_err_t http_request(const char *url, http_sink_t *sink)
return ESP_ERR_TIMEOUT;
}
/* Disable power save during HTTPS requests for maximum reliability */
wifi_manager_set_power_save(false);
esp_err_t overall_ret = ESP_FAIL;
for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
if (sink->file != NULL) {
rewind(sink->file);
ftruncate(fileno(sink->file), 0);
if (attempt > 0 && sink->file_path != NULL) {
fclose(sink->file);
sink->file = fopen(sink->file_path, "wb");
if (sink->file == NULL) {
overall_ret = ESP_FAIL;
break;
}
} else {
rewind(sink->file);
}
} else {
sink->len = 0;
}
@@ -171,6 +183,7 @@ static esp_err_t http_request(const char *url, http_sink_t *sink)
.user_data = sink,
.buffer_size = 2048,
.crt_bundle_attach = esp_crt_bundle_attach,
.keep_alive_enable = false,
};
esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (client == NULL) {
@@ -201,6 +214,7 @@ static esp_err_t http_request(const char *url, http_sink_t *sink)
vTaskDelay(pdMS_TO_TICKS(500));
}
wifi_manager_set_power_save(true);
wifi_manager_release_net_lock();
return overall_ret;
}
@@ -230,7 +244,7 @@ static esp_err_t http_download_file(const char *url, const char *path)
if (f == NULL) {
return ESP_FAIL;
}
http_sink_t sink = { .file = f };
http_sink_t sink = { .file = f, .file_path = tmp };
esp_err_t ret = http_request(url, &sink);
fclose(f);
if (ret != ESP_OK) {
@@ -441,8 +455,17 @@ static esp_err_t scryfall_ensure_cached(const char *card_name, const char *uid)
/* net_task */
/* ------------------------------------------------------------------ */
#ifdef CONFIG_MTG_SCAN_PREFETCH
static void prefetch_mappings(void)
{
/* Settle delay: give system and Wi-Fi time to settle before background fetch */
vTaskDelay(pdMS_TO_TICKS(5000));
if (esp_get_free_heap_size() < 40000) {
ESP_LOGW(TAG, "Heap too low (%u bytes) for startup prefetch, skipping", (unsigned int)esp_get_free_heap_size());
return;
}
char *buf = NULL;
size_t len = 0;
if (storage_read_text(STORAGE_MAPPINGS, &buf, &len) != ESP_OK || buf == NULL) {
@@ -463,12 +486,14 @@ static void prefetch_mappings(void)
cJSON_ArrayForEach(item, root) {
uid = item->string;
if (uid == NULL || item->valuestring == NULL) continue;
scryfall_ensure_cached(item->valuestring, uid);
/* Call scryfall_do_fetch directly so we do not clobber UI fetch signaling */
scryfall_do_fetch(item->valuestring, uid);
/* Rate-limit delay: Scryfall requests 50-100ms between queries */
vTaskDelay(pdMS_TO_TICKS(100));
vTaskDelay(pdMS_TO_TICKS(150));
}
cJSON_Delete(root);
}
#endif
static void net_task(void *arg)
{