OpenThread deinit crash

Marttico
Posts: 1
Joined: Fri May 24, 2024 12:00 pm

OpenThread deinit crash

Postby Marttico » Fri May 24, 2024 12:16 pm

I'm currently trying to deinitialize OpenThread. I've tried to implement this myself, however this keeps ending up in a Guru Meditation Error: Core 0 panic'ed (Stack protection fault). To implement this I've read up on the docs and saw that I need to:
• Call esp_netif_destroy() and esp_openthread_netif_glue_deinit() to deinitialize the OpenThread network interface if you have created one.
• Call esp_openthread_deinit() to deinitialize the OpenThread stack.

My stopThread() function currently looks like this:
  1. void stopThread(){
  2.  
  3.     ESP_LOGI(TAG,"Stopping Thread");
  4.     esp_openthread_netif_glue_deinit();
  5.     esp_netif_destroy(openthread_netif);
  6.    
  7.     esp_openthread_deinit();
  8.  
  9.     ESP_LOGI(TAG,"Thread Service Stopped");
  10. }
I trigger this code 5 seconds after initializing Thread using the following code:
  1. vTaskDelay(5000 / portTICK_PERIOD_MS);
  2. stopThread();
Does anyone know what I'm doing wrong here and how I could fix it? Thanks in advance!

Here's my entire code:

Code: Select all

/*
 * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 *
 * OpenThread Command Line Example
 *
 * This example code is in the Public Domain (or CC0 licensed, at your option.)
 *
 * Unless required by applicable law or agreed to in writing, this
 * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_types.h"
#include "esp_openthread.h"
#include "esp_openthread_cli.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_netif_glue.h"
#include "esp_openthread_types.h"
#include "esp_ot_config.h"
#include "esp_vfs_eventfd.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "esp_timer.h"
#include "freertos/task.h"
#include "hal/uart_types.h"
#include "nvs_flash.h"
#include "openthread/cli.h"
#include "openthread/instance.h"
#include "openthread/logging.h"
#include "openthread/tasklet.h"

#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
#include "esp_ot_cli_extension.h"
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION

#define TAG "ot_esp_cli"

TaskHandle_t openThreadHandle;
esp_timer_handle_t connection_timeout_timer;
esp_netif_t *openthread_netif;

static esp_netif_t *init_openthread_netif(const esp_openthread_platform_config_t *config)
{
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
    esp_netif_t *netif = esp_netif_new(&cfg);
    assert(netif != NULL);
    ESP_ERROR_CHECK(esp_netif_attach(netif, esp_openthread_netif_glue_init(config)));

    return netif;
}

static void ot_task_worker(void *aContext)
{
    esp_openthread_platform_config_t config = {
        .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
        .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
        .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
    };

    // Initialize the OpenThread stack
    ESP_ERROR_CHECK(esp_openthread_init(&config));

#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
    // The OpenThread log level directly matches ESP log level
    (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
#endif
    // Initialize the OpenThread cli
#if CONFIG_OPENTHREAD_CLI
    esp_openthread_cli_init();
#endif

    
    // Initialize the esp_netif bindings
    openthread_netif = init_openthread_netif(&config);
    esp_netif_set_default_netif(openthread_netif);

#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
    esp_cli_custom_command_init();
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION

    // Run the main loop
#if CONFIG_OPENTHREAD_CLI
    esp_openthread_cli_create_task();
#endif
#if CONFIG_OPENTHREAD_AUTO_START
    otOperationalDatasetTlvs dataset;
    otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
    ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
#endif
    esp_openthread_launch_mainloop();

    // Clean up
    esp_openthread_netif_glue_deinit();
    esp_netif_destroy(openthread_netif);

    esp_vfs_eventfd_unregister();
    vTaskDelete(NULL);
}


void stopThread(){

    ESP_LOGI(TAG,"Stopping Thread");
    // Clean up
    esp_openthread_netif_glue_deinit();
    esp_netif_destroy(openthread_netif);
    
    esp_openthread_deinit();

    ESP_LOGI(TAG,"Thread Service Stopped");
}


void app_main(void)
{
    // Used eventfds:
    // * netif
    // * ot task queue
    // * radio driver
    esp_vfs_eventfd_config_t eventfd_config = {
        .max_fds = 3,
    };

    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));


    xTaskCreate(ot_task_worker, "ot_cli_main", 10240, xTaskGetCurrentTaskHandle(), 5, openThreadHandle);

    vTaskDelay(5000 / portTICK_PERIOD_MS);

    stopThread();

}
Here's the log it produces:

Code: Select all

ESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0xc (SW_CPU),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x4001975a
SPIWP:0xee
mode:DIO, clock div:2
load:0x40875720,len:0x1804
load:0x4086c410,len:0xe58
load:0x4086e610,len:0x2e24
entry 0x4086c41a
I (26) boot: ESP-IDF v5.2.1-dirty 2nd stage bootloader
I (27) boot: compile time May 24 2024 13:50:07
I (28) boot: chip revision: v0.0
I (30) boot.esp32c6: SPI Speed      : 80MHz
I (35) boot.esp32c6: SPI Mode       : DIO
I (40) boot.esp32c6: SPI Flash Size : 2MB
I (44) boot: Enabling RNG early entropy source...
I (50) boot: Partition Table:
I (53) boot: ## Label            Usage          Type ST Offset   Length
I (61) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (68) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (76) boot:  2 factory          factory app      00 00 00010000 00120000
I (83) boot: End of partition table
I (87) esp_image: segment 0: paddr=00010020 vaddr=420b8020 size=3c560h (247136) map
I (197) esp_image: segment 1: paddr=0004c588 vaddr=40800000 size=03a90h ( 14992) load
I (205) esp_image: segment 2: paddr=00050020 vaddr=42000020 size=b101ch (725020) map
I (501) esp_image: segment 3: paddr=00101044 vaddr=40803a90 size=0c70ch ( 50956) load
I (526) esp_image: segment 4: paddr=0010d758 vaddr=408101a0 size=021dch (  8668) load
I (539) boot: Loaded app from partition at offset 0x10000
I (540) boot: Disabling RNG early entropy source...
I (551) cpu_start: Unicore app
W (561) clk: esp_perip_clk_init() has not been implemented yet
I (568) cpu_start: Pro cpu start user code
I (568) cpu_start: cpu freq: 160000000 Hz
I (568) cpu_start: Application information:
I (571) cpu_start: Project name:     esp_ot_cli
I (576) cpu_start: App version:      b978f4a-dirty
I (582) cpu_start: Compile time:     May 24 2024 13:49:50
I (588) cpu_start: ELF file SHA256:  44dcfdd21...
I (593) cpu_start: ESP-IDF:          v5.2.1-dirty
I (598) cpu_start: Min chip rev:     v0.0
I (603) cpu_start: Max chip rev:     v0.99 
I (608) cpu_start: Chip rev:         v0.0
I (613) heap_init: Initializing. RAM available for dynamic allocation:
I (620) heap_init: At 4081D090 len 0005F580 (381 KiB): RAM
I (626) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM
I (632) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM
I (639) spi_flash: detected chip: generic
I (643) spi_flash: flash io: dio
W (647) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (660) sleep: Configure to isolate all GPIO pins in sleep state
I (667) sleep: Enable automatic switching of GPIO sleep configuration
I (674) coexist: coex firmware version: 77cd7f8
I (679) coexist: coexist rom version 5b8dcfa
I (685) main_task: Started on CPU0
I (685) main_tQ�I (695) phy_init: phy_version 250,e14681b,Jan 24 2024,17:43:11
I (735) phy: libbtbb version: 939f79c, Jan 24 2024, 17:43:26
0x4001975a: software_reset_cpu in ROM

I(745) OPENTHREAD:[I] ChildSupervsn-: Timeout: 0 -> 190
I(745) OPENTHREAD:[I] Settings------: Read NetworkInfo {rloc:0xdc11, extaddr:aef1b8878651de48, role:child, mode:0x0d, version:4, keyseq:0x0, ...
I(755) OPENTHREAD:[I] Settings------: ... pid:0x20295af2, mlecntr:0x20c33, maccntr:0x20788, mliid:d0327fdcf556468b}
I(775) OPENTHREAD:[I] Settings------: Read ParentInfo {extaddr:369ca8648d1a1101, version:4}
> I (775) OPENTHREAD: OpenThread attached to netif
> I (5745) ot_esp_cli: Stopping Thread
Guru Meditation Error: Core  0 panic'ed (Stack protection fault). 

Detected in task "main" at 0x4003be86
0x4003be86: _vfprintf_r in ROM

Stack pointer: 0x4081f280
Stack bounds: 0x4081f30c - 0x40820300


Stack dump detected
Core  0 register dump:
MEPC    : 0x4003bec2  RA      : 0x420b0ff4  SP      : 0x4081f280  GP      : 0x408109a0  
0x4003bec2: _vfprintf_r in ROM
0x420b0ff4: esp_log_writev at /home/<redacted>/esp/5.2.1/esp-idf/components/log/log.c:215

TP      : 0x407e48b8  T0      : 0x400283c2  T1      : 0x10000000  T2      : 0xffffffff  
0x400283c2: __getreent in ROM

S0/FP   : 0x00000001  S1      : 0x420bf4b4  A0      : 0x40820368  A1      : 0x4081dbd8  
A2      : 0x420bf648  A3      : 0x4081f6fc  A4      : 0x00000000  A5      : 0x00000001  
A6      : 0x00000002  A7      : 0x0000000a  S2      : 0x420bf648  S3      : 0x4081dbd8  
S4      : 0x40820368  S5      : 0x4081f8f0  S6      : 0x420bf660  S7      : 0x00000000  
S8      : 0x00000019  S9      : 0x00000000  S10     : 0x4081f904  S11     : 0x00000004  
T3      : 0x00000000  T4      : 0x6e65706f  T5      : 0x00000000  T6      : 0x00000000  
MSTATUS : 0x00001881  MTVEC   : 0x40800001  MCAUSE  : 0x0000001b  MTVAL   : 0x08d030ef  
0x40800001: _vector_table at ??:?

MHARTID : 0x00000000  

Failed to run gdb_panic_server.py script: Command '['riscv32-esp-elf-gdb', '--batch', '-n', '/home/<redacted>/<redacted>/examples/ot_cli/build/esp_ot_cli.elf', '-ex', 'target remote | "/home/<redacted>/esp/5.2.1/bin/python_env/idf5.2_py3.8_env/bin/python" -m esp_idf_panic_decoder --target esp32c6 "/tmp/tmp_lr6p69j"', '-ex', 'bt']' returned non-zero exit status 1.
b'Traceback (most recent call last):\n  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main\n    return _run_code(code, main_globals, None,\n  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code\n    exec(code, run_globals)\n  File "/home/<redacted>/esp/5.2.1/bin/python_env/idf5.2_py3.8_env/lib/python3.8/site-packages/esp_idf_panic_decoder/__main__.py", line 4, in <module>\n    main()\n  File "/home/<redacted>/esp/5.2.1/bin/python_env/idf5.2_py3.8_env/lib/python3.8/site-packages/esp_idf_panic_decoder/gdb_panic_server.py", line 281, in main\n    panic_info = PANIC_OUTPUT_PARSERS[args.target](args.input_file.read())\n  File "/home/<redacted>/esp/5.2.1/bin/python_env/idf5.2_py3.8_env/lib/python3.8/site-packages/esp_idf_panic_decoder/gdb_panic_server.py", line 148, in parse_idf_riscv_panic_output\n    stack_base_addr, stack_data = get_stack_addr_and_data(res)\n  File "/home/<redacted>/esp/5.2.1/bin/python_env/idf5.2_py3.8_env/lib/python3.8/site-packages/esp_idf_panic_decoder/gdb_panic_server.py", line 116, in get_stack_addr_and_data\n    assert base_addr == prev_base_addr + bytes_in_line\nAssertionError\nRemote communication error.  Target disconnected.: Connection reset by peer.\nNo stack.\n'


Stack memory:
4081f280: 0x00000000 0x24aa2a2b 0x4081f230 0x00000020 0x00000000 0x00000000 0x00000000 0x00000000
4081f2a0: 0x42095192 0x00000000 0x00000000 0x00000000 0x00000034 0x40000b10 0x40000b20 0x40000b18
4081f2c0: 0x40000b14 0x40000b08 0x40000b0c 0x40000b1c 0x40000b30 0x40000b2c 0x40000b28 0x40000b38
4081f2e0: 0x40000b3c 0x42094d26 0x0000000c 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0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4081f5e0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4081f600: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4081f620: 0xa5a5a5a5 0x00000000 0x4081df44 0x40808ba8 0x00000000 0x00000000 0x4081df44 0x408091d8
4081f640: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4081f660: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x4081f6fc 0x420bf648 0x420bf4b4 0x420bf4b4 0x00000004



ELF file SHA256: 44dcfdd21

Rebooting...

Who is online

Users browsing this forum: No registered users and 68 guests