RT-Thread / RT-Thread/rt-thread

workqueue.c 中 rt_workqueue_submit_work bug

Open
#8,477 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

RT-Thread 版本:4.1.x,5.1.0 ,其他版本没验证 应该也存在
问题描述
rt_workqueue_submit_work 延迟任务执行,如果在定时到达之前,提前重新调用并传参tick=0,会导致定时到达之后出现bug。

问题复现代码,bsp:qemu-vexpress-a9
main.c 如下

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <rtthread.h>

struct rt_work test_work;

void work_func(struct rt_work *work, void *work_data)
{
    printf("work_func %lu\n", rt_tick_get_millisecond());
}

int main(void)
{
    rt_thread_mdelay(200);
    printf("Hello RT-Thread!\n");

    rt_work_init(&test_work, work_func, NULL);

    printf("main %lu\n", rt_tick_get_millisecond());
    rt_work_submit(&test_work, rt_tick_from_millisecond(1000));
    rt_thread_mdelay(200);
    rt_work_submit(&test_work, 0);

    return 0;
}

msh输出如下:

 \ | /
- RT -     Thread Operating System
 / | \     5.1.0 build Jan  9 2024 10:46:20
 2006 - 2022 Copyright by RT-Thread team   
[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to high speed failed!
[I/FileSystem] file system initialization done!

msh />Hello RT-Thread!
main 1220
work_func 1420
(queue != RT_NULL) assertion failed at function:_delayed_work_timeout_handler, line number:176 
backtrace:
please use: addr2line -e rtthread.elf -a -f 6003fe10 6004c848 60057fb4 60053454 6005361c       

问题分析:

_workqueue_submit_work 函数当 ticks传参等于0 的时候,没有停止上次的定时器。导致任务触发后,定时还在继续。当定时器触发之后任务已经执行完成。

解决办法:
修改 _workqueue_submit_work 函数,

    if (ticks == 0)
    {
        rt_list_insert_after(queue->work_list.prev, &(work->list));
        work->flags |= RT_WORK_STATE_PENDING;
        work->workqueue = queue;

        if (work->flags & RT_WORK_STATE_SUBMITTING)
        {
            rt_timer_stop(&(work->timer));
            rt_timer_detach(&(work->timer));
            work->flags &= ~RT_WORK_STATE_SUBMITTING;
        }
.
.
.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in workqueue.c at _workqueue_submit_work and reproduce the issue on the qemu-vexpress-a9 BSP using the provided main.c sequence. Verify that resubmitting delayed work with tick=0 does not leave the old timer active and that the later timeout produces no assertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.