Skip to content

POST /workloads: concurrent create of one new name can hand the owner to the later writer #356

Closed

Description

@pdettori

Problem

POST /workloads is check-then-act (packages/knative-server/src/server.ts, handleCreateWorkload):

  1. findWorkload(name) checks for a live workload that another subject owns, and answers 409 workload_name_taken if there is one.
  2. createWorkload() calls Context Service.
  3. saveWorkload() writes the record with owner = <caller subject>.

Nothing makes steps 1–3 atomic. When two subjects create the same new name at the same moment, both pass step 1. If Context Service's create is idempotent for an existing pool name (not verified), both get 201, and the later saveWorkload sets the owner. The subject that lost now holds a 201 for a workload that answers it 404, while the other subject owns a pool the first one may already have started using.

A lost sh:workload:<id> record has the same effect (compose and deploy/vm Redis have no volume): if Context Service's create is idempotent, the next caller to create that name takes over the live pool.

Proposed fix

Before calling Context Service, reserve the owner with a set-if-absent write (SET NX, with a TTL so a crashed create cannot hold the name for ever). Release or confirm the reservation after saveWorkload. RedisResultStore only has get/set today, so this needs setIfAbsent and delete. The handling must also cover a record whose status is deleted: a name must be reusable once its workload is deleted.

Tests: two concurrent creates of one name by different subjects give exactly one 201, and the stored owner matches it.

Context

Found in the self-review of #350 (MI1 S1). It is recorded as deferred in docs/specs/2026-09-28-moca-multi-user-isolation-design.md §13 ("An atomic workload create"), and the code comment in handleCreateWorkload points there. #350's ownership checks (ownedBy, mayDelete, the 409) are otherwise pinned by packages/knative-server/test/workload-auth-route.test.ts.

Assisted-By: Claude (Anthropic AI) [email protected]

Activity

  1. added
    bug
    on Sep 29, 2026
  2. pdettori commented on Sep 29, 2026

    @pdettori
    MemberAuthor

    Round 3 of #350's review found that the create race is only half of it. handleGetWorkload's ownership check and its saveWorkload are also a Context Service round trip apart, so GET is a writer too. Alice's in-flight GET can land after she deletes the workload and Bob re-creates it, and overwrite his record with owner=alice, or bring back a record just marked deleted. A set-if-absent at create does not cover that; it needs a compare-and-set against the record read at the check, or GET not writing ownership fields. The full description is in #363. Fix both together.

    Assisted-By: Claude (Anthropic AI) [email protected]

  3. pdettori commented on Oct 16, 2026

    @pdettori
    MemberAuthor

    Closing: the code this issue was filed against no longer exists, and the rewrite that replaced it removes every leg of the race.

    handleCreateWorkload in server.ts (check-then-act: findWorkload → Context Service → saveWorkload) was replaced by the workload lifecycle in packages/knative-server/src/workloads.ts:

    • Atomic create. The RESERVE Lua script checks the name and quota and writes the reservation in one atomic Redis operation. A same-name create racing an in-flight one answers 409 workload_name_taken — pinned by the first lifecycle races test in packages/knative-server/test/workloads.test.ts (4a6a57d).
    • Cross-subject name contention no longer exists. Record keys are subject-scoped (sh:wl:<sha256(owner)>:<name>, the first half of /workloads: authorize caller-named PVCs per subject, and scope workload names #358), and contextName derives from owner + name + generation. Two subjects creating the same name each get their own record and Context, so the issue's proposed acceptance test ("two concurrent creates by different subjects give exactly one 201") no longer applies — both 201s are the specified behavior now.
    • A lost record can't hand over a live pool. A missing record only frees the name for its owner; a fresh create derives a new contextName (new generation) and can never take over a live Context.
    • The MI1 S1 follow-up: #350 round-3 review items (SH_MODEL_HEADERS, Vertex ADC, GET write-back race, pins, doc fixes) #363 GET write-back race is closed by the same rewrite. GET is read-only — handleWorkloads reads the record and answers publicView; it never writes ownership fields. (The comment above asked to fix both together; the second half needed no code because the writer went away.)
    • A crashed create cannot hold the name forever. A record stuck in creating keeps the name and its quota unit, but the owner's DELETE cleans it up from every live status, and generation-CAS keeps a stale create from rolling back a newer Context — both pinned by the other lifecycle races tests. No reservation TTL is needed.
    • Deleted names are reusable — RESERVE only refuses records whose status is not deleted.

    The MI1 spec's §13 row carries the closure note. The only commit is the pin test; no production change was needed.

    Assisted-By: Claude (Anthropic AI) [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions