mihomo-cli 3.12.0 → 3.12.1

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,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.12.1] - 2026-09-05
4
+
5
+ 并发数据丢失与信号响应两处稳定性修复。
6
+
7
+ ### 修复
8
+
9
+ - **订阅缓存并发写丢失**:`cache.json` 的读-改-写此前无跨进程保护(`settings.json` 早有 `withFileLock`,缓存被漏掉)。两个 CLI 进程同时写——一个终端 `sub update` 并行下载各自回写、另一个终端 `start` 又触发自动更新——后写者会整块覆盖先写者的条目,实测 4 进程各写 30 条丢 7 条。
10
+
11
+ 丢的是 `updated_at`,后果是该订阅 `needsAutoUpdate` 恒为 true、每次 `start` 都重新下载全量配置,且流量/到期展示一并消失。现在写入路径一律持锁,回归测试锁定(回退修复可稳定复现丢失)
12
+
13
+ - **`stop` 等待进程退出期间 Ctrl+C 无响应**:`cleanupAll` 用 `Atomics.wait` 忙等最多 5 秒、`stopSshTunnel` 最多 4 秒,其间事件循环被完全阻塞,SIGINT 要等轮询走完才被处理(实测 5.3 秒)。用户在 `mihomo stop` 卡住时按 Ctrl+C 会以为 CLI 挂死。改用 async 轮询后实测 102ms 响应
14
+
15
+ ### 内部
16
+
17
+ - `cleanupAll` / `stop` / `stopSshTunnel` / `stopAutoSshTunnels` / `stopAllSshTunnels` 改为 async;`ResetTarget.onBefore` 放宽为 `() => void | Promise<void>`。`withFileLock` 内的同步忙等**保持不变**——持锁期间让出事件循环会让另一进程等到强夺陈旧锁
18
+ - 删除 `ConfigInfo.mode` 死字段(解析并声明,但无任何消费者)
19
+
3
20
  ## [3.12.0] - 2026-09-04
4
21
 
5
22
  `ssh` 弱化为只管端口,配置层剥离;内部重构与命令合并,减少冗余。
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ var colors = {
19
19
  import { spawnSync } from "child_process";
20
20
  import fs4 from "fs";
21
21
 
22
- // node_modules/js-yaml/dist/js-yaml.mjs
22
+ // ../../../node_modules/js-yaml/dist/js-yaml.mjs
23
23
  var NOT_RESOLVED = /* @__PURE__ */ Symbol("NOT_RESOLVED");
24
24
  function defineScalarTag(tagName, options) {
25
25
  return {
@@ -3377,9 +3377,21 @@ function writeSubscriptionCache(cache) {
3377
3377
  atomicWriteFileSync(PATHS.subscriptionsCacheFile, JSON.stringify(cache, null, 2), { mode: 384 });
3378
3378
  }
3379
3379
  function saveSubscriptionCache(subName, data) {
3380
- const cache = readSubscriptionCache();
3381
- cache[subName] = { ...cache[subName], ...data };
3382
- writeSubscriptionCache(cache);
3380
+ ensureDirs();
3381
+ withFileLock(PATHS.subscriptionsCacheFile, () => {
3382
+ const cache = readSubscriptionCache();
3383
+ cache[subName] = { ...cache[subName], ...data };
3384
+ writeSubscriptionCache(cache);
3385
+ });
3386
+ }
3387
+ function deleteSubscriptionCache(subName) {
3388
+ ensureDirs();
3389
+ withFileLock(PATHS.subscriptionsCacheFile, () => {
3390
+ const cache = readSubscriptionCache();
3391
+ if (!cache[subName]) return;
3392
+ delete cache[subName];
3393
+ writeSubscriptionCache(cache);
3394
+ });
3383
3395
  }
3384
3396
  function readSettingsList(key, isValid, reAddHint) {
3385
3397
  const list = readSettings()[key];
@@ -3459,11 +3471,7 @@ function removeSubscription(name) {
3459
3471
  return updates;
3460
3472
  });
3461
3473
  if (!found) return null;
3462
- const cache = readSubscriptionCache();
3463
- if (cache[name]) {
3464
- delete cache[name];
3465
- writeSubscriptionCache(cache);
3466
- }
3474
+ deleteSubscriptionCache(name);
3467
3475
  try {
3468
3476
  fs2.rmSync(getSubscriptionRawConfigPath(name), { force: true });
3469
3477
  } catch {
@@ -3737,9 +3745,6 @@ function listOverwriteFile() {
3737
3745
 
3738
3746
  // src/utils.ts
3739
3747
  var sleepBuf = new Int32Array(new SharedArrayBuffer(4));
3740
- function sleepSync(ms) {
3741
- Atomics.wait(sleepBuf, 0, 0, ms);
3742
- }
3743
3748
  function sleep(ms) {
3744
3749
  return new Promise((resolve) => setTimeout(resolve, ms));
3745
3750
  }
@@ -4256,7 +4261,6 @@ function getConfigInfo() {
4256
4261
  return {
4257
4262
  proxies: proxies ? proxies.length : 0,
4258
4263
  proxyGroups: proxyGroups ? proxyGroups.length : 0,
4259
- mode: cfg.mode || "rule",
4260
4264
  mixedPort: cfg["mixed-port"] || null,
4261
4265
  httpPort: cfg.port || null,
4262
4266
  socksPort: cfg["socks-port"] || null,
@@ -5192,7 +5196,7 @@ function killAllMihomo(forceSudo = false) {
5192
5196
  }
5193
5197
  }
5194
5198
  }
5195
- function cleanupAll(forceSudo = false) {
5199
+ async function cleanupAll(forceSudo = false) {
5196
5200
  const pids = getMihomoPids();
5197
5201
  if (pids.length === 0) {
5198
5202
  clearPid();
@@ -5225,19 +5229,19 @@ function cleanupAll(forceSudo = false) {
5225
5229
  }
5226
5230
  for (let i = 0; i < PROCESS_WAIT_ATTEMPTS; i++) {
5227
5231
  if (getMihomoPids().length === 0) break;
5228
- sleepSync(PROCESS_WAIT_INTERVAL);
5232
+ await sleep(PROCESS_WAIT_INTERVAL);
5229
5233
  }
5230
5234
  clearPid();
5231
5235
  return { killed: killedCount, failed: failedPids.length, remaining: getMihomoPids() };
5232
5236
  }
5233
- function stop(forceSudo = false) {
5237
+ async function stop(forceSudo = false) {
5234
5238
  const allPids = getMihomoPids();
5235
5239
  if (allPids.length === 0) {
5236
5240
  clearPid();
5237
5241
  clearRuntime();
5238
5242
  return { success: true, notRunning: true };
5239
5243
  }
5240
- const result = cleanupAll(forceSudo);
5244
+ const result = await cleanupAll(forceSudo);
5241
5245
  const remaining = getMihomoPids();
5242
5246
  if (remaining.length > 0) {
5243
5247
  console.log("");
@@ -5330,7 +5334,7 @@ async function startMixedMode(staleState) {
5330
5334
  console.log("\u6216\u8005\u5207\u6362\u5230 TUN \u6A21\u5F0F\uFF0C\u542F\u52A8\u65F6\u4F1A\u81EA\u52A8\u6E05\u7406");
5331
5335
  throw new Error("\u5B58\u5728\u9700\u8981 root \u6743\u9650\u6E05\u7406\u7684\u6B8B\u7559");
5332
5336
  }
5333
- const cleanupResult = cleanupAll();
5337
+ const cleanupResult = await cleanupAll();
5334
5338
  if (cleanupResult.killed > 0) {
5335
5339
  console.log(`\u6E05\u7406\u4E86 ${cleanupResult.killed} \u4E2A\u6B8B\u7559\u8FDB\u7A0B`);
5336
5340
  }
@@ -5715,7 +5719,7 @@ function readLogTail(logPath, maxLines = 5) {
5715
5719
  return [];
5716
5720
  }
5717
5721
  }
5718
- function stopSshTunnel(name) {
5722
+ async function stopSshTunnel(name) {
5719
5723
  const config = findSshTunnel(name);
5720
5724
  const runtime = readSshRuntime(name);
5721
5725
  if (!runtime) {
@@ -5734,7 +5738,7 @@ function stopSshTunnel(name) {
5734
5738
  }
5735
5739
  for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
5736
5740
  if (!isProcessRunning(pid)) break;
5737
- sleepSync(STOP_WAIT_INTERVAL);
5741
+ await sleep(STOP_WAIT_INTERVAL);
5738
5742
  }
5739
5743
  if (isProcessRunning(pid) && isProcessCommandMatching(pid, commandNeedle(port))) {
5740
5744
  try {
@@ -5743,7 +5747,7 @@ function stopSshTunnel(name) {
5743
5747
  }
5744
5748
  for (let i = 0; i < STOP_WAIT_ATTEMPTS; i++) {
5745
5749
  if (!isProcessRunning(pid)) break;
5746
- sleepSync(STOP_WAIT_INTERVAL);
5750
+ await sleep(STOP_WAIT_INTERVAL);
5747
5751
  }
5748
5752
  }
5749
5753
  clearSshRuntime(name);
@@ -5766,20 +5770,20 @@ async function startAutoSshTunnels() {
5766
5770
  }
5767
5771
  return outcomes;
5768
5772
  }
5769
- function stopAutoSshTunnels() {
5773
+ async function stopAutoSshTunnels() {
5770
5774
  const stopped = [];
5771
5775
  for (const tunnel of getSshTunnels()) {
5772
5776
  const runtime = readSshRuntime(tunnel.name);
5773
5777
  if (runtime?.started_by !== "auto") continue;
5774
- const result = stopSshTunnel(tunnel.name);
5778
+ const result = await stopSshTunnel(tunnel.name);
5775
5779
  if (!result.notRunning) stopped.push(tunnel.name);
5776
5780
  }
5777
5781
  return stopped;
5778
5782
  }
5779
- function stopAllSshTunnels() {
5783
+ async function stopAllSshTunnels() {
5780
5784
  const stopped = [];
5781
5785
  for (const tunnel of getSshTunnels()) {
5782
- const result = stopSshTunnel(tunnel.name);
5786
+ const result = await stopSshTunnel(tunnel.name);
5783
5787
  if (!result.notRunning) stopped.push(tunnel.name);
5784
5788
  }
5785
5789
  return stopped;
@@ -5868,8 +5872,8 @@ function handleStopResult(result) {
5868
5872
  throw new CliError(result.remaining.join(", "), { label: "\u90E8\u5206\u8FDB\u7A0B\u672A\u7EC8\u6B62", hint: "\u8BF7\u624B\u52A8\u8FD0\u884C: sudo pkill -9 mihomo" });
5869
5873
  }
5870
5874
  }
5871
- function stopAutoSshTunnelsWithLog() {
5872
- const stopped = stopAutoSshTunnels();
5875
+ async function stopAutoSshTunnelsWithLog() {
5876
+ const stopped = await stopAutoSshTunnels();
5873
5877
  if (stopped.length > 0) {
5874
5878
  console.log(`${colors.green("\u5DF2\u505C\u6B62\u96A7\u9053")}: ${stopped.join(", ")}`);
5875
5879
  }
@@ -5884,13 +5888,13 @@ async function cmdStop(args) {
5884
5888
  const pids = getMihomoPids();
5885
5889
  if (pids.length === 0) {
5886
5890
  console.log(colors.yellow("\u4E0D\u5728\u8FD0\u884C"));
5887
- if (!skipSsh) stopAutoSshTunnelsWithLog();
5891
+ if (!skipSsh) await stopAutoSshTunnelsWithLog();
5888
5892
  return;
5889
5893
  }
5890
5894
  console.log(`\u505C\u6B62 ${pids.length} \u4E2A\u8FDB\u7A0B...`);
5891
- handleStopResult(stop());
5895
+ handleStopResult(await stop());
5892
5896
  console.log(colors.green("\u5DF2\u505C\u6B62\u8FDB\u7A0B"));
5893
- if (!skipSsh) stopAutoSshTunnelsWithLog();
5897
+ if (!skipSsh) await stopAutoSshTunnelsWithLog();
5894
5898
  }
5895
5899
 
5896
5900
  // src/commands/start.ts
@@ -5956,7 +5960,7 @@ async function cmdStart(args) {
5956
5960
  const count = status.allProcesses.length > 0 ? status.allProcesses.length : 1;
5957
5961
  console.log(`\u505C\u6B62 ${count} \u4E2A\u8FDB\u7A0B...`);
5958
5962
  }
5959
- handleStopResult(stop());
5963
+ handleStopResult(await stop());
5960
5964
  if (hasProcess) {
5961
5965
  console.log(`${colors.green("\u5DF2\u505C\u6B62\u8FDB\u7A0B")}
5962
5966
  `);
@@ -6212,7 +6216,7 @@ import { spawnSync as spawnSync5 } from "child_process";
6212
6216
  import fs12 from "fs";
6213
6217
  import path8 from "path";
6214
6218
 
6215
- // node_modules/compare-versions/lib/esm/utils.js
6219
+ // ../../../node_modules/compare-versions/lib/esm/utils.js
6216
6220
  var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
6217
6221
  var validateAndParse = (version) => {
6218
6222
  if (typeof version !== "string") {
@@ -6250,7 +6254,7 @@ var compareSegments = (a, b) => {
6250
6254
  return 0;
6251
6255
  };
6252
6256
 
6253
- // node_modules/compare-versions/lib/esm/compareVersions.js
6257
+ // ../../../node_modules/compare-versions/lib/esm/compareVersions.js
6254
6258
  var compareVersions = (v1, v2) => {
6255
6259
  const n1 = validateAndParse(v1);
6256
6260
  const n2 = validateAndParse(v2);
@@ -6780,7 +6784,9 @@ var RESET_TARGETS = [
6780
6784
  // 维护的 overwrite.yaml 里(归 overwrites 目标管),这里没有额外文件要清
6781
6785
  paths: () => [DIRS.ssh],
6782
6786
  needsStop: false,
6783
- onBefore: () => stopAllSshTunnels(),
6787
+ onBefore: async () => {
6788
+ await stopAllSshTunnels();
6789
+ },
6784
6790
  // 同步清空 settings 里的隧道列表:只删运行态会留下「列表在但状态没了」的半重置状态
6785
6791
  onAfter: () => writeSettings({ ssh: void 0 })
6786
6792
  },
@@ -6917,7 +6923,7 @@ async function cmdReset(args) {
6917
6923
  }
6918
6924
  if (needsStop && getMihomoPids().length > 0) {
6919
6925
  console.log("\u505C\u6B62\u8FDB\u7A0B...");
6920
- const cleanup = cleanupAll();
6926
+ const cleanup = await cleanupAll();
6921
6927
  for (let i = 0; i < PROCESS_WAIT_ATTEMPTS; i++) {
6922
6928
  if (getMihomoPids().length === 0) break;
6923
6929
  await new Promise((r) => setTimeout(r, PROCESS_WAIT_INTERVAL));
@@ -6935,7 +6941,7 @@ async function cmdReset(args) {
6935
6941
  }
6936
6942
  }
6937
6943
  for (const t of targets) {
6938
- t.onBefore?.();
6944
+ await t.onBefore?.();
6939
6945
  for (const p of t.paths()) {
6940
6946
  if (fs13.existsSync(p)) {
6941
6947
  try {
@@ -7069,7 +7075,7 @@ async function sshUp(args) {
7069
7075
  }
7070
7076
  }
7071
7077
  }
7072
- function sshDown(args) {
7078
+ async function sshDown(args) {
7073
7079
  const name = getSshNameArg(args);
7074
7080
  const targets = name ? [resolveSshTunnel(name)] : getSshTunnels();
7075
7081
  if (targets.length === 0) {
@@ -7077,7 +7083,7 @@ function sshDown(args) {
7077
7083
  }
7078
7084
  let stopped = 0;
7079
7085
  for (const config of targets) {
7080
- const result = stopSshTunnel(config.name);
7086
+ const result = await stopSshTunnel(config.name);
7081
7087
  if (result.notRunning) {
7082
7088
  console.log(`${colors.yellow("\u4E0D\u5728\u8FD0\u884C")} ${config.name}`);
7083
7089
  } else {
@@ -7129,7 +7135,7 @@ async function sshRemove(args) {
7129
7135
  return;
7130
7136
  }
7131
7137
  }
7132
- stopSshTunnel(config.name);
7138
+ await stopSshTunnel(config.name);
7133
7139
  removeSshTunnel(config.name);
7134
7140
  console.log(`${colors.green("\u5DF2\u5220\u9664\u96A7\u9053")} ${config.name}`);
7135
7141
  console.log("");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mihomo-cli",
3
- "version": "3.12.0",
3
+ "version": "3.12.1",
4
4
  "type": "module",
5
5
  "description": "A terminal-based mihomo (Clash.Meta) client for macOS",
6
6
  "bin": {