hosted: release our own state lock on every root a killed block touched
A block killed while tofu planned (a destroy's plan phase) left its own state lock, and the retry failed on it. Now TF-START is journalled before tofu init, and at a deadline, with the group proved gone, every root the block touched has its lock object read directly; a lock this block's killed tofu provably left (this user@host, created in the block's window) is force-unlocked, any other is a stop. Only roots the block was applying are reconciled. All inside the same 15-minute context.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+230
−35
.rastrillo/budgets.txt+1 −1docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+35 −0internal/hostedops/fake_test.go+8 −0internal/hostedops/kit34r3_test.go+1 −1internal/hostedops/kit34r4_test.go+16 −5internal/hostedops/kit34r5_test.go+75 −0internal/hostedops/ops_test.go+10 −2internal/hostedops/stop.go+76 −19internal/hostedops/tf.go+8 −7
CI failed — run details
CI log
=== prepare ===
ci-box: machine: aarch64, 32 CPUs, 62 GiB memory, kernel 7.0.0-1017-aws
ci-box: python 3.12.3; Docker version 29.1.3, build 29.1.3-0ubuntu3~24.04.2
ci-box: user namespaces allowed: 233151
ci-box: cgroup: cgroup2fs
ci-box: disk /home/ci/work/repos/oficina__equipo/wt/hosted-runners-2: Type Size Avail | ext4 96G 84G
ci-box: disk /home/ci/.cache/equipo-ci-box: Type Size Avail | ext4 96G 84G
ci-box: bwrap: fetched and verified in 0.3s
ci-box: image pulled in 9.9s
ci-box: claude: fetched and verified in 14.3s
ci-box: codex: fetched and verified in 3.5s
ci-box: codex-code-mode-host: fetched and verified in 1.4s
ci-box: libwrap: fetched and verified in 0.3s
ci-box: socat: fetched and verified in 0.3s
ci-box: tofu: fetched and verified in 1.6s
npm warn deprecated node-domexception@1.0.0: Use your platform's native DOMException instead
ci-box: pi: installed and verified in 7.2s
ci-box: opencode: installed and verified in 2.3s
ci-box: chromium 156.0.8078.79-1~deb13u1: 76 packages fetched, verified and unpacked in 8.8s
ci-box: clone deepened in 6.4s
ci-box: file times set from content: 4068 files, 303 directories
ci-box: ready in 30.6s
=== copy ===
EQUIPO_RELEASE_COPY_CHECK=1 CGO_ENABLED=0 go test ./internal/equipotest -run '^TestReleaseCopyReviewed$' -count=1
--- FAIL: TestReleaseCopyReviewed (0.09s)
task_pages_test.go:236: COPY REVIEW REQUIRED: copy-status.py verify found copy pending review or stale bookkeeping
FAIL
rastrillo-budget/v1 ran 89ms
FAIL amadan.net/oficina/equipo/internal/equipotest 0.243s
FAIL
make: *** [Makefile:324: copy-release-check] Error 1
amadan: step copy failed: exit status 2
amadan: job failed at step copy — its output is above, not at the tail of this log
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 661d4a3c..34eba813 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -88,4 +88,4 @@ perf GET /native/preview/exchange skip Rejects GET with 404; only a same-origin, |
| # and the same bounded cardView as the snapshot; no list scan or new route. |
| perf GET /api/v1/tasks/{id}/changes/diff skip Device-only bounded display patch JSON: indexed step/attempt lookup, 64 KiB patch and 128 KiB response, no Git or network work at read time; not an HTML screen. |
| size internal/hostedctl 6079 10017 Hosted controller core and its lifecycle simulation; split planned for slice 1b; slice 2: egress snapshot, admission, deadlines, healthz; measured 6079/10017. |
| -size internal/hostedops 8304 - Hosted slice 2 Task 33: equipo-hosted-ops, one journal and one Ops shared by every run kit subcommand, so one package; dev drill run-kit fixes 2 and 5 to 7 (fence record, reboot waits, scan retry, S.1 argv); Task 34 kit K1 to K8 (teardown blocks, exit classes, tag cross-check, VM accounting and step settle, unit status and retries, finding); round 2 K9 to K11 (recovery-to-teardown continuation, settle --ended-at, resumable bucket teardown); after cap K12 and K13 (block deadlines with process groups, reboot --not-after); scoped review K14 to K17 (synchronous group stop, own stale lock release, safety kept, touched roots); measured 8304. |
| +size internal/hostedops 8362 - Hosted slice 2 Task 33: equipo-hosted-ops, one journal and one Ops shared by every run kit subcommand, so one package; dev drill run-kit fixes 2 and 5 to 7 (fence record, reboot waits, scan retry, S.1 argv); Task 34 kit K1 to K8 (teardown blocks, exit classes, tag cross-check, VM accounting and step settle, unit status and retries, finding); round 2 K9 to K11 (recovery-to-teardown continuation, settle --ended-at, resumable bucket teardown); after cap K12 and K13 (block deadlines with process groups, reboot --not-after); scoped review K14 to K17 (synchronous group stop, own stale lock release, safety kept, touched roots; own locks released after a killed plan); measured 8362. |
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md| index f06392ba..3dc1bf08 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -4046,3 +4046,38 @@ Mutation checks, each restored and green again (`go test ./internal/hostedops -c |
| - **K17a** every TF-START counted, not only this block's (the first form, an unused variable, was a |
| build error and not counted): `TestDeadlineReconcilesOnlyTouchedRoots` FAILED `:247 reconciled the wrong roots`. |
| - **K17b** `ops tf` not journalling TF-START: FAILED `:260 1 TF-START lines for network, want 2`. |
| + |
| +### Task 34 kit, K15 extended: locks from a killed plan (coordinator, 2026-12-02) |
| + |
| +No AWS call. Tests kill only the PIDs their commands recorded. |
| + |
| +- `ops tf` and `tf reconcile` journal `TF-START` before `tofu init`, so a root counts as touched from |
| + its first tofu command (init, plan, show, apply, destroy). |
| +- At a block's deadline, with the group proved gone, every touched root has its state lock read |
| + directly: the S3 lockfile beside the state (`<env>/<root>.tfstate.tflock` in the state bucket, |
| + `GetObject` under the run's guard). None: nothing. One this block's killed tofu provably left (Who |
| + this user@host, Created between the block's start and its deadline): `tofu force-unlock -force ID`, |
| + `FORCE-UNLOCK`. Any other lock, or one that does not read as a lock: 99. A read that fails: a |
| + FINDING. Then only the roots this block was applying (a `TF-APPLY start` since its start, no |
| + `done`) are reconciled; the reconcile keeps its own lock handling. All of it runs inside the same |
| + 15-minute context, so the deadline's worst case is unchanged. |
| + |
| +Tests: `kit34r5_test.go` (`TestDeadlineInPlanReleasesOwnLock`: a real `sh` runs the fake tofu, whose |
| +plan takes our lock and stalls; the block is killed in plan, the lock released, no reconcile, and |
| +`ops tf network destroy-network` then succeeds; `TestDeadlineInPlanKeepsAnotherLock`: another Who, 99, |
| +no force-unlock; `TestTouchedFromInit`). The fake tofu: `stall-plan` writes `lock.json` and the lock |
| +text and stalls with a recorded `sleep`; any plan is refused while `lock` exists; `fail-init`; the |
| +fake cloud reads `*.tflock` from `lock.json`. The K12 and K14 to K17 tests now journal `TF-APPLY start` |
| +where they mean a root applying. Red first: `kit34r5_test.go:40 a root that was only planning was |
| +reconciled`, `:73 0 TF-START lines with init failing, want 2`. |
| + |
| +Mutation checks, each restored and green again: |
| +- **K15f** the touched roots' locks not read: `TestDeadlineInPlanReleasesOwnLock` FAILED `:37 our own |
| + lock from a killed plan was not released`. |
| +- **K15g** any Who released from the lock object: `TestDeadlineInPlanKeepsAnotherLock` FAILED `:50 ... = 1, want 99`. |
| +- **K15h** another's lock made ordinary: FAILED `:50 ... = 1, want 99`. |
| +- **K17c** every touched root reconciled: `TestDeadlineInPlanReleasesOwnLock` FAILED `:40 a root that |
| + was only planning was reconciled`. |
| +- **K17d** TF-START after init: `TestTouchedFromInit` FAILED `:73 1 TF-START lines with init failing, want 2`. |
| +- **K17e** applying never recognised: `TestBlockDeadlineKillsAndReconciles` FAILED `kit34r3_test.go:59 no |
| + reconcile after the deadline`. |
diff --git a/internal/hostedops/fake_test.go b/internal/hostedops/fake_test.go| index 8685c59c..b7117667 100644 |
| --- a/internal/hostedops/fake_test.go |
| +++ b/internal/hostedops/fake_test.go |
| @@ -284,6 +284,14 @@ func (f *fakeCloud) HeadObject(_ context.Context, b, o, k string) (string, error |
| return base64.StdEncoding.EncodeToString(s[:]), nil |
| } |
| func (f *fakeCloud) GetObject(_ context.Context, b, o, k string) ([]byte, error) { |
| + if strings.HasSuffix(k, ".tflock") { // the state lock object: the fake tofu's lock.json, if any |
| + f.rec("GetObject %s/%s", b, k) |
| + raw, err := os.ReadFile(filepath.Join(os.Getenv("FAKE_TOFU"), "lock.json")) |
| + if err != nil { |
| + return nil, ErrNotFound |
| + } |
| + return raw, nil |
| + } |
| f.mu.Lock() |
| defer f.mu.Unlock() |
| body, ok := f.objects[b+"/"+k] |
diff --git a/internal/hostedops/kit34r3_test.go b/internal/hostedops/kit34r3_test.go| index 76a23722..2864d660 100644 |
| --- a/internal/hostedops/kit34r3_test.go |
| +++ b/internal/hostedops/kit34r3_test.go |
| @@ -42,7 +42,7 @@ func TestBlockDeadlineKillsAndReconciles(t *testing.T) { |
| {"address":"module.network.aws_vpc.vm","type":"aws_vpc","values":{"id":"vpc-0vm"}}, |
| {"address":"module.network.aws_iam_role.connector_operator","type":"aws_iam_role","values":{"id":"equipo-hosted-connector-operator","arn":"arn:aws:iam::149757027610:role/equipo-hosted-connector-operator","name":"equipo-hosted-connector-operator"}}]}]}}}`), 0o600) |
| start := time.Now() |
| - touch := `printf '2026-12-01T10:00:00.000Z\tTF-START network destroy-network\n' >> ` + filepath.Join(r.dir, JournalFile) + `; ` |
| + touch := `printf '2026-12-01T10:00:00.000Z\tTF-START network destroy-network\n2026-12-01T10:00:00.000Z\tTF-APPLY network start destroy-network\n' >> ` + filepath.Join(r.dir, JournalFile) + `; ` |
| r.want(ExitFail, "block", "8.3", "--teardown", "--max", "500ms", "--", "sh", "-c", touch+`"$FAKE_TOFU/tofu" -chdir=x apply x.tfplan`) |
| if d := time.Since(start); d > 30*time.Second { |
| t.Fatalf("the block ran %v past a 2s deadline", d) |
diff --git a/internal/hostedops/kit34r4_test.go b/internal/hostedops/kit34r4_test.go| index d2910648..edce6b3e 100644 |
| --- a/internal/hostedops/kit34r4_test.go |
| +++ b/internal/hostedops/kit34r4_test.go |
| @@ -138,7 +138,7 @@ func TestDeadlineReleasesOnlyOurStaleLock(t *testing.T) { |
| r, fake := tfRig(t) |
| lock := filepath.Join(fake, "lock") |
| r.deps.Exec = func(ctx ctxT, _ []string, _ []string, _, _ wT) int { |
| - r.journal("TF-START network destroy-network") |
| + r.journal("TF-START network destroy-network", "TF-APPLY network start destroy-network") |
| os.WriteFile(lock, []byte(lockText("lock-0abc", c.who, c.at(time.Now()))), 0o600) |
| <-ctx.Done() |
| return 143 |
| @@ -181,7 +181,7 @@ func TestDeadlineKeepsSafetyRefusals(t *testing.T) { |
| touch := func(r *rig, extra ...string) { |
| r.deps.Exec = func(ctx ctxT, _ []string, _ []string, _, _ wT) int { |
| - r.journal(append([]string{"TF-START network destroy-network", "TF-START services destroy-services"}, extra...)...) |
| + r.journal(append([]string{"TF-START network destroy-network", "TF-APPLY network start destroy-network", "TF-START services destroy-services", "TF-APPLY services start destroy-services"}, extra...)...) |
| <-ctx.Done() |
| return 143 |
| } |
| @@ -205,9 +205,20 @@ func TestDeadlineKeepsSafetyRefusals(t *testing.T) { |
| if got := r.ops("block", "8.3", "--teardown", "--max", "50ms", "--", "x"); got != ExitStop { |
| t.Errorf("%s: block = %d, want 99:\n%s", name, got, r.out.String()) |
| } |
| - if strings.Contains(calls(t, fake), "envs/prod/services") { |
| - t.Errorf("%s: the next root was reconciled after a safety error", name) |
| + // Between the DEADLINE and the block's ABORT no other root is |
| + // touched (the recovery after it may reconcile roots left applying). |
| + in := false |
| + for _, e := range r.ledger().Entries { |
| + switch { |
| + case e.Verb == "DEADLINE": |
| + in = true |
| + case e.Verb == "ABORT" && strings.HasPrefix(e.Text, "block 8.3"): |
| + in = false |
| + case in && e.Verb == "TF-START" && e.Field(0) == "services": |
| + t.Errorf("%s: the next root was reconciled after a safety error", name) |
| + } |
| } |
| + _ = fake |
| } |
| // The ledger not reading is a stop. |
| r, _ = tfRig(t) |
| @@ -237,7 +248,7 @@ func TestDeadlineReconcilesOnlyTouchedRoots(t *testing.T) { |
| r, fake := tfRig(t, "TF-APPLY services start destroy-services", "TF-START services destroy-services") |
| withServices(r) |
| r.deps.Exec = func(ctx ctxT, _ []string, _ []string, _, _ wT) int { |
| - r.journal("TF-START network destroy-network") |
| + r.journal("TF-START network destroy-network", "TF-APPLY network start destroy-network") |
| <-ctx.Done() |
| return 143 |
| } |
diff --git a/internal/hostedops/kit34r5_test.go b/internal/hostedops/kit34r5_test.go| new file mode 100644 |
| index 00000000..ac63f85f |
| --- /dev/null |
| +++ b/internal/hostedops/kit34r5_test.go |
| @@ -0,0 +1,75 @@ |
| +//go:build unix |
| + |
| +package hostedops |
| + |
| +import ( |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// K15 extended (the coordinator, after the scoped review): a block |
| +// killed while tofu plans (a destroy's plan phase) leaves its own state |
| +// lock; at the deadline every root the block touched has its lock read, |
| +// and one this block provably left is released, though the root was not |
| +// applying; only roots the block was applying are reconciled. |
| + |
| +// planKillRig runs a block whose command journals TF-START for network |
| +// and runs the fake tofu's plan, which takes a lock for who and stalls. |
| +func planKillRig(t *testing.T, who string) (*rig, string, []string) { |
| + r, fake := tfRig(t) |
| + t.Setenv("FAKE_WHO", who) |
| + os.WriteFile(filepath.Join(fake, "stall-plan"), nil, 0o600) |
| + recordedPIDs(t, filepath.Join(fake, "sleep.pid")) |
| + r.deps.groupGrace, r.deps.groupKillWait = 300*1e6, 5*1e9 |
| + dir := filepath.Join(r.dir, "repo", "infra", "hosted", "envs", "prod", "network") |
| + cmd := `printf '2026-12-01T10:00:00.000Z\tTF-START network destroy-network\n' >> ` + filepath.Join(r.dir, JournalFile) + |
| + `; "$FAKE_TOFU/tofu" -chdir=` + dir + ` plan -destroy -out=` + filepath.Join(fake, "x.tfplan") |
| + return r, fake, []string{"block", "8.3b", "--teardown", "--max", "500ms", "--", "sh", "-c", cmd} |
| +} |
| + |
| +func TestDeadlineInPlanReleasesOwnLock(t *testing.T) { |
| + r, fake, block := planKillRig(t, lockWho()) |
| + r.want(ExitFail, block...) |
| + log := calls(t, fake) |
| + if !r.has("FORCE-UNLOCK", "network id=lock-0plan who="+lockWho()) || !strings.Contains(log, "force-unlock -force lock-0plan") { |
| + t.Fatalf("our own lock from a killed plan was not released:\n%s\n%s", log, journalText(r)) |
| + } |
| + if strings.Contains(log, "-refresh-only") { |
| + t.Fatal("a root that was only planning was reconciled") |
| + } |
| + if r.cloud.index("GetObject equipo-hosted-tfstate-149757027610/prod/network.tfstate.tflock") < 0 { |
| + t.Fatalf("the lock object was not read: %v", r.cloud.log()) |
| + } |
| + r.want(0, "tf", "network", "destroy-network") // the retry |
| +} |
| + |
| +func TestDeadlineInPlanKeepsAnotherLock(t *testing.T) { |
| + r, fake, block := planKillRig(t, "someone@elsewhere") |
| + r.want(ExitStop, block...) |
| + if strings.Contains(calls(t, fake), "force-unlock") || r.has("FORCE-UNLOCK", "") { |
| + t.Fatal("another Who's lock was released") |
| + } |
| + if !r.has("ABORT", "not released") { |
| + t.Fatalf("no record of the lock kept:\n%s", journalText(r)) |
| + } |
| +} |
| + |
| +// A root counts as touched from the first tofu command: TF-START comes |
| +// before init, so an init that fails still marks it. |
| +func TestTouchedFromInit(t *testing.T) { |
| + r, fake := tfRig(t) |
| + os.WriteFile(filepath.Join(fake, "fail-init"), nil, 0o600) |
| + r.want(ExitFail, "tf", "network", "destroy-network") |
| + r.want(ExitFail, "tf", "reconcile", "network") |
| + n := 0 |
| + for _, e := range r.ledger().Entries { |
| + if e.Verb == "TF-START" && e.Field(0) == "network" { |
| + n++ |
| + } |
| + } |
| + if n != 2 { |
| + t.Fatalf("%d TF-START lines with init failing, want 2:\n%s", n, journalText(r)) |
| + } |
| +} |
diff --git a/internal/hostedops/ops_test.go b/internal/hostedops/ops_test.go| index 06bb414f..0d1e41bc 100644 |
| --- a/internal/hostedops/ops_test.go |
| +++ b/internal/hostedops/ops_test.go |
| @@ -211,8 +211,16 @@ case " $* " in |
| *" output -json "*) echo '{"vm_vpc_id":{"value":"vpc-0vm"}}';; |
| esac |
| case " $* " in |
| - *" force-unlock -force "*) rm -f "$FAKE_TOFU/lock";; |
| - *" -refresh-only "*) if [ -f "$FAKE_TOFU/lock" ]; then cat "$FAKE_TOFU/lock" >&2; exit 1; fi; [ -f "$FAKE_TOFU/fail-refresh" ] && exit 1;; |
| + *" init "*) [ -f "$FAKE_TOFU/fail-init" ] && exit 1;; |
| + *" force-unlock -force "*) rm -f "$FAKE_TOFU/lock" "$FAKE_TOFU/lock.json";; |
| + *" plan "*) if [ -f "$FAKE_TOFU/lock" ]; then cat "$FAKE_TOFU/lock" >&2; exit 1; fi |
| + case " $* " in *" -refresh-only "*) [ -f "$FAKE_TOFU/fail-refresh" ] && exit 1;; esac |
| + if [ -f "$FAKE_TOFU/stall-plan" ]; then |
| + printf '{"ID":"lock-0plan","Operation":"OperationTypePlan","Info":"","Who":"%s","Version":"1.12.5","Created":"%s","Path":"x"}' "$FAKE_WHO" "$(date -u +%Y-%m-%dT%H:%M:%S.%NZ)" > "$FAKE_TOFU/lock.json" |
| + printf 'Error: Error acquiring the state lock\nLock Info:\n ID: lock-0plan\n Operation: OperationTypePlan\n Who: %s\n Created: %s\n' "$FAKE_WHO" "$(date -u '+%Y-%m-%d %H:%M:%S.%N +0000 UTC')" > "$FAKE_TOFU/lock" |
| + rm -f "$FAKE_TOFU/stall-plan" |
| + sleep 120 >/dev/null 2>&1 & echo $! > "$FAKE_TOFU/sleep.pid"; wait |
| + fi;; |
| esac |
| case " $* " in *" apply "*) if [ -f "$FAKE_TOFU/stall-apply" ]; then sleep 120 >/dev/null 2>&1 & echo $! > "$FAKE_TOFU/sleep.pid"; wait; fi;; esac |
| case " $* " in *" apply "*) [ -f "$FAKE_TOFU/fail-apply" ] && exit 1;; esac |
diff --git a/internal/hostedops/stop.go b/internal/hostedops/stop.go| index 1253fc33..f09fd24b 100644 |
| --- a/internal/hostedops/stop.go |
| +++ b/internal/hostedops/stop.go |
| @@ -2,6 +2,7 @@ package hostedops |
| import ( |
| "context" |
| + "encoding/json" |
| "errors" |
| "flag" |
| "io" |
| @@ -194,7 +195,20 @@ func (o *Ops) blockDeadline(id string, maxD time.Duration, code int, end groupEn |
| if err != nil { |
| return stopf("after its deadline the ledger does not read: %v", err) |
| } |
| - for _, root := range touchedRoots(l, id) { |
| + touched, applying := touchedRoots(l, id) |
| + for _, root := range touched { // a lock this block's killed tofu left, at any phase |
| + err := o.releaseOwnLock(root, began, deadline) |
| + switch { |
| + case err == nil: |
| + case IsStop(err): |
| + return stopf("after its deadline, the state lock of %s: %v", root, err) |
| + default: |
| + if ferr := o.finding("block %s: after its deadline, the state lock of %s: %v", id, root, err); ferr != nil { |
| + return ferr |
| + } |
| + } |
| + } |
| + for _, root := range applying { |
| err := o.reconcileAfterKill(root, began, deadline) |
| switch { |
| case err == nil: |
| @@ -216,28 +230,80 @@ func yesNo(b bool) string { |
| return "no" |
| } |
| -// touchedRoots are the roots with a TF-START line since block id's last |
| -// BLOCK start line, in roots order (K17). |
| -func touchedRoots(l *Ledger, id string) []string { |
| +// touchedRoots are, since block id's last BLOCK start line and in roots |
| +// order, the roots with a TF-START line (any tofu work, init on), and of |
| +// those the ones this block was applying: a TF-APPLY start with no later |
| +// TF-APPLY done (K15, K17). |
| +func touchedRoots(l *Ledger, id string) (touched, applying []string) { |
| start := 0 |
| for _, e := range l.Entries { |
| if e.Verb == "BLOCK" && e.Field(0) == id && e.Field(1) == "start" { |
| start = e.Line |
| } |
| } |
| - seen := map[string]bool{} |
| + seen, apply := map[string]bool{}, map[string]bool{} |
| for _, e := range l.Entries { |
| - if e.Line > start && e.Verb == "TF-START" { |
| + switch { |
| + case e.Line <= start: |
| + case e.Verb == "TF-START": |
| seen[e.Field(0)] = true |
| + case e.Verb == "TF-APPLY": |
| + apply[e.Field(0)] = e.Field(1) == "start" |
| } |
| } |
| - var out []string |
| for _, r := range roots { |
| if seen[r] { |
| - out = append(out, r) |
| + touched = append(touched, r) |
| } |
| + if seen[r] && apply[r] { |
| + applying = append(applying, r) |
| + } |
| + } |
| + return touched, applying |
| +} |
| + |
| +// releaseOwnLock reads root's state lock object (the S3 lockfile beside |
| +// the state, KEY.tflock) directly: none, nothing to do; one, released only |
| +// when this block's killed tofu provably left it (forceUnlockOwn), else a |
| +// stop. A read that fails is ordinary. |
| +func (o *Ops) releaseOwnLock(root string, began, deadline time.Time) error { |
| + key := o.env() + "/" + root + ".tfstate.tflock" |
| + raw, err := o.cloud.GetObject(o.ctx, o.stateBucket(), o.Account, key) |
| + switch { |
| + case errors.Is(err, ErrNotFound): |
| + return nil |
| + case err != nil: |
| + return failf("the lock object %s did not read: %v", key, err) |
| + } |
| + var in struct { |
| + ID, Operation, Who string |
| + Created time.Time |
| + } |
| + if err := json.Unmarshal(raw, &in); err != nil || in.ID == "" || in.Who == "" || in.Created.IsZero() { |
| + _ = o.jw("ABORT", "the lock object %s does not read as a lock (%v)", key, err) |
| + return stopf("the lock object %s does not read as a lock (%v)", key, err) |
| } |
| - return out |
| + return o.forceUnlockOwn(root, LockInfo{ID: in.ID, Who: in.Who, Operation: in.Operation, Created: in.Created}, began, deadline) |
| +} |
| + |
| +// forceUnlockOwn releases lock in on root only when this block's killed |
| +// tofu provably left it: Who is this user@host and Created lies between |
| +// the block's start and its deadline (the caller has proved the group |
| +// gone). Anything else is a stop; locking is never bypassed. |
| +func (o *Ops) forceUnlockOwn(root string, in LockInfo, began, deadline time.Time) error { |
| + if in.Who != lockWho() || !in.Created.After(began) || !in.Created.Before(deadline) { |
| + _ = o.jw("ABORT", "state lock on %s not released: id=%s who=%s operation=%s created=%s (this block: %s, %s to %s)", root, in.ID, in.Who, in.Operation, |
| + in.Created.UTC().Format(time.RFC3339Nano), lockWho(), began.UTC().Format(time.RFC3339Nano), deadline.UTC().Format(time.RFC3339Nano)) |
| + return stopf("the state of %s is locked by %s since %s, not provably by this block's killed tofu; not released", root, in.Who, in.Created.UTC().Format(time.RFC3339)) |
| + } |
| + dir := o.rootDir(root) |
| + if err := o.tofuInit(root, dir); err != nil { |
| + return err |
| + } |
| + if _, err := o.tofu("-chdir="+dir, "force-unlock", "-force", in.ID); err != nil { |
| + return stopf("force-unlock of %s (%s) failed: %v", root, in.ID, err) |
| + } |
| + return o.jw("FORCE-UNLOCK", "%s id=%s who=%s operation=%s created=%s", root, in.ID, in.Who, in.Operation, in.Created.UTC().Format(time.RFC3339Nano)) |
| } |
| // reconcileAfterKill is tf reconcile ROOT after a block's group was |
| @@ -252,16 +318,7 @@ func (o *Ops) reconcileAfterKill(root string, began, deadline time.Time) error { |
| if !errors.As(err, &le) { |
| return err |
| } |
| - in := le.Info |
| - if in.Who != lockWho() || !in.Created.After(began) || !in.Created.Before(deadline) { |
| - _ = o.jw("ABORT", "state lock on %s not released: id=%s who=%s operation=%s created=%s (this block: %s, %s to %s)", root, in.ID, in.Who, in.Operation, |
| - in.Created.UTC().Format(time.RFC3339Nano), lockWho(), began.UTC().Format(time.RFC3339Nano), deadline.UTC().Format(time.RFC3339Nano)) |
| - return stopf("the state of %s is locked by %s since %s, not provably by this block's killed tofu; not released", root, in.Who, in.Created.UTC().Format(time.RFC3339)) |
| - } |
| - if _, err := o.tofu("-chdir="+o.rootDir(root), "force-unlock", "-force", in.ID); err != nil { |
| - return stopf("force-unlock of %s (%s) failed: %v", root, in.ID, err) |
| - } |
| - if err := o.jw("FORCE-UNLOCK", "%s id=%s who=%s operation=%s created=%s", root, in.ID, in.Who, in.Operation, in.Created.UTC().Format(time.RFC3339Nano)); err != nil { |
| + if err := o.forceUnlockOwn(root, le.Info, began, deadline); err != nil { |
| return err |
| } |
| err = o.tfReconcile(root) |
diff --git a/internal/hostedops/tf.go b/internal/hostedops/tf.go| index 74086b30..0bcf8a58 100644 |
| --- a/internal/hostedops/tf.go |
| +++ b/internal/hostedops/tf.go |
| @@ -260,12 +260,13 @@ func cmdTf(o *Ops, args []string) error { |
| return failf("no %s: run tfvars first", vars) |
| } |
| dir := o.rootDir(root) |
| - if err := o.tofuInit(root, dir); err != nil { |
| + // TF-START: this root's tofu work begins, from init on (a lock may be |
| + // taken). A block's deadline reads the lock of every root it touched |
| + // (K15) and reconciles those it was applying (K17). |
| + if err := o.jw("TF-START", "%s %s", root, phase); err != nil { |
| return err |
| } |
| - // TF-START: this root's tofu work begins (a lock may be taken); a |
| - // block's deadline reconciles the roots it touched (K17). |
| - if err := o.jw("TF-START", "%s %s", root, phase); err != nil { |
| + if err := o.tofuInit(root, dir); err != nil { |
| return err |
| } |
| if err := o.adoptPending(root, dir, vars); err != nil { |
| @@ -521,6 +522,9 @@ func (o *Ops) tfReconcile(root string) error { |
| if _, err := o.open(); err != nil { |
| return err |
| } |
| + if err := o.jw("TF-START", "%s reconcile", root); err != nil { |
| + return err |
| + } |
| dir := o.rootDir(root) |
| if err := o.tofuInit(root, dir); err != nil { |
| return err |
| @@ -530,9 +534,6 @@ func (o *Ops) tfReconcile(root string) error { |
| if err := os.MkdirAll(o.path("tf"), 0o700); err != nil { |
| return failf("%v", err) |
| } |
| - if err := o.jw("TF-START", "%s reconcile", root); err != nil { |
| - return err |
| - } |
| if _, err := o.tofu("-chdir="+dir, "plan", "-refresh-only", "-input=false", "-no-color", "-out="+planFile, "-var-file="+vars, "-var=run="+o.Run); err != nil { |
| var te *tofuError |
| if errors.As(err, &te) { |