rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

[Bug] netstat

Open 0-errors0-warnings opened this issue 4 weeks ago • 1 comments

RT-Thread Version

5.2.1

Hardware Type/Architectures

STM32H743

Develop Toolchain

GCC

Describe the bug

在 rt-thread/components/net/lwip/port/ethernetif.c 中 list_if, list_tcps, list_udps 函数调用 rt_kprintf 之前由于调用了 rt_enter_critical(); 导致调度器上锁,在 rt_kprintf 中调用 console 设备时,使用 rt_mutex_t 导致在调度器上锁时出现死锁。

详细的调用堆栈如下:

void list_if(void) 
{
    ......
    rt_enter_critical();
    ......
    while( netif != RT_NULL )
    {
        rt_kprintf("network interface: %c%c%s\n",
                   netif->name[0],
                   netif->name[1],
                   (netif == netif_default)?" (Default)":"");
        ...
    }
    ....
}

rt_weak int rt_kprintf(const char *fmt, ...)
{
     ...
    _kputs(rt_log_buf, length);
    ...
}

static void _kputs(const char *str, long len)
{
        .....
        rt_device_write(console_device, 0, str, len);
        ....
}

static rt_ssize_t telnet_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
    ......
    rt_mutex_take(telnet->tx_ringbuffer_lock, RT_WAITING_FOREVER);
    ......
}

static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend_flag)
{
    struct rt_thread *thread;
    rt_err_t ret;

    /* this function must not be used in interrupt even if time = 0 */
    /* current context checking */
    RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE);
    ...
}

#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)                              \
do                                                                            \
{                                                                             \
    if (need_check)                                                           \
    {                                                                         \
        if (rt_critical_level() != 0)                                         \
        {                                                                     \
            rt_kprintf("Function[%s]: scheduler is not available\n",          \
                    __FUNCTION__);                                            \
            RT_ASSERT(0)                                                      \
        }                                                                     \
        RT_DEBUG_IN_THREAD_CONTEXT;                                           \
    }                                                                         \
}     

然后将会再次调用 rt_kprintf,一直重复,直到堆栈溢出。

Other additional context

No response

0-errors0-warnings avatar Nov 28 '25 18:11 0-errors0-warnings

这个应该是因为调用了 telnet_write 所以触发了,而如果是串口的console则不会有这个问题。或者对于这类需要有一定的机制避免。

BernardXiong avatar Nov 29 '25 01:11 BernardXiong