Files
37duAutomation/docs/桌面端/壳子自动升级失效事件复盘.md
T
puddy 1d66167156 chore(center): 下线第一代中心机并清除旧公网信息
- 删除 desktop/controlplane(FastAPI 旧中心机)全部代码,历史见 git

- 移除依赖旧中心机模块的两个失效测试

- AGENTS/README/docs 表述改为「已移除/只有内网一套」,去掉旧公网地址与宝塔部署信息

- agent_cli 去掉旧库拦截逻辑,--base 帮助指向内网生产;electron-shell .env.example 默认地址改内网
2026-11-06 15:40:37 +08:00

127 lines
7.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 壳子自动升级失效事件复盘(2026-09-23)
> 性质:线上功能事故(执行机壳子自动升级长期失效,1.1.8 → 1.2.0 → 1.2.1 均无法自动检测到新版本,用户只能手动升级)。
> 结论:根因 = `shell.version` 版本标记文件带 UTF-8 BOM,叠加版本比较兜底逻辑缺陷,导致执行机永远判定"已是最新"。
> 状态:已修复(代码 3 处 + 文件固化),执行机应急去 BOM 后恢复升级。
---
## 1. 事件摘要
| 项 | 内容 |
|---|---|
| 时间 | 2026-09-23(排查当日修复) |
| 影响 | 全部存量执行机壳子无法自动升级(1.1.8 / 1.2.0 均检测不到新版本),只能手动下载安装包升级 |
| 现象 | 执行机点"检查更新"提示"已是最新版本 v1.2.0";手动升级正常 |
| 直接根因 | 执行机 `shell.version` 文件开头带 UTF-8 BOM 字节(`EF BB BF`),程序读成 `"\ufeff1.2.0"`,版本比较永远判定"不大于" |
| 隐性根因 | `_version_lt()` 比较异常时走字符串兜底,BOM 码点(U+FEFF)大于数字字符,兜底结果恒为 False |
| 历史背景 | 服务端旧代码 `/api/shell/version` 只读 `version.json`(内容停在 1.0.0),发布走 `file push` 从不更新 version.json,也是"1.1.8 时代升不了"的因素之一;新版已优先读 `file_sources` 表 |
---
## 2. 升级链路与根因剖析
### 2.1 正常升级链路
```
执行机(启动/每30分钟/手动"检查更新")
→ GET /api/shell/version (X-API-Token)
→ 服务端:优先 file_sources(category='shell') 最新启用记录;无则回退 shell/version.json
→ 执行机 local_shell_version() 读 exe 同级 shell.version
→ _version_lt(本机, 云端) 为 True → available=True → 前端提示"发现新版本"
→ 下载(内网文件服务器直链或 /api/shell/pkg)→ sha256 校验 → 用户确认重启升级
```
### 2.2 根因(两个缺陷叠加)
**缺陷 A:版本文件带 UTF-8 BOM**
- `webhost/tools/shell.version` 历史提交内容均带 BOM(git 中显示为 `1.1.8`、`1.2.0`),Inno Setup 打包时原样复制进安装包(`install.iss`:`Source: "..\tools\shell.version"`),执行机安装后文件带 BOM。
- 程序读取用 `read_text(encoding="utf-8")`,**不剥离 BOM** → 得到 `"\ufeff1.2.0"`。
- 记事本打开 BOM 不可见,肉眼无法发现。
**缺陷 B:`_version_lt()` 兜底比较逻辑缺陷**
```python
def _key(v): return [int(x) if x.isdigit() else x for x in str(v).split(".")]
try:
return _key(a) < _key(b)
except Exception:
return a != b and a < b # ← 兜底
```
- 带 BOM 时 `_key("\ufeff1.2.0")` 首元素是字符串 `"\ufeff1"`,与云端 int `1` 比较 → `TypeError` → 走兜底。
- 兜底字符串比较:`"\ufeff1.2.0" < "1.2.1"` 按码点比较,`\ufeff`(65279) > `'1'`(49) → **恒为 False** → 永远"已是最新"。
**叠加效果**:任何版本(1.1.8 / 1.2.0)都检测不到更新,只有手动升级;1.1.8 时代服务端只读 version.json(1.0.0) 进一步加重。
---
## 3. 排查过程(证据链)
| 步骤 | 验证 | 结论 |
|---|---|---|
| 1. 查线上库 file_sources(shell) | id=9 已登记 `version=1.2.1, enabled=1` | 数据正确 |
| 2. 本地新版代码 + 线上库调 `/api/shell/version` | 返回 `{"version":"1.2.1",...}` | 服务端新版逻辑正确 |
| 3. 直连当时线上控制面调 `/api/shell/version` | 返回 1.2.1;`/health` = `{"ok":true}` | 线上已部署新版 |
| 4. 客户端 `_version_lt("1.2.0","1.2.1")` | 复现 True | 比对逻辑正常(无 BOM 时) |
| 5. 执行机 shell.version 文件内容 | 用户提供:"1.2.0"(记事本) | 表面正常 |
| 6. 执行机 curl 中心机 `/api/shell/version` | 返回 1.2.1 | 网络/服务端/鉴权正常,**问题在客户端进程内** |
| 7. 执行机文件字节检查 | `239,187,191,49` = **EF BB BF + '1'** | **BOM 实锤** |
| 8. 复现带 BOM 版本比较 | TypeError → 兜底 False → "已是最新" | 根因闭环 |
关键排除项:客户端检查 UI 逻辑(轮询等待结果)、`pending_status` 字段映射、token 配置、网络连通——均正常,逐一排除后锁定字节级文件问题。
---
## 4. 修复内容
### 4.1 代码修复(治本,3 处同类隐患)
| 文件 | 位置 | 修复 |
|---|---|---|
| `desktop/webhost/backend/core/shell_updater.py` | `local_shell_version()` | `read_text(encoding="utf-8")` → `utf-8-sig`(自动剥离 BOM) |
| `desktop/webhost/backend/core/plugin_sync.py` | `_local_version()` | 同上(plugin.json 带 BOM 会让 `json.loads` 抛 JSONDecodeError,误判为新装重下插件) |
| `desktop/controlplane/apis/plugins.py` | `_shell_meta()` | 同上(version.json 带 BOM 会读失败) |
### 4.2 文件固化
- `webhost/tools/shell.version`:确认当前为无 BOM 的纯 UTF-8(`31 2e 32 2e 31` = "1.2.1"),后续打包不会带 BOM。
### 4.3 应急方案(存量执行机立即恢复,无需等新包)
```powershell
$p = "D:\Program Files (x86)\37duHost\shell.version" # 以实际安装目录为准
$c = [System.IO.File]::ReadAllText($p).Trim()
[System.IO.File]::WriteAllText($p, $c, (New-Object System.Text.UTF8Encoding($false)))
# 重启客户端或点"检查更新" → 应提示"发现新版本"
```
注:`%LOCALAPPDATA%\Programs\37duHost\` 与 `D:\Program Files (x86)\37duHost\` 均可能为安装目录,以实际为准;Program Files 下写文件需管理员权限。
---
## 5. 预防措施(沉淀为规范)
1. **版本标记文件写入规范**:所有 `.version` / `version.json` / `plugin.json` 类文件**必须存为无 BOM 的 UTF-8**(VS Code 右下角编码选 "UTF-8",勿用 "UTF-8 with BOM";记事本"另存为"选 UTF-8 时会带 BOM,需注意)。
2. **代码读取规范**:读取含版本号的文本文件统一用 `encoding="utf-8-sig"`(兼容带/不带 BOM),JSON 文件用 `json.loads(read_text("utf-8-sig"))`。
3. **打包前检查**:打包安装包前用字节检查确认版本文件无 BOM:
```powershell
$b = [System.IO.File]::ReadAllBytes("D:\work\code\37duAutomation\desktop\webhost\tools\shell.version"); ($b[0..2] -join ',')
# 239,187,191 = 有 BOM(禁止发布);49,46,50 = "1.2" 无 BOM(正常)
```
4. **发布双源一致**:壳子版本发布走 `file push --category shell`(登记 file_sources)与 `shell-release`(写 version.json)时,两处 version 保持一致;新版服务端优先 file_sources,`version.json` 仅为回退源。
5. **升级链路自检**:升级发布后,先在一台执行机点"检查更新"验证出现"发现新版本",再全量推送。
---
## 6. 相关文件索引
| 文件 | 作用 |
|---|---|
| `desktop/webhost/backend/core/shell_updater.py` | 壳子检查/下载/应用(本次修复点 1) |
| `desktop/webhost/backend/core/plugin_sync.py` | 插件同步(本次修复点 2) |
| `desktop/controlplane/apis/plugins.py` | `/api/shell/version`、`_shell_meta`(本次修复点 3) |
| `desktop/webhost/tools/shell.version` | 壳子版本标记(已固化无 BOM) |
| `desktop/webhost/tools/install.iss` | Inno Setup 打包脚本(shell.version 进包) |
| `docs/桌面端/Node版客户端开发交接文档.md` | Node 版客户端开发参考(含升级链路 §7) |
---
## 7. 待办/预警
- **内网直链下载隐患**:`/api/shell/version` 返回的 `url` 是内网文件服务器直链(如 `http://172.30.35.135:8802/...`)。若执行机不在内网(公网/异地),能检查到新版本但**下载会失败**。后续如需支持公网执行机,服务端应针对公网来源返回空 `url`,让执行机回退走 `/api/shell/pkg`(中心机 8800 下载)。
- 存量执行机版本参差(含手动升级的 1.2.0),建议借本次 1.2.1 全量收敛到统一版本,后续版本号以 `shell.version`(无 BOM)为准。