Skip to content

fix(server): workspace 创建 receipt 与当前容器不匹配导致 Space 永久死锁 - #30

Merged
Sunwuyuan merged 1 commit into
mainfrom
fix/workspace-receipt-rebind
Nov 25, 2026
Merged

Sunwuyuan merged 1 commit into
mainfrom
fix/workspace-receipt-rebind

Conversation

@Sunwuyuan

Copy link
Copy Markdown
Member

现象

自建控制面上某个 Space 永久卡在:

workspace_status = error
last_error       = workspace creation receipt does not match the current container

之后无论怎么点启动,都返回同一句错误,空间再也起不来。

现场数据:

-- 只有一个 create operation,且 digest 已写,永不清理
workspace_create_operations: 28f756b7-… | created 2026-11-18T12:53:52Z

-- managed_containers 里还有一条 running 的缓存行,但容器早就不在了
6800e7c4-… | 444797c6… | running

$ docker inspect 444797c6…
error: no such object: 444797c6…

根因

workspace_create_operations 是持久意图,只插入、从不删除;reconcileWorkspaceCreate 用 DockerID 做硬比较,于是下面两种真实场景都会永久失败:

  1. 容器被重建:docker.recreate(节点镜像更新)会重建 v1 工作区容器。specFromInspectJSON 保留了全部标签(zakura.create-operation、zakura.create-digest 等),但 DockerID 变了 → receipt 与观测到的容器 ID 永远不同。
  2. 容器被带外删除:receipt 仍是 committed,而 docker.list 为空 → op.matches 因 DockerID == "" 失败 → 同样永久报错。

由于没有任何路径能清掉这条 operation,Space 只能一直 error。

修复

A. 容器身份用标签证明,而不是 DockerID

if !op.matches(obs.Container, receipt.ConfigDigest) {
    return errors.New("workspace creation receipt does not match the current container")
}

op.matches 已经校验名字 + 全部 operation 标签 + create-operation + create-digest;而 observeSpaceWorkspace 保证同一 space/instance 最多一个容器,所以 ID 变化只可能是同一个逻辑容器被重建,重绑是安全的(不会放过真正的重复创建)。

B. 确认容器真的不存在时,清除陈旧意图

观测为空时用 docker.inspect 精确检查 receipt 里的容器 ID:

  • 只有 Docker 明确返回 no such object 才判定"效果已不存在",在同一把 fence 下删除该 operation,返回 errWorkspaceCreateVoided;
  • 启动路径据此新建 operation(op = nil 后走原有 create 流程);
  • 恢复路径不重建容器,只把 Space 结算为 stopped;
  • 传输失败 / 超时 / 其它错误保持原语义(保留意图,交给人处理)。

验证

单测新增两个场景(recreated、missing-container),并做了反向验证:

# 旧代码
--- FAIL: .../recreated
    recovery recreated: recover workspace id-000001: workspace creation receipt does not match the current container

# 新代码
ok  github.com/Moonrend/Zakura/apps/server/internal/runtime  91.6s
ok  github.com/Moonrend/Zakura/apps/server/internal/workspaces 0.7s

生产环境热修后,同一 Space:

before: error   | workspace creation receipt does not match the current container
after : stopped | (none)     # op 行已清理,lease 已释放

备注

  • 该修复已在自建控制面以热修镜像验证过,可随时回滚。
  • 迁移路径(workspace_migration_orchestrator)对 errWorkspaceCreateVoided 仍按失败处理:迁移中容器消失属于真实故障,不应静默重建。

receipt 只证明过去发生过一次创建,不证明容器现在还活着。以下两种真实场景
都会让 reconcileWorkspaceCreate 永久失败,且 workspace_create_operations
从不清理,Space 只能一直停在 error:

1. docker.recreate(节点镜像更新)会重建 v1 工作区容器,保留名字、标签和
   create-digest,但 DockerID 变了;旧逻辑用 DockerID 硬比较 -> 永久不匹配。
2. 容器被带外删除后,receipt 仍是 committed,而 docker.list 观测为空 ->
   同样永久不匹配。

修复:
- 观测到的容器只要满足 op.matches(名字 + 全部标签 + digest)就视为同一逻辑
  效果并重绑,不再比较 DockerID;observeSpaceWorkspace 已保证同一 space/
  instance 只有一个容器,因此不会放过真正的重复创建。
- 观测为空时用 docker.inspect 精确确认 receipt 容器是否真的不存在;仅当
  Docker 明确返回 no such object 时,在同一把 fence 下清除陈旧 operation,
  返回 errWorkspaceCreateVoided。启动路径会据此创建新的 operation;恢复路径
  只会把 Space 结算为 stopped,绝不重建容器。
- 传输失败、超时或其它 inspect 错误仍保持原语义(保留 intent,交给人处理)。

测试覆盖 recreated / missing-container 两个场景,旧代码可精确复现
"workspace creation receipt does not match the current container"。
Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Sunwuyuan
Sunwuyuan merged commit 5b717eb into main Nov 25, 2026
2 of 6 checks passed
@Sunwuyuan
Sunwuyuan deleted the fix/workspace-receipt-rebind branch November 25, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants