Skip to content

🤖 fix: refuse child creation under a workspace being removed across backends - #4908

Merged
ThomasK33 merged 3 commits into
mainfrom
fix/4782-removal-child-creation
Sep 27, 2026
Merged

ThomasK33 merged 3 commits into
mainfrom
fix/4782-removal-child-creation

Conversation

@ThomasK33

@ThomasK33 ThomasK33 commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR finishes the cross-process removal work tracked in #4782: the other backend can no longer create a child task under a workspace that is being removed (or was removed), a removal now checks for descendants after it claims its marker, and a removal marker with an empty identity field is dropped at load instead of blocking the workspace. Item 1 (a send into a live attempt) is already closed by the use leases and mutation gate that landed in #4861 and #4866; see below.

Fixes #4782

Background

#4776 added the durable pendingRemoval marker. The issue lists what it left open:

  1. A send into a live attempt is admitted by a config read. Backend B could pass the pendingRemoval read and start a turn while backend A commits the marker and deletes the checkout.
  2. Child creation does not check the parent's marker. removeUnlocked checked for descendants before its claim, and no task-creation config write looked at the parent's marker, so B could commit TaskService.create under a parent that A was removing and leave an orphaned child row (and possibly an unmanaged checkout).
  3. (comment) PendingRemovalSchema accepted empty identity strings. parseProcessIdentity() turns "" into null, and judgeHolder() on Linux then treats the holder as an unknown PID domain that never dies, so such a marker blocked admission and removal until someone edited config.json.
  4. (comment) Reactivation's marker check before it unarchives is a read, so a removal could race the unarchive's checkout restore.

Implementation

  • Item 2. A shared assertParentAdmitsChild runs inside every task-creation config write (commitReservations for createMany, and both the queued and the direct write in create). It refuses while the parent row carries pendingRemoval. create also refuses when the parent row it found by id during preparation is gone at write time (the direct path materializes a checkout in between, so a whole removal can fit in that gap). createMany does the same with the parent row it found by id when it revalidated the reservation (its checkpoint callback runs before the commit, so a whole removal can fit there too). A parent that was not found by id (an upgraded, id-less row that resolves only through its session metadata) is not required. removeUnlocked now checks for descendants after claimPendingRemoval instead of before the gate. Config writes are serialized, so either the child's write sees the marker and refuses, or the removal's post-claim read sees the child and refuses (its finally releases the marker, as for any other refusal after the claim). A refused direct create rolls its checkout back through the existing launch-failure path.
  • Item 3. The identity fields of PendingRemovalSchema are z.string().min(1).nullable(), so normalizePersistedWorkspace drops such a marker as malformed.
  • Item 1: no code change. removeUnlocked takes the structural mutation gate before it claims the marker and holds it until removal settles (🤖 feat: structural workspace mutations refuse while another backend uses the workspace #4866), and every turn holds a turn use lease from preparation and confirms it before the provider starts (🤖 feat: turns and terminals hold cross-process workspace use leases #4861). By the Dekker ordering in workspaceUseLeases.ts, either B's turn sees A's gate and fails as a settled preparation failure, or A's lease scan sees B's turn and the removal refuses before any effect. This also covers a turn B started before A's claim. Covered by agentSession.useLease.test.ts ("B's send while A's mutation runs fails as a settled preparation failure", "a turn in B holds its lease from preparation until it settles") and workspaceService.mutationGate.test.ts (other-backend leases refuse rename and remove before any effect).
  • Item 4: no code change. Since 🤖 fix: gate keep-mode unarchive and Coder stop/delete archives across backends #4904, a keep-mode unarchive holds an unarchive use lease from before its commit to the end, and a snapshot-restoring unarchive takes the mutation gate. Either one refuses while a removal holds the gate, and a removal refuses while the unarchive holds its lease.

Out of scope

The removal marker and this fence find the row by id. An upgraded workspace whose config row is still id-less gets no marker (as before this PR), so creation under it is not fenced; tracked in #4914.

Validation

New tests in workspaceService.pendingRemoval.test.ts (two backends on one root) fail on origin/main (25faf8f1c4):

(fail) workspace removal across two backends on one root > a malformed removal marker (an empty identity field) is dropped instead of blocking the task
(fail) workspace removal across two backends on one root > a removal refuses when the other backend created a child just before it closed admission
(fail) workspace removal across two backends on one root > the other backend cannot create a child task under a workspace being removed

Review round 1 added a test that a batch creation under a parent resolved only through metadata (legacy, no config row by id) still commits; it failed on the first PR head b6d58bcb49 (parent workspace legacy-parent was removed).

Review round 2 added a test that a batch creation refuses when the other backend finished removing the parent during the reservation checkpoint; it failed on 9ed0343a94 (Received: "created").

Before the fix, B's create under the parent being removed succeeded, A's removal deleted a parent whose child had been committed just before its claim, and the empty-identity marker refused the reawaken. With the fix all pass, and so do every taskService*/workspaceService* unit test file (1851 tests) and the 21 test files that mention pendingRemoval, createMany or descendant checks (935 tests).

Risks

Low to medium. create and createMany now refuse when the parent row they found by id is missing at write time; before, such a child was written as an orphan. A removal of a workspace with descendants now takes the gate and briefly claims its marker before it refuses (the marker is released in the same call), so another backend's task admission can see a short refusal during that window.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-27T21:14:01.134796Z 9631419 New commits
🔒 Security Review ✅ Completed 2026-09-27T21:15:33.228689Z 9631419 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b6d58bcb49

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/node/services/taskService.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9ed0343a94

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/node/services/taskService.ts Resolved
@ThomasK33
ThomasK33 force-pushed the fix/4782-removal-child-creation branch from 9ed0343 to 9631419 Compare September 27, 2026 21:10

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96314191c9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/node/services/taskService.ts Resolved
@ThomasK33

Copy link
Copy Markdown
Member Author

Readiness record

  • Final head: 96314191c9d326f358118c8da9a28fd2241d4846
  • Checks: all required CI checks pass on this head (the non-required Pixel / Review is pending). Local: make static-check, make fmt-sync-docs-check and 1853 taskService*/workspaceService* unit tests green.
  • Codex: 6 completed assessments (3 automatic normal/security pairs). Security lane clean on every head. Normal review, one finding per round, each on a narrower case:
  • Interpretation of "clean": the security lane is clean and every normal-review finding is fixed or deferred with a tracking issue; all threads are resolved. No further push: this head was the last push the budget allowed.
  • Budget: the final independent check is the 7th assessment (1 of the +2 extension, which comes from automatic runs).
  • Independent clean-context check: READY, with tracked follow-up 🤖 bug: removal marker and child-creation fence do not cover legacy id-less workspace rows #4914. It confirmed that item 1 is closed by the landed lease/gate work and found that 🤖 bug: removal marker and child-creation fence do not cover legacy id-less workspace rows #4914 is not a merge blocker.
  • Stopping reason: acceptance criteria met; the remaining finding is tracked.

Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high

@ThomasK33
ThomasK33 added this pull request to the merge queue Sep 27, 2026
Merged via the queue into main with commit 5c8d39a Sep 27, 2026
57 of 60 checks passed
@ThomasK33
ThomasK33 deleted the fix/4782-removal-child-creation branch September 27, 2026 21:35
yermakoffivan pushed a commit to yermakoffivan/mux that referenced this pull request Sep 28, 2026
…coder#4919)

## Summary

The cross-process removal fence (coder#4776/coder#4908) now covers upgraded
workspaces whose config row has no id yet. A removal persists the legacy
row's id before it claims its `pendingRemoval` marker. A task creation
whose parent resolved only through session metadata finds that row again
by its stable identity (config project key + workspace path) inside its
config commit. If the row is gone or marked, the commit refuses.

Fixes coder#4914

## Background

Every step of the fence found the row by id: `claimPendingRemoval` wrote
no marker when `findWorkspaceEntry` missed, `Config.removeWorkspace` did
not deregister the row, and `assertParentAdmitsChild` could not see a
marker or the row's absence (coder#4908 exempted id-less parents so that
creation under them kept working). So another backend could create a
child under such a workspace while it was being removed, or after it was
removed.

## Implementation

- **Removal** (`WorkspaceService.removeUnlocked`, after the mutation
gate and before the marker claim): if the row is not found by id but
`Config.findWorkspace` resolves it (a legacy row), it calls
`Config.getWorkspaceMetadataById`, which persists the read-time id
migration that workspace listing already persists. Then the claim, the
release and the deregistration find the row by id as usual. If the row
still has no id (the best-effort migration write failed), the removal
refuses before any effect: it could not be fenced. This is the
fail-closed choice.
- **Task creation** (`createMany` plans and both `create` commits): when
the parent row has no id in the preparation snapshot, the plan records
`LegacyParentRow` from `Config.findWorkspace`. `assertParentAdmitsChild`
falls back to the id-less row at that path and requires a row by id or
by path, so a removed legacy parent refuses the child the same way a
removed parent with an id does. A parent that resolves nowhere (e.g. the
existing "no config row by id" test that fakes a parent id) keeps
today's behavior.

Scope stayed small (3 files); no new persisted fields: `LegacyParentRow`
is an in-memory plan field.

## Validation

New tests in `workspaceService.pendingRemoval.test.ts` (two backends,
real service stacks, a real id-less row with its id in
`sessions/<basename>/metadata.json`) fail on `origin/main` and pass with
this change:

```
(fail) workspace removal across two backends on one root > a removal marks a legacy id-less row, so the other backend refuses a child under it
(fail) workspace removal across two backends on one root > a batch creation under a legacy id-less parent refuses once the other backend removed it
Expected to contain: "removed"
Received: "created"
```

Each production hunk has its own red test: without the removal change
the first test fails (no marker on the row), and without the
task-creation change the second fails (the batch commits under the
removed parent). A third test checks that a batch creation under a live
legacy parent still commits.

## Risks

Low. The new paths run only for rows without an id, which the read-time
migration normally fixes at the first workspace listing. The removal of
such a row now performs that migration write first, and refuses if the
write fails.

---

_Generated with `xum` • Model: `anthropic:claude-opus-5-5` • Thinking:
`high`_

<!-- mux-attribution: model=anthropic:claude-opus-5-5 thinking=high
costs= -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant