mihomo-cli 4.7.3 → 4.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.7.4] - 2026-09-06
4
+
5
+ 独立复审一轮,聚焦「上轮文档未覆盖」的缺口。两处并发/可见性缺陷均已实测复现后修复。单测 290(+7)。
6
+
7
+ ### 修复
8
+
9
+ - **`start`/`install` 报「退出码 null」,信号死亡的死因在启动路径上不可见**:v4.7.3 补信号判据时把 status/doctor 的重复判断收口成 `describeAbnormalExit`,**却漏了 `runtime.assertServiceHealthy` 这第三个消费者**——它仍自己拼 `退出码 ${exitCode}`,而信号死亡时 launchd 不写 `last exit code`(整行消失,本轮用一次性 label + 桩内核再次实测确认),于是 `start` 期间被 OOM killer 杀掉的内核只显示「内核启动后立即退出(退出码 null)」,`ServiceHealth.terminatingSignal` 一路传过来却无人读。判据现收口成 `describeExitCause(exitCode, signal)` **一份**,三个消费者共用(`isCrashed` 判有无、`describeAbnormalExit` 供 status/doctor、`assertServiceHealthy` 供 start/install),修复后文案为「被信号终止(Killed: 9)」。**教训**:把实测事实锚在注释里挡不住漏铺——注释锚在事实发生处,防线要铺在所有消费处,两者不重合时只有「判据收成一个函数」才真的有效
10
+ - **`service.lock` 放在会被整体删除的 `runtime/` 下,跨进程互斥可被打破**:`stop()` 的 `clearRuntime()` 与 `reset runtime` 都会 `rmrf(DIRS.runtime)`,把**别的进程正持着的锁文件**一起删掉——第三个进程随即 `openSync(..., 'wx')` 成功,两个进程同时进入临界区(已实测复现)。`withFileLock` 的 token 所有权校验对此无效:它防的是「被强夺者误删新持有者的锁」,而这里锁是被第三方连目录一起删的,持锁方毫不知情。可达路径正是文档反复强调的那个——慢速 `start`(订阅更新约 10s)持锁期间另一终端 `stop`,后果是自启位终态与用户最后一条命令相反。锁现移到 `USER_DATA_DIR` 根下
11
+
12
+ ### 测试
13
+
14
+ - 新增 `describeExitCause` 判据本身的用例(5 例),含「不能退化成只看退出码」的锁定断言——直接对判据断言而非各消费者,避免再出现「收口了却漏一个消费者」
15
+ - 新增锁存放位置的用例(2 例):断言的是**不变量**「`service.lock` 不在任何会被 `rmrf` 的目录下」而非具体路径(后者会在目录结构调整时误报),另一例记录缺陷机制本身。已验证该测试对旧位置确实失败
16
+
17
+ ### 变更
18
+
19
+ - **`CLAUDE.md` 修正一处写反的记载**:「内核下载的来源信任」的 quickstart.sh 对照表里写「无标准版资产时回退第一个匹配项(CLI 不回退)」,但 `kernel.ts` 是 `standardAsset || matchingAssets[0]`,且 `kernel.spec.ts` 有单测锁定回退行为——两侧其实一致,是文档写反了。这段文字的用途正是两侧对齐的对照表,写反会误导下次对齐。顺带指明真正不回退的是 `pickLatestRelease`(全预发布时抛错)
20
+ - **`CLAUDE.md` 新增两条纪律**:锁文件不能放在会被 `rmrf` 的目录里(附受影响目录清单);异常退出判据只有 `describeExitCause` 一份,别散写 `lastExitCode !== 0` 也别自己拼退出码文案
21
+ - 去掉 4 处对同步函数(`bootoutService` / `withFileLock`)的 `await`——`withFileLock` 明确要求 `fn` 同步(持锁期间让出事件循环等于没锁),`await` 会暗示可传 async mutator
22
+ - README 同步两处:`start` 失败报错会写明死因(与 `status` 同口径);数据目录树补上 `service.lock` 并注明为何放在根下
23
+
3
24
  ## [4.7.3] - 2026-09-06
4
25
 
5
26
  清掉 CODE_REVIEW 积压的全部未处理项(一条修复、一条决策豁免)。单测 283(+37)。
package/README.md CHANGED
@@ -377,6 +377,7 @@ mihomo start --update-timeout=30000 # 长选项 + 等号
377
377
  ```
378
378
  ~/.mihomo-cli/
379
379
  ├── settings.json # 用户设置(订阅列表、当前订阅、覆写开关、端口覆盖等)
380
+ ├── service.lock # 服务启停的跨进程锁(刻意放在根下:runtime/ 会被 stop 整体清除)
380
381
  ├── overwrite.yaml # 覆写配置(主文件,可选)
381
382
  ├── overwrite.*.yaml # 覆写配置(扩展文件,如 overwrite.dns.yaml)
382
383
  ├── subscriptions/
@@ -533,7 +534,7 @@ mihomo doctor
533
534
 
534
535
  ### 启动失败
535
536
 
536
- `mihomo start` 会确认内核真的跑起来了才报「已启动」——内核因配置问题启动后立即退出时,命令会报错并直接附上日志尾部,退出码非 0。常见原因是端口被占用或订阅里有内核不接受的字段。
537
+ `mihomo start` 会确认内核真的跑起来了才报「已启动」——内核因配置问题启动后立即退出时,命令会报错并直接附上日志尾部,退出码非 0。常见原因是端口被占用或订阅里有内核不接受的字段。报错会写明死因,与下面 `status` 的提示同口径(`退出码 N` 或 `被信号终止(Killed: 9)`)。
537
538
 
538
539
  ```bash
539
540
  mihomo logs 0 # 看完整原因
package/dist/index.js CHANGED
@@ -3190,8 +3190,17 @@ var PATHS = {
3190
3190
  configFile: path.join(DIRS.runtime, "config.yaml"),
3191
3191
  logFile: path.join(DIRS.logs, "mihomo.log"),
3192
3192
  pidFile: path.join(DIRS.runtime, "pid"),
3193
- /** 服务操作跨进程锁:start/stop/install/uninstall 的 enable/bootstrap/bootout/disable 串行化 */
3194
- serviceLock: path.join(DIRS.runtime, "service.lock"),
3193
+ /**
3194
+ * 服务操作跨进程锁:start/stop/install/uninstall 的 enable/bootstrap/bootout/disable 串行化。
3195
+ *
3196
+ * **必须放在 USER_DATA_DIR 根下,不能放 runtime/**:`stop()` 的 clearRuntime() 与
3197
+ * `reset runtime` 都会 `rmrf(DIRS.runtime)`,把别的进程正持着的锁文件一起删掉——
3198
+ * 于是第三个进程立刻 `openSync(..., 'wx')` 成功,两个进程同时进临界区(实测复现)。
3199
+ * withFileLock 的 token 所有权校验挡不住这种情形:它防的是「被强夺者误删新持有者的锁」,
3200
+ * 而这里锁是被第三方连目录一起删的,持锁方毫不知情。
3201
+ * 可达路径正是文档反复强调的那个:慢速 start(订阅更新约 10s)持锁期间另一终端 stop。
3202
+ */
3203
+ serviceLock: path.join(USER_DATA_DIR, "service.lock"),
3195
3204
  configStage1Subscription: path.join(DIRS.runtime, "1.subscription.yaml"),
3196
3205
  configStage2Overwrite: path.join(DIRS.runtime, "2.overwrite.yaml"),
3197
3206
  configStage3System: path.join(DIRS.runtime, "3.system.yaml"),
@@ -4881,15 +4890,17 @@ async function waitServiceHealthy() {
4881
4890
  }
4882
4891
  return { healthy: false, crashed: false, pid: last.pid, exitCode: last.lastExitCode, terminatingSignal: last.lastTerminatingSignal };
4883
4892
  }
4893
+ function describeExitCause(exitCode, terminatingSignal) {
4894
+ if (terminatingSignal !== null) return `\u88AB\u4FE1\u53F7\u7EC8\u6B62\uFF08${terminatingSignal}\uFF09`;
4895
+ if (exitCode !== null && exitCode !== 0) return `\u9000\u51FA\u7801 ${exitCode}`;
4896
+ return null;
4897
+ }
4884
4898
  function isCrashed(status) {
4885
4899
  if (status.running) return false;
4886
- if (status.lastTerminatingSignal !== null) return true;
4887
- return status.lastExitCode !== null && status.lastExitCode !== 0;
4900
+ return describeExitCause(status.lastExitCode, status.lastTerminatingSignal) !== null;
4888
4901
  }
4889
4902
  function describeAbnormalExit(status) {
4890
- if (status.lastTerminatingSignal !== null) return `\u88AB\u4FE1\u53F7\u7EC8\u6B62\uFF08${status.lastTerminatingSignal}\uFF09`;
4891
- if (status.lastExitCode !== null && status.lastExitCode !== 0) return `\u9000\u51FA\u7801 ${status.lastExitCode}`;
4892
- return null;
4903
+ return describeExitCause(status.lastExitCode, status.lastTerminatingSignal);
4893
4904
  }
4894
4905
  function escapeXml(s) {
4895
4906
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
@@ -5019,7 +5030,7 @@ async function installService(wasRunning) {
5019
5030
  hint: [(lint.stderr || lint.stdout || "").trim()].filter(Boolean)
5020
5031
  });
5021
5032
  }
5022
- await bootoutService();
5033
+ bootoutService();
5023
5034
  await waitUntilUnloaded();
5024
5035
  fs8.mkdirSync(path6.dirname(PATHS.userAgentPlist), { recursive: true });
5025
5036
  fs8.copyFileSync(stagePath, PATHS.userAgentPlist);
@@ -5061,7 +5072,7 @@ async function startService() {
5061
5072
  });
5062
5073
  }
5063
5074
  cleanupRootResidue();
5064
- await bootoutService();
5075
+ bootoutService();
5065
5076
  await waitUntilUnloaded();
5066
5077
  rotateAndCleanupLogs();
5067
5078
  withFileLock(PATHS.serviceLock, () => {
@@ -5083,7 +5094,7 @@ function disableServiceAutoStart() {
5083
5094
  }
5084
5095
  async function stopService() {
5085
5096
  assertServiceLabelSafe();
5086
- await withFileLock(PATHS.serviceLock, () => {
5097
+ withFileLock(PATHS.serviceLock, () => {
5087
5098
  bootoutService();
5088
5099
  disableServiceAutoStart();
5089
5100
  });
@@ -5092,7 +5103,7 @@ async function stopService() {
5092
5103
  }
5093
5104
  async function uninstallService() {
5094
5105
  assertServiceLabelSafe();
5095
- await withFileLock(PATHS.serviceLock, () => {
5106
+ withFileLock(PATHS.serviceLock, () => {
5096
5107
  bootoutService();
5097
5108
  disableServiceAutoStart();
5098
5109
  });
@@ -5502,7 +5513,8 @@ async function assertServiceHealthy(label = "\u542F\u52A8\u5931\u8D25") {
5502
5513
  const health = await waitServiceHealthy();
5503
5514
  if (health.healthy) return health.pid;
5504
5515
  const tail = readLogTail();
5505
- const reason = health.crashed ? `\u5185\u6838\u542F\u52A8\u540E\u7ACB\u5373\u9000\u51FA\uFF08\u9000\u51FA\u7801 ${health.exitCode}\uFF09` : "\u5185\u6838\u672A\u80FD\u8FDB\u5165\u8FD0\u884C\u72B6\u6001";
5516
+ const cause = describeExitCause(health.exitCode, health.terminatingSignal);
5517
+ const reason = health.crashed ? `\u5185\u6838\u542F\u52A8\u540E\u7ACB\u5373\u9000\u51FA\uFF08${cause ?? "\u6B7B\u56E0\u672A\u8BB0\u5F55"}\uFF09` : "\u5185\u6838\u672A\u80FD\u8FDB\u5165\u8FD0\u884C\u72B6\u6001";
5506
5518
  throw new CliError(reason, {
5507
5519
  label,
5508
5520
  hint: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mihomo-cli",
3
- "version": "4.7.3",
3
+ "version": "4.7.4",
4
4
  "type": "module",
5
5
  "description": "A terminal-based mihomo (Clash.Meta) client for macOS",
6
6
  "bin": {