修复: unwrap 吞错 + lock 中毒 panic 高危项(数据吞 warn 跳过 / lock 降级返 Err)

This commit is contained in:
lxy
2026-08-01 12:21:52 +08:00
parent 143b859727
commit 484080ac12
6 changed files with 184 additions and 27 deletions
+43 -9
View File
@@ -291,12 +291,24 @@ type SkillsGuard = RwLockReadGuard<'static, Option<Vec<SkillInfo>>>;
/// P1-260617-3:`scan_skills` 同步递归 `fs::read_dir` + `read_to_string`(plugins/marketplaces
/// 多层嵌套,Windows 文件多时同步阻塞 tokio runtime)。本函数改 async,慢路径扫盘包
/// `spawn_blocking` 隔离(对齐 commands/project.rs detect_stack 模式)。快路径(读锁命中)仍同步无 fs。
async fn skills_lock_async() -> SkillsGuard {
///
/// 锁中毒(P1-260617-3 加固):读写锁 expect 中毒会 panic,技能加载热路径 panic 不可接受。
/// 中毒 → tracing::error! 记录 + 返 None(调用方 `skills_cached` 得空 Vec / `read_skill_content_stripped`
/// 返 None),不再 panic。中毒通常因持锁 panicking 线程(早期改 *g 时 unwrap)残留,缓存本身可重建,
/// 返空后下次 `invalidate_skills` 或进程重启自愈。
async fn skills_lock_async() -> Option<SkillsGuard> {
// 快路径:读锁命中(无 fs,纯内存)
{
let g = RwLock::read(&SKILLS).expect("SKILLS poisoned");
// 锁中毒不 panic:PoisonError 携 guard 仍可恢复数据,但缓存一致性不保 → 记录后返 None 降级空。
let g = match RwLock::read(&SKILLS) {
Ok(g) => g,
Err(_) => {
tracing::error!("SKILLS 读锁中毒,返空技能列表");
return None;
}
};
if g.is_some() {
return g;
return Some(g);
}
}
// 慢路径:扫盘(spawn_blocking 隔离同步 fs 递归,防阻塞 tokio runtime)
@@ -305,16 +317,28 @@ async fn skills_lock_async() -> SkillsGuard {
.map(|res| res.skills)
.unwrap_or_default();
{
let mut g = RwLock::write(&SKILLS).expect("SKILLS poisoned");
let mut g = match RwLock::write(&SKILLS) {
Ok(g) => g,
Err(_) => {
tracing::error!("SKILLS 写锁中毒,返空技能列表");
return None;
}
};
// 另一线程可能已填,二次检查(双检锁)
if g.is_none() {
*g = Some(scanned);
}
}
// 再取读锁返回(此时必 Some)
let g = RwLock::read(&SKILLS).expect("SKILLS poisoned");
let g = match RwLock::read(&SKILLS) {
Ok(g) => g,
Err(_) => {
tracing::error!("SKILLS 读锁(慢路径后)中毒,返空技能列表");
return None;
}
};
debug_assert!(g.is_some(), "skills_lock 慢路径后必 Some");
g
Some(g)
}
/// 技能扫描结果缓存(进程内;命中即 clone,不重复扫盘)。
@@ -326,7 +350,8 @@ async fn skills_lock_async() -> SkillsGuard {
/// 隔离同步 fs 防阻塞 tokio runtime(Tauri 单线程 runtime)。快路径(读锁命中)无 fs。
pub(crate) async fn skills_cached() -> Vec<SkillInfo> {
let g = skills_lock_async().await;
g.clone().unwrap_or_default()
// 锁中毒 → None → unwrap_or_default() 得空 Vec(对齐 P1-260617-3 中毒降级)。
g.and_then(|g| g.clone()).unwrap_or_default()
}
/// 置缓存为 None,下次 `skills_cached()` 触发重扫。
@@ -334,7 +359,15 @@ pub(crate) async fn skills_cached() -> Vec<SkillInfo> {
/// `ai_reload_skills` IPC 调用:写锁置 None → 紧接 `skills_cached()` 重扫,
/// 实现"改技能不重启即生效"。
pub(crate) fn invalidate_skills() {
let mut g = RwLock::write(&SKILLS).expect("SKILLS poisoned");
// 锁中毒:本就是想置 None 清缓存重建,但中毒时持锁线程已 panic,此处无法恢复一致状态。
// 记录错误并提前返回(下次加载由 skills_lock_async 中毒降级返空,进程重启自愈)。
let mut g = match RwLock::write(&SKILLS) {
Ok(g) => g,
Err(_) => {
tracing::error!("SKILLS 写锁中毒(invalidate_skills),跳过置 None");
return;
}
};
*g = None;
}
@@ -349,8 +382,9 @@ pub(crate) fn invalidate_skills() {
/// 否则非 Send 跨 await 点致 future 不 Send(MentionResolver 要求 Send)。guard 用 { } 限作用域。
pub(crate) async fn read_skill_content_stripped(name: String) -> Option<String> {
// 在作用域内取 path 后立即 drop guard,避免非 Send guard 跨 spawn_blocking await 点
// 锁中毒 → skills_lock_async 返 None → ? 早返 None(对齐 P1-260617-3 中毒降级返空 desc)。
let path: String = {
let g = skills_lock_async().await;
let g = skills_lock_async().await?;
let skills = g.as_ref()?;
let info = skills.iter().find(|s| s.name == name)?;
info.path.clone()