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

[Bug] pthread_cancel线程无法退出

Open wycwyhwyq opened this issue 10 months ago • 6 comments

RT-Thread Version

c9c103f30e28b31e5d393e7a8cc1a07779fb99dc

Hardware Type/Architectures

risc-v c908

Develop Toolchain

GCC

Describe the bug

调用pthread_cancel线程无法退出,附上测试代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

pthread_t tid;

static void *subthread(void *arg)
{
    while (1) {
        usleep(1000000);
        printf("%s: %d\n", __func__, __LINE__);
    }

    return 0;
}

int main(int argc, char **argv)
{
    pthread_create(&tid, NULL, subthread, 0);

    while (getchar() != 'q')
        usleep(10000);

    printf("%s: %d\n", __func__, __LINE__);
    pthread_cancel(tid);
    printf("%s: %d\n", __func__, __LINE__);
    pthread_join(tid, NULL);
    printf("%s: %d\n", __func__, __LINE__);

    return 0;
}

Other additional context

No response

wycwyhwyq avatar Jan 07 '25 07:01 wycwyhwyq

什么板子上发现的?

unicornx avatar Jan 07 '25 08:01 unicornx

什么板子上发现的?

CanMV-K230

wycwyhwyq avatar Jan 09 '25 06:01 wycwyhwyq

什么板子上发现的?

CanMV-K230

你用的什么 bsp 在测试?是 bsp/k230 吗?我怎么听说这个 bsp 在 canMV 上跑不起来的?

unicornx avatar Jan 09 '25 10:01 unicornx

什么板子上发现的?

CanMV-K230

你用的什么 bsp 在测试?是 bsp/k230 吗?我怎么听说这个 bsp 在 canMV 上跑不起来的?

bsp/k230系列的板子都可以复现,可能和板子没有关系,我定位到问题大概是在musl lib的cancel信号处理部分 loH94YH51d 这里会导致被cancel的线程一直接收到信号

wycwyhwyq avatar Jan 10 '25 03:01 wycwyhwyq

通过qemu调试用户态发现pthread_kill这个函数一直被调用,看c库代码里是没有循环调用的

heyuanjie87 avatar Jan 10 '25 11:01 heyuanjie87

编译c库的时候关闭优化(-O0)pthread_cancel能正常退出,但pthread_join无法退出(被挂起在了pthread->detach_state的futex上)

heyuanjie87 avatar Jan 10 '25 13:01 heyuanjie87

编译c库的时候关闭优化(-O0)pthread_cancel能正常退出,但pthread_join无法退出(被挂起在了pthread->detach_state的futex上)

请问最新userapp make自动下载下来的工具链对应的源码在哪里下载啊,我最近在尝试解决这个问题,但是xmake 自动下载的工具链里的libc.a没有调试信息😂

eatvector avatar Jul 09 '25 15:07 eatvector

这个bug我定位是rt-smart musl 工具链不支持取消点检测加上lwp 信号处理相关存在问题合起来导致的,lwp信号处理的问题已经解决但工具链可能没法这么快更新,只能自己手动插入取消点检测。

eatvector avatar Jul 24 '25 02:07 eatvector

出现两个问题: 1.新给的工具链,编译出的user_test,一跳转到地址0就崩溃了,user_test运行地址从0地址开始; 2.把上方测试例运行在内核态,运行到pthread_cancel,会发生Exception 15:Store/AMO Page Fault;

yixinghua121 avatar Aug 04 '25 07:08 yixinghua121

测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

pthread_t tid;

static void *subthread(void *arg)
{
    while (1) {
        printf("%s: %d\n", __func__, __LINE__);
        usleep(1000);
    }

    return 0;
}

int main(int argc, char **argv)
{
    pthread_create(&tid, NULL, subthread, 0);

    usleep(100);

    printf("%s: %d\n", __func__, __LINE__);
    pthread_cancel(tid);
    printf("%s: %d\n", __func__, __LINE__);
    pthread_join(tid, NULL);
    printf("%s: %d\n", __func__, __LINE__);

    return 0;
}

k230 01stdio板卡运行结果

Image

rcitach avatar Aug 05 '25 07:08 rcitach

测试流程

一、SMART Kernel 编译

使用最新版本工具链,根据README.md 编译bsp/k230

注:需要在menuconfig调整 RT_NAME_MAX 的大小,并在 FPU precision选择 Software floating-point Image

Image

二、生成img镜像

同样根据 README.md 生成 K230 RTOS Only SDK的基础镜像 并更新 RT-Thread 内核

三、添加用户态测试程序

在过程二中,会将镜像烧录到sd卡中,再将sd卡插入到电脑中,可以发现会多出 BIN SDCARD两个盘符

Image

使用工具链编译上述测试代码(以pthread.c命令):

/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-linux-musleabi-gcc pthread.c  --static -o pthread_cancel

将编译出的pthread_cancel复制到 SDCARD盘符中

四、运行用户态测试程序

将SD卡插入板卡中,上电复位,在控制台运行 pthread_cancel

Image

rcitach avatar Aug 05 '25 08:08 rcitach