Linux多线程调用ubus导致死锁问题

2024-04-24 19:08

本文主要是介绍Linux多线程调用ubus导致死锁问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

测试组发现用户进程在某种特定情况下,会出现死锁,现象是进程还在S状态,但没有任何反应,所以怀疑死锁。

问题复现

通过几次测试发现,进程中设置的参数恢复出厂后重启进程很大概率会出现死锁,这时候已经把复现的方法明确,但是从复现的场景来看暂时无法定位出原因。接下来就编译问题版本进行问题跟踪。

调试方法

追查进程死锁方法我知道的有这么几种:另开线程心跳监控、另开进程心跳监控,打印调试,gdb调试,git回溯版本范围缩小;

由于进程中开了2个业务线程,所以使用另开线程心跳监控方法有弊端,死锁后调度也会卡住心跳线程导致不能准确定位;综合来看使用gdb进行调试;

GDB调式

下载gdb源码并进行交叉编译,然后拷贝到盒子进行调试;编译进程时加上-g调试信息:

./arm-linux-gnueabihf-gdb ifotond 开始复现死锁,死锁后打印如下

root@www:/mnt/emmc/lock# ./arm-linux-gnueabihf-gdb ifotond-25-g 
GNU gdb (GDB) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ifotond-25-g...done.
(gdb) r
Starting program: /mnt/emmc/lock/ifotond-25-g 
warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
[Detaching after fork from child process 28336]
not set the app nameSet sleep time is 28800 
[New LWP 28341]
[New LWP 28342]
[New LWP 28343]
[WARNING]:not set the ubus name
regis id = 8
regis id = 3
regis id = 1
regis id = 2
[LWP 28343 exited]
227 ota_event_set      etype:5, e_status:1, LR:00040600
[Detaching after fork from child process 28346]
227 ota_event_set      etype:6, e_status:1, LR:000283fc
[Detaching after fork from child process 28348]
send data!type is 1!data is 07 01 ac 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 be 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 30 d4 59 56 71 e8 9a 6a b2 fc db 7e f3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 04 07 60 2a 22 00 00 00 00 00 00 00 00 00 00 00 00 08 07 60 2a 22 d9 db 2b 89 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
recv data!type is 0!data is 01 00 
[Detaching after fork from child process 28357]
[ERROR]:register timeout = 30 name = ifoton
Successfully captured all of multi-frame. Freeing memory.卡住了^C 断下来
Thread 1 "ifotond-25-g" received signal SIGINT, Interrupt.
0xb6cc41b0 in poll () from /lib/arm-linux-gnueabihf/libc.so.6
(gdb) 
(gdb) 
(gdb) info threadId   Target Id                Frame 
* 1    LWP 28322 "ifotond-25-g" 0xb6cc41b0 in poll ()	主进程==线程1from /lib/arm-linux-gnueabihf/libc.so.62    LWP 28341 "ifotond-25-g" 0xb6caa0c0 in nanosleep ()		线程2from /lib/arm-linux-gnueabihf/libc.so.63    LWP 28342 "ifotond-25-g" 0xb6cc41b0 in poll ()	线程3from /lib/arm-linux-gnueabihf/libc.so.6
(gdb) thread 1
[Switching to thread 1 (LWP 28322)]
#0  0xb6cc41b0 in poll () from /lib/arm-linux-gnueabihf/libc.so.6
(gdb) bt
#0  0xb6cc41b0 in poll () from /lib/arm-linux-gnueabihf/libc.so.6
#1  0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

从上面打印看,死锁后主线程和线程3卡在同一个poll函数,由于进程中使用的socket通信使用的是select,所以没有直接调用poll函数;poll函数也是用于网络通信的,进程中频繁使用的ubus内部的机制就是使用的网络通信;

查询进程代码没有直接调用poll函数,poll函数在libc库实现,又由于bt没有打印出回溯信息,所以怀疑poll函数是在动态库里面调用的;

搜索动态库,可知在/lib/libubus.so:1742:poll调用,查看ubus源码,确实有调用poll函数;

ubus-1d2b3bb/libubus-io.c
static void wait_data(int fd, bool write)
{struct pollfd pfd = { .fd = fd };pfd.events = write ? POLLOUT : POLLIN;poll(&pfd, 1, -1);
}void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
{struct pollfd pfd = {.fd = ctx->sock.fd,.events = POLLIN | POLLERR,};poll(&pfd, 1, timeout ? timeout : -1);ubus_handle_data(&ctx->sock, ULOOP_READ);
}

分析原因

到这里就知道是ubus导致的死锁,我们知道,ubus不支持多线程调用,否则容易出现死锁;进程代码中调用ubus是主线程负责,出现死锁的原因可能就是其他线程调用了ubus,这点从gdb打印也可看出;

在ubus提供的接口ubus_call、ubus_reply、ubus_send中添加参数和in/out打印,待死锁后查看参数就可知道在代码中调用的位置;

最后查出是ubus_send在复位情况后会被线程3调用,导致了主线程调用ubus_call卡住死锁,ubus_call可以明确是正常调用,通过在ubus_send中造一个空指针把pg调用顺序打出就知道了调用者,最后查出了问题原因:没有注意到复位流程会走线程3调用;

问题解决:把这个ubus_send调用加入到主线程队列等待被调用就可以了,可能会有不实时的风险;

扩展

打印调试,在怀疑死锁的模块里面加上这段代码,db_msg换成printf。

	#if 1#define pthread_mutex_lock(lock)  do { \db_msg("lock: in %d, %s", __LINE__, __FUNCTION__); \pthread_mutex_lock(lock); \db_msg("locked: in %d, %s", __LINE__, __FUNCTION__); \} while(0)#define pthread_mutex_unlock(lock)  do { \db_msg("unlock: in %d, %s", __LINE__, __FUNCTION__); \pthread_mutex_unlock(lock); \db_msg("unlocked: in %d, %s", __LINE__, __FUNCTION__); \} while(0)#endif

提示warning: Unable to find libthread_db,应该是libthread.so strap过了或者需要调用libthread_db库来支持,需要验证一下,又有说法是需要额外的libc库和libthread库(size很大)在支持调试,否则info thread信息不准确。

 

这篇关于Linux多线程调用ubus导致死锁问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/932593

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序