Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/server/internal/runtime/computer_server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ func (h *handler) startSpaceWorkspaceWithLease(ctx context.Context, lease *works
}
if op != nil {
if err := h.reconcileWorkspaceCreate(ctx, lease, runner, op, obs); err != nil {
return lease.uncertain(err)
if !errors.Is(err, errWorkspaceCreateVoided) {
return lease.uncertain(err)
}
// The recorded create effect is gone; the Space has no workspace and
// the create path below starts a fresh operation.
op = nil
}
} else if obs.Container.DockerID == "" {
for _, row := range obs.Rows {
Expand Down
53 changes: 51 additions & 2 deletions apps/server/internal/runtime/computer_workspace_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ func (op *workspaceCreateOperation) matches(c computerContainer, digest string)
c.Labels["zakura.create-operation"] == op.OperationID && c.Labels["zakura.create-digest"] == digest
}

// errWorkspaceCreateVoided reports that a durable create intent was cleared
// because its recorded container no longer exists. Callers must treat the
// Space as having no workspace instead of retrying the dead operation.
var errWorkspaceCreateVoided = errors.New("workspace create effect no longer exists")

// workspaceContainerGone distinguishes a removed container from an
// unobservable one. Only an explicit Docker "no such object" is proof of
// absence; a transport failure must retain the intent.
func (h *handler) workspaceContainerGone(ctx context.Context, runner *runnerSession, id string) (bool, error) {
if id == "" {
return true, nil
}
var out computerContainer
if err := runner.call(ctx, "docker.inspect", map[string]any{"id": id}, &out); err != nil {
if strings.Contains(strings.ToLower(err.Error()), "no such object") {
return true, nil
}
return false, err
}
return false, nil
}

func (h *handler) reconcileWorkspaceCreate(ctx context.Context, lease *workspaceLease, runner *runnerSession, op *workspaceCreateOperation, obs workspaceObservation) error {
var receipt struct {
State, ConfigDigest, Name string
Expand All @@ -155,8 +177,35 @@ func (h *handler) reconcileWorkspaceCreate(ctx context.Context, lease *workspace
if op.ConfigDigest != nil && *op.ConfigDigest != receipt.ConfigDigest {
return errors.New("workspace creation receipt changed configuration")
}
// A receipt proves a past effect, not current liveness or absence.
if obs.Container.DockerID != receipt.Container.DockerID || !op.matches(obs.Container, receipt.ConfigDigest) {
if obs.Container.DockerID == "" {
gone, err := h.workspaceContainerGone(ctx, runner, receipt.Container.DockerID)
if err != nil {
return err
}
if !gone {
return errors.New("workspace creation receipt does not match the current container")
}
// The receipt describes an effect Docker no longer has (for example the
// container was removed out of band). Keeping the durable intent would
// deadlock the Space forever, because a receipt is never re-executed.
// Only an explicit "no such object" reaches this point, so the effect is
// provably absent and a new operation is safe.
if err := lease.fenced(ctx, func(tx *gorm.DB) error {
return tx.Table("workspace_create_operations").Where("space_id=? AND tenant_id=? AND node_id=? AND operation_id=?", op.SpaceID, op.TenantID, op.NodeID, op.OperationID).Delete(map[string]any{}).Error
}); err != nil {
return err
}
return errWorkspaceCreateVoided
}
// A receipt proves a past effect, not current liveness or absence. The
// Docker identity can change underneath it: docker.recreate (node image
// update) restarts running workspace containers with the same name, labels
// and create digest but a new DockerID, and a Docker restart may do the
// same. Observation already rejects multiple containers for one space or
// instance, so a container carrying this operation's complete identity is
// the same logical effect and the receipt must be rebound to it instead of
// deadlocking the Space forever on a stale ID.
if !op.matches(obs.Container, receipt.ConfigDigest) {
return errors.New("workspace creation receipt does not match the current container")
}
return lease.fenced(ctx, func(tx *gorm.DB) error {
Expand Down
33 changes: 29 additions & 4 deletions apps/server/internal/runtime/computer_workspace_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func workspaceReceiptStatusFixture(c computerContainer) map[string]any {
}

func TestWorkspaceCreateLostReplyUsesOriginalReceipt(t *testing.T) {
for _, scenario := range []string{"committed", "missing-receipt", "missing-container", "wrong-operation"} {
for _, scenario := range []string{"committed", "missing-receipt", "missing-container", "wrong-operation", "recreated"} {
t.Run(scenario, func(t *testing.T) {
d := testDeps(t)
seedTenant(t, d, "tenant")
Expand All @@ -53,6 +53,12 @@ func TestWorkspaceCreateLostReplyUsesOriginalReceipt(t *testing.T) {
if instance.DockerID == "" || scenario == "missing-container" {
return []any{}, nil
}
if scenario == "recreated" {
// docker.recreate keeps name/labels/digest but assigns a new ID.
recreated := instance
recreated.DockerID = "recreated-workspace"
return []computerContainer{recreated}, nil
}
return []computerContainer{instance}, nil
case "docker.images":
return []computerImageStatus{}, nil
Expand All @@ -79,6 +85,12 @@ func TestWorkspaceCreateLostReplyUsesOriginalReceipt(t *testing.T) {
c.Labels = map[string]string{}
}
return workspaceReceiptStatusFixture(c), nil
case "docker.inspect":
// Only a removed container is proof of absence.
if scenario == "missing-container" {
return nil, errors.New("Error response from daemon: No such object: " + fmt.Sprint(p["id"]))
}
return nil, errors.New("unexpected docker.inspect")
default:
return nil, fmt.Errorf("unexpected RPC %s", method)
}
Expand All @@ -91,7 +103,7 @@ func TestWorkspaceCreateLostReplyUsesOriginalReceipt(t *testing.T) {
t.Fatal(err)
}
err = h.recoverSpaceWorkspaces(ctx, 10)
if (err == nil) != (scenario == "committed") {
if (err == nil) != (scenario == "committed" || scenario == "recreated" || scenario == "missing-container") {
t.Fatalf("recovery %s: %v", scenario, err)
}
}
Expand All @@ -104,8 +116,21 @@ func TestWorkspaceCreateLostReplyUsesOriginalReceipt(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if scenario == "committed" && current.WorkspaceStatus != "running" {
t.Fatalf("status=%s", current.WorkspaceStatus)
wantStatus := map[string]string{"committed": "running", "recreated": "running", "missing-container": "stopped"}
if want, ok := wantStatus[scenario]; ok && current.WorkspaceStatus != want {
t.Fatalf("status=%s want=%s", current.WorkspaceStatus, want)
}
if scenario == "missing-container" {
// A confirmed-absent container must clear the durable intent so the
// Space is not deadlocked on a receipt that can never be replayed.
var remaining int64
if err := d.Gorm.Table("workspace_create_operations").Where("space_id=?", space.ID).Count(&remaining).Error; err != nil {
t.Fatal(err)
}
if remaining != 0 {
t.Fatalf("stale create operations=%d", remaining)
}
return
}
var op workspaceCreateOperation
if err := d.Gorm.Table("workspace_create_operations").Where("space_id=?", space.ID).Take(&op).Error; err != nil {
Expand Down
7 changes: 6 additions & 1 deletion apps/server/internal/runtime/computer_workspace_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ func (h *handler) recoverSpaceWorkspace(ctx context.Context, l *workspaceLease)
return l.uncertain(err)
}
if err = h.reconcileWorkspaceCreate(ctx, l, runner, op, obs); err != nil {
return l.uncertain(err)
if !errors.Is(err, errWorkspaceCreateVoided) {
return l.uncertain(err)
}
// The container is gone. Recovery never recreates a workspace, so
// settle the Space as stopped and let its owner start it again.
return l.finish(ctx, "stopped", map[string]any{"last_error": nil})
}
}
}
Expand Down
Loading