fix(local): 预览槽位加活性校验,watch 退出后不再卡在 502
watch 进程被外部杀掉或 vendor 崩溃后,槽位仍留在网关里,于是所有转发都打到死端口, 面板上只有一句「预览服务不可达」(HEAD 探针只查文件存在性,还回 200,所以更像死链)。 - ensureWatch 拿槽位前先探活,死了就丢掉重起(HEAD 探针也会间接触发) - forward 转发失败时立即丢槽位,下一次导航/刷新即可恢复,不必等 6h 闲置回收 - killSlot 对已退出的子进程不再按 pid 杀(pid 可能已被系统复用)
This commit is contained in:
@@ -339,6 +339,8 @@ health 只作为「端口上有服务但不是本启动器拉起的」兜底提
|
||||
- `/preview/<会话id>/<相对路径>` → 解析真实文件 → 确保有 watch → 剥前缀转发
|
||||
- `/events`(SSE)与 `/api/*`(选区回读、切换、发送)→ 按 `Referer` 认领槽位再转发
|
||||
- 4 个槽位,LRU 挤占 + 闲置回收;watch 端口从 26315 起按代轮转,跳过已被占用的
|
||||
- 槽位带活性校验:watch 自己退了(vendor 崩溃/被外部 kill)就丢掉槽位重起,转发出错也立即丢;
|
||||
否则那份 dead port 会一直转到下次闲置回收,面板上只有一句「预览服务不可达」
|
||||
- 缺 `officecli` 时给「安装说明页」,而不是无限转圈:服务器那份二进制是构建期必装的,
|
||||
`preview.js` 从没为「没装」设计;本地全新 clone 一定没有,这是常态不是异常
|
||||
|
||||
@@ -384,6 +386,7 @@ POSIX 补 `chmod +x`、`officecli-*` 资产名也认)。其余只用 Node 内
|
||||
| 预览数据面:`/events` 是真 SSE、`/api/selection` 204、`/api/send` 200;无 `Referer` 404 | 通过 |
|
||||
| 缺 `officecli`:给安装说明页,HEAD 仍 200(面板不闪死链) | 通过 |
|
||||
| 网关退出:watch 端口释放,无残留进程 | 通过 |
|
||||
| 槽位自愈:`kill -9` 掉 watch 后再请求,自动在下一代端口重起(26315 → 26316),页面照常返回 | 通过 |
|
||||
| 经 core 的 `/preview` 端到端(含手选工作目录、`work_dir` 在 `tasks/` 外的会话) | 待复验 |
|
||||
| `osascript` 真实选目录弹窗 | 未验证(用桩跑的「没选→回落 `~/AgentWork`」分支) |
|
||||
| 真浏览器打开(`open`) | 未验证(用桩记录了被打开的 URL) |
|
||||
|
||||
@@ -137,7 +137,8 @@ function killSlot(abs) {
|
||||
const s = slots.get(abs);
|
||||
if (!s) return;
|
||||
slots.delete(abs);
|
||||
killTree(s.child);
|
||||
// 已退出的进程不再按 pid 杀:pid 可能已被系统回给别的进程组(POSIX 上是 -pid)
|
||||
if (s.child && s.child.exitCode === null && s.child.signalCode === null) killTree(s.child);
|
||||
}
|
||||
|
||||
function evictOne(keep) {
|
||||
@@ -278,7 +279,13 @@ async function spawnWatch(abs, dir) {
|
||||
|
||||
async function ensureWatch(abs, dir) {
|
||||
const have = slots.get(abs);
|
||||
if (have) { have.lastUsed = Date.now(); return have; }
|
||||
if (have) {
|
||||
// watch 可能自己退了(vendor 崩溃、被外部 kill):槽位留着就会一直往死端口转发,
|
||||
// 面板上只看得到一个 502。每次拿槽位前探活,死了就丢掉重起。
|
||||
if (await portListening(have.port)) { have.lastUsed = Date.now(); return have; }
|
||||
log('槽位失效,重起 watch ' + path.basename(abs));
|
||||
killSlot(abs);
|
||||
}
|
||||
const cool = cooldown.get(abs);
|
||||
if (cool && Date.now() < cool) {
|
||||
const e = new Error('刚才启动失败,稍后再试');
|
||||
@@ -303,7 +310,7 @@ async function ensureWatch(abs, dir) {
|
||||
const PASS_HEADERS = new Set(['accept', 'accept-encoding', 'cache-control', 'pragma', 'content-type',
|
||||
'content-length', 'if-modified-since', 'if-none-match', 'last-event-id', 'referer', 'user-agent']);
|
||||
|
||||
function forward(port, req, res, outPath) {
|
||||
function forward(port, req, res, outPath, abs) {
|
||||
const headers = {};
|
||||
for (const k of Object.keys(req.headers)) if (PASS_HEADERS.has(k)) headers[k] = req.headers[k];
|
||||
// watch 的同源校验只认回环:从别的名字(host 名解析到 127.0.0.1)打开 core 时会被 403。
|
||||
@@ -321,7 +328,10 @@ function forward(port, req, res, outPath) {
|
||||
up.pipe(res);
|
||||
});
|
||||
upstream.on('error', () => {
|
||||
if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' }).end('预览服务不可达');
|
||||
// 上游已死:立刻把槽位扔掉,下一次导航(面板重开 / 刷新)会重新拉起,
|
||||
// 而不是让这个会话在槽位闲置回收(6h)之前一直 502。
|
||||
if (abs) killSlot(abs);
|
||||
if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' }).end('预览服务不可达,刷新可重新拉起');
|
||||
else res.destroy();
|
||||
});
|
||||
req.pipe(upstream);
|
||||
@@ -382,7 +392,7 @@ const server = http.createServer((req, res) => {
|
||||
return htmlPage(res, '正在准备预览…', '<p>首次打开需要启动预览进程(大文档要几秒)。</p>', 1);
|
||||
}
|
||||
entry.lastUsed = Date.now();
|
||||
return forward(entry.port, req, res, rawPath.slice(PREFIX.length) || '/');
|
||||
return forward(entry.port, req, res, rawPath.slice(PREFIX.length) || '/', target.abs);
|
||||
}
|
||||
|
||||
// ---- 页内数据面:/events(SSE)与 /api/* 都是根绝对路径 ----
|
||||
@@ -394,7 +404,7 @@ const server = http.createServer((req, res) => {
|
||||
const entry = slots.get(target.abs);
|
||||
if (!entry) return notFound(res); // 槽位已被回收:页面下次导航会重新起
|
||||
entry.lastUsed = Date.now();
|
||||
return forward(entry.port, req, res, req.url);
|
||||
return forward(entry.port, req, res, req.url, target.abs);
|
||||
}
|
||||
|
||||
return notFound(res);
|
||||
|
||||
Reference in New Issue
Block a user