Skip to content

Commit 12dae53

Browse files
committed
fix: 修复测试日志目录竞争条件
修复测试时可能出现的日志目录锁竞争条件问题。当 CCV_LOG_DIR 环境变量设为 "tmp" 或 "temp" 时,现在会使用进程特定的临时目录,避免多个测试进程间的冲突。同时改进锁机制,增加陈旧锁检测(超过5秒未更新视为死锁),防止因进程崩溃导致的锁永久持有问题。
1 parent fba5e68 commit 12dae53

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

findcc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ function resolveLogDir() {
1313
const envDir = process.env.CCV_LOG_DIR;
1414
if (typeof envDir === 'string' && envDir.trim()) {
1515
const raw = envDir.trim();
16+
if (raw === 'tmp' || raw === 'temp') {
17+
return join(tmpdir(), 'cc-viewer-test', `${process.pid}-${threadId}`);
18+
}
1619
const expanded = raw.startsWith('~/') ? join(homedir(), raw.slice(2)) : raw;
1720
return resolve(expanded);
1821
}
19-
if (process.argv.includes('--test')) {
20-
return join(tmpdir(), 'cc-viewer-test', `${process.pid}-${threadId}`);
21-
}
2222
return join(homedir(), '.claude', 'cc-viewer');
2323
}
2424

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"dev": "vite",
1919
"build": "node build.js",
2020
"start": "node server.js",
21-
"test": "node --test --test-concurrency=1",
21+
"test": "CCV_LOG_DIR=tmp node --test --test-concurrency=1",
2222
"prepublishOnly": "npm run build"
2323
},
2424
"keywords": [
@@ -94,4 +94,4 @@
9494
"undici": "^7.22.0",
9595
"ws": "^8.19.0"
9696
}
97-
}
97+
}

workspace-registry.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,32 @@ function sleep(ms) {
1414
function withLock(fn) {
1515
mkdirSync(LOG_DIR, { recursive: true });
1616
const deadline = Date.now() + 2000;
17+
// 如果锁文件超过 5 秒未更新,认为它是死锁(前一个进程崩溃)
18+
const STALE_THRESHOLD = 5000;
19+
1720
while (true) {
1821
try {
1922
const fd = openSync(LOCK_FILE, 'wx');
2023
closeSync(fd);
2124
break;
2225
} catch (err) {
23-
if (err?.code === 'EEXIST' && Date.now() < deadline) {
24-
sleep(25);
25-
continue;
26+
if (err?.code === 'EEXIST') {
27+
if (Date.now() < deadline) {
28+
// 检查是否为陈旧锁
29+
try {
30+
const stats = statSync(LOCK_FILE);
31+
if (Date.now() - stats.mtimeMs > STALE_THRESHOLD) {
32+
// 尝试强制移除锁
33+
try { unlinkSync(LOCK_FILE); } catch { }
34+
// 立即重试获取
35+
continue;
36+
}
37+
} catch {
38+
// stat 失败可能意味着锁刚被释放,继续循环尝试获取
39+
}
40+
sleep(25);
41+
continue;
42+
}
2643
}
2744
throw err;
2845
}

0 commit comments

Comments
 (0)