fix(web): stop short/read/control from filling execute slots

Isolating every short invoke on the 2-slot worker pool caused no_slot under
normal traffic; keep short paths on parent (execute already off-process) and
background-reap after observer disconnect so zombies release slots.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-10-27 08:56:56 +08:00
1 parent 9af929971c
commit b8265a92aa
2 files changed
+50 -18

No files matched your search

+6 -6
View File
@@ -14,19 +14,19 @@ ADR 0008 确认:Go HTTP 多线程,但所有 Endpoint 共享一把解释器
## 决策
1. **首选架构**:预热的一次性 Marqdo worker 进程池;每个租约独占一个进程与 HostContext。
2. **HTTP 默认**:声明的 `class=execute|short|read|control`,或 `return=sse-live` / body `stream:true` → `admit` → `dispatch`(`--call-lib-path` worker);**禁止**父进程 `CallLibPath` / `Setenv` / `legacySSEBridgeMu`。未声明非流式 → `legacy_serial`(仍父锁)。
3. **控制/读取分离**:`/_runtime/*`(cancel、events、task get)不进入解释器锁;`short`/`read`/`control` 走独立 admission 槽与隔离 worker,不与父进程 `INVOKE_LOCK` 串行。
4. **订阅 ≠ 取消**:`sse-live` 观察者 `ctx.Done()` 默认**只结束订阅**,不 `runtime.cancel`。仅显式 header `X-Marqdo-Cancel-On-Disconnect: 1` 才 disconnect=cancel。同步 `waitIsolatedJSON` 可保留 disconnect 取消。重连:`GET /_runtime/tasks/{id}/events` + `Last-Event-ID` / `after_seq`。
2. **HTTP 默认**:`class=execute` 或 `return=sse-live` / body `stream:true` → `admit` → `dispatch`(`--call-lib-path` worker);**禁止**父进程 `CallLibPath` / `Setenv` / `legacySSEBridgeMu`。声明的 `short`/`read`/`control` 走父进程短路径(execute 已离进程,父锁通常空闲);未声明非流式 → `legacy_serial`。
3. **控制/读取分离**:`/_runtime/*`(cancel、events、task get)不进入解释器锁。短读勿挤占 execute worker 槽(池默认 2);产品 cancel 优先 `/_runtime`。
4. **订阅 ≠ 取消**:`sse-live` 观察者 `ctx.Done()` 默认**只结束订阅**,不 `runtime.cancel`;后台仍 reap worker 以释放槽。仅显式 header `X-Marqdo-Cancel-On-Disconnect: 1` 才 disconnect=cancel。同步 `waitIsolatedJSON` 可保留 disconnect 取消。重连:`GET /_runtime/tasks/{id}/events` + `Last-Event-ID` / `after_seq`。
5. **协议**:`InvokeRequestV2` / worker env + NDJSON `MARQDO_EVENT_FORWARD` + `--emit-result`(首期);`/_runtime/*` 控制面。
6. **能力探测**:`runtime.isolated_execution=1`、`runtime.task_control_v2`、`runtime.event_replay`、`view.analysis_cache_v2`、`runtime.read_lane=1`。
6. **能力探测**:`runtime.isolated_execution=1`、`runtime.task_control_v2`、`runtime.event_replay`、`view.analysis_cache_v2`、`runtime.read_lane=1`(短读不与 execute 抢父锁)。
7. **发布**:`expected_generation` CAS + publish journal;逻辑文档 ID 与物理 generation 路径解耦。
## 后果
- Execute / live SSE 不再抢进程级 SSE 桥;双流可并行(MR-01)。
- 声明的 short/read/control 不再与长流抢父进程 `INVOKE_LOCK`;产品 cancel 应优先 `/_runtime/tasks/{id}/cancel`。
- 短读走父进程:在 dual execute 下通常仍 200(父锁空闲);勿把 short/read/control 塞进 2 槽 execute 池。
- Legacy 未声明非流式路径仍可走父进程锁(ADR 0008)。
- 产品(qdagent)`turn_events` / Last-Event-ID / cancel CAS 接线由 qdagent 验收;框架侧 disconnect≠cancel 与无锁控制面已落地。
- 产品(qdagent)`turn_events` / Last-Event-ID / cancel CAS 接线由 qdagent 验收;框架侧 disconnect≠cancel 与 `/_runtime` 无锁控制面已落地。
## 验收(摘要)
+44 -12
View File
@@ -171,12 +171,17 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
class := resolveInvokeClass(route, liveSSE)
switch class {
case "execute", "short", "read", "control":
case "execute":
// Isolated workers: do not take parent INVOKE_LOCK (ADR 0009).
// control/read/short use separate admission slots; cancel/events also
// available lock-free via /_runtime/*.
st.handleInvokeIsolated(w, r, route, args, liveSSE, class)
return
case "short", "read", "control":
// Stay on parent: execute already runs off-process, so parent lock is free.
// Putting short/read/control on the 2-slot execute worker pool caused
// systemic no_slot (and zombies held slots after observer disconnect).
// Product cancel/events prefer /_runtime/* (lock-free).
st.handleInvokeParent(w, r, route, args)
return
default:
// legacy_serial: parent CallLibPath (ADR 0008). Never isolate undeclared non-stream.
st.handleInvokeParent(w, r, route, args)
@@ -453,6 +458,9 @@ func (st *state) streamIsolatedSSE(w http.ResponseWriter, r *http.Request, route
"resume": "/_runtime/tasks/" + taskID + "/events",
},
})
// Observer gone ≠ cancel, but still reap when the worker exits so
// execute slots are not held forever by zombies.
go reapIsolatedTask(taskID, emitResult, 10*time.Minute)
}
finish(true)
return
@@ -524,23 +532,47 @@ func (st *state) waitIsolatedJSON(w http.ResponseWriter, r *http.Request, route
}
}
func workerDone(taskID, emitResult string) bool {
if emitResult != "" {
if st, err := os.Stat(emitResult); err == nil && st.Size() > 0 {
return true
// reapIsolatedTask polls runtime get until the worker exits (or timeout),
// so supervisor/scheduler slots are released after observer disconnect.
func reapIsolatedTask(taskID, emitResult string, budget time.Duration) {
deadline := time.Now().Add(budget)
for time.Now().Before(deadline) {
if workerDone(taskID, emitResult) {
return
}
time.Sleep(200 * time.Millisecond)
}
// Sibling of event file written by Supervisor even if emit_result was omitted.
if taskID != "" {
// Last-chance cancel so slots can free.
_, _ = runtimeQuery("cancel", map[string]any{"task_id": taskID})
for i := 0; i < 25; i++ {
if workerDone(taskID, emitResult) {
return
}
time.Sleep(200 * time.Millisecond)
}
}
func workerDone(taskID, emitResult string) bool {
// Always runtime.get when probing completion: get runs try_reap and
// releases execute slots. Do not return early on emit_result alone —
// that left zombie workers holding both pool slots (no_slot).
if taskID == "" {
return false
}
g, err := runtimeQuery("get", map[string]any{"task_id": taskID})
if err != nil {
// Fallback: result file present (get may fail during early boot).
if emitResult != "" {
if st, err := os.Stat(emitResult); err == nil && st.Size() > 0 {
return true
}
}
if dir := os.Getenv("MARQDO_RUNTIME_DIR"); dir != "" {
p := filepath.Join(dir, "events", taskID+".result.json")
if st, err := os.Stat(p); err == nil && st.Size() > 0 {
return true
}
}
}
g, err := runtimeQuery("get", map[string]any{"task_id": taskID})
if err != nil {
return false
}
if _, ok := g["exit_code"]; ok {