Skip to content

S3: conditional writes never return 409 Conflict, so the retry path a caller must take is untestable #540

Closed

Description

@scttfrdmn

Summary

evaluateWritePreconditions (emulator/s3_conditional.go) returns 412 PreconditionFailed for a
lost race and 404 NoSuchKey for If-Match against an absent key. Both are correct and match AWS.

The gap is that S3 has a third outcome the emulator cannot produce: 409 Conflict. It is not a
cosmetic difference in status code — it selects a different recovery path in the caller, and a
caller that treats it as a synonym for 412 is wrong in a way no test against substrate can reveal.

This follows on from #397, which made 412 reachable. Filing it separately because 409 is a distinct
behavior rather than a missed case of the same one.

What AWS documents

From How to prevent object overwrites with conditional writes, under Conditional write behavior:

You can also receive a 409 Conflict response in the case of concurrent requests if a delete
request to an object succeeds before a conditional write operation on that object completes. When
using conditional writes with PutObject, uploads may be retried after receiving a 409 Conflict
error. When using CompleteMultipartUpload, the entire multipart upload must be re-initiated with
CreateMultipartUpload to upload the object again after receiving a 409 Conflict error.

And in the scenarios section, for a delete landing during an in-flight multipart upload:

This scenario will result in a 409 Conflict response for an If-None-Match header and a
404 Not Found response for an If-Match header.

So the caller contract is three-way, and the arms are not interchangeable:

response meaning correct recovery
412 PreconditionFailed another writer won the race re-read state, recompute, retry the CAS
409 Conflict on PutObject a delete interleaved retry the PUT as-is
409 Conflict on CompleteMultipartUpload a delete interleaved abandon the upload ID; re-do CreateMultipartUpload and every UploadPart
404 NoSuchKey on If-Match object gone re-upload rather than retry the CAS

A CAS loop that retries a CompleteMultipartUpload 409 by re-sending Complete with the same upload
ID will spin until it gives up, because that upload ID can no longer complete. That is the bug this
would catch, and it is invisible today: substrate can only produce 412, so the loop looks correct.

Why it matters here

ObjectFS is evaluating S3 conditional writes as a coordination primitive (objectfs#169) — per-key
CAS for leader election and metadata updates, in place of a replicated log. Verified against
substrate v0.85.0 already, and all of it passes: exactly one of 32 concurrent If-None-Match: *
PUTs wins, a stale-ETag write gets PreconditionFailed, If-Match on an absent key gets
NoSuchKey, and 8 workers running a CAS increment loop converge to exactly 8 with no lost updates.
Substrate's per-key striped mutex, and the comment explaining why check-then-Put would invert the
exactly-one-winner property, is exactly the fidelity that made those results worth trusting.

What cannot be tested is the failure branch a production caller most needs: distinguishing "I lost a
race, recompute and retry" from "a delete interleaved, and for MPU my upload ID is dead." Since the
CAS loop is the whole mechanism under evaluation, its error taxonomy is the part where a wrong
assumption is most expensive.

Suggested shape

409 arises from a race, not from a state a request can assert, so it cannot be produced by
inspecting the current object the way 412 is. Two options, and the second seems better:

  1. Emit 409 when a conditional write finds the key absent and a delete for that key was observed
    recently — needs a per-key "recently deleted" window, which is state substrate would have to keep.

  2. Make it fault-injectable. A FaultController rule that turns a conditional write into
    409 Conflict lets a test exercise the branch deterministically without substrate having to model
    the race. This matches how the outcome is reached in reality — it is a timing accident, so a test
    wanting it should ask for it rather than try to provoke it.

Option 2 also covers the MPU asymmetry cleanly: the rule can target CompleteMultipartUpload
specifically, which is the arm with the expensive recovery.

The error body S3 returns for this is ConditionalRequestConflict, per the
PutObject error list — a
distinct code from PreconditionFailed, which is what an SDK matches on, so it needs to be exact
for errors.As on smithy.APIError to work the way a caller writes it.

Also worth noting

If-None-Match on write correctly rejects any value other than * (matching "The If-None-Match
header expects the * (asterisk) value"), and CompleteMultipartUpload correctly ignores in-progress
uploads when evaluating preconditions — both verified in v0.85.0. Those are the two subtleties I
expected to find missing and did not.

Activity

  1. added
    type: bug
    area: s3
    on Aug 3, 2026
  2. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    Labelled type: bug / area: s3 / fidelity, and deliberately left unmilestoned
    rather than pulled into v0.89.0. Recording the reasoning so the absence is a decision
    rather than an oversight.

    Agreed on the substance, including your option 2. The three-way taxonomy is real and
    the CompleteMultipartUpload arm is the expensive one: a CAS loop that retries Complete
    with the same upload ID after a 409 spins until it gives up, and substrate can only
    produce 412, so that loop looks correct today. That is a caller-contract gap, not a
    cosmetic status-code difference.

    Why option 2 is the right shape here, in substrate's own terms. A 409 arises from a
    race — a delete landing between the conditional write's evaluation and its completion.
    There is no request-inspectable state that implies it, so it cannot be derived the way
    412 is. Option 1's "recently deleted" window would make the response depend on when
    a request arrives, which is the one thing substrate's determinism guarantee forbids:
    same inputs → same outputs, and a replay must reproduce exactly. A FaultController
    rule is the established way substrate produces an outcome that is real but timing-born —
    the test asks for it rather than trying to provoke it, and the seed makes it reproducible.

    Why not v0.89.0. That release is the CloudFormation delete/rollback cluster (#518,
    #508, #534, #520) — one coupled model, sharing no mechanism with the fault controller.
    This belongs with whichever release next touches fault injection; #510 (the shared PRNG
    making Probability order-dependent) is parked for the same reason, and the two are
    better done together: #510 is precisely the kind of determinism defect that would
    undermine a new fault rule's reproducibility, so fixing it first is the sensible order.

    Two things worth pinning down before implementation, both of which affect what a rule
    can express:

    1. Whether the rule targets the operation (PutObject vs CompleteMultipartUpload)
      or the condition header (If-None-Match vs If-Match). Your table makes them
      independent axes — a delete during MPU is 409 for If-None-Match but 404 for
      If-Match — so a rule keyed on operation alone cannot express the documented matrix.
    2. Whether a 409 on CompleteMultipartUpload must also invalidate the upload ID in
      substrate's state. If it does not, a test could retry Complete and succeed, which
      would validate the exact broken retry loop this issue exists to catch. I think it
      must, and that is the part with real state implications rather than just a response.

    Thanks for the detail on the ObjectFS side (objectfs#169) — knowing the CAS loop is the
    mechanism under evaluation, rather than an incidental caller, is what makes the error
    taxonomy worth getting exactly right rather than approximately.

  3. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Picking this up for v0.93.0. Measured it on a live server first, and one result changes the plan I recorded above: option 2 produces the code but not the consequence. It also settles both of the questions I left open.

    Option 2 cannot express the bug this issue exists to catch

    Armed over the control plane on a running server:

    {"service":"s3","operation":"CompleteMultipartUpload",
     "fault_type":"error","error_code":"ConditionalRequestConflict",
     "http_status":409,"times":1}

    The first CompleteMultipartUpload failed as asked, and GET /v1/fault/rules reported fired: 1:

    An error occurred (ConditionalRequestConflict) when calling the
    CompleteMultipartUpload operation: injected fault
    

    Then the immediate retry with the same upload ID succeeded:

    200, ETag "0b5160c2adf21a2967b0bb17bb704b12-1"
    

    Control, same server: a CompleteMultipartUpload that genuinely succeeds and is then retried returns NoSuchUpload. So the difference is not the SDK or the harness — the faulted upload really was still completable.

    The cause is where faults run. InjectFault is step 4.5 of the request pipeline (emulator/server.go:636), before registry.RouteRequest at step 5, so a fault never reaches the plugin and writes no state. That is deliberate and documented ("no state is written for a request a fault refuses"), and right for what fault injection is — it just means a fault rule cannot invalidate an upload ID. A consumer's broken CAS loop, the one that re-sends Complete with the same ID, would pass against a fault rule exactly as it passes today.

    So my answer to my own question 2 above is confirmed, and it is load-bearing rather than a detail: the 409 must invalidate the upload ID, and that requirement is what rules out the fault controller as the mechanism.

    Question 1: the matrix does not need a header axis

    I worried that a rule keyed on operation alone could not express "409 for If-None-Match, 404 for If-Match". It does not have to, because those two cells are not the same kind of outcome:

    • The 404 cell is a determinate observation of state — the object is gone, so If-Match fails with NoSuchKey. Substrate already produces it today from evaluateWritePreconditions, reachable by simply deleting the key. Nothing needs seeding.
    • The 409 cell applies to both headers, not just If-None-Match. The API model's IfMatch documentation for PutObject, CopyObject and CompleteMultipartUpload each says "If a conflicting operation occurs during the upload S3 returns a 409 ConditionalRequestConflict response", with recovery advice that differs per arm — PutObject If-None-Match "retry the upload", If-Match "fetch the object's ETag and retry", Complete "re-initiate the multipart upload with CreateMultipartUpload, and re-upload each part".

    The user guide's 404-for-If-Match line describes the timing where the delete lands before evaluation, which is the determinate case above. So one seeded outcome per operation covers the documented matrix, and the header axis is not needed.

    What ships

    A plugin-level seed — substrate's own documented answer to "a deterministic emulator needs to produce a rare outcome" (the Fault-aware fidelity level). The seed reaches the plugin, so the multipart arm can kill the upload.

    • POST /v1/s3/conditional-conflict and DELETE the same, keyed bucket/key or the "*" wildcard, following the established seed pattern (sqsConsistencySeed / consumeQueueMiss in emulator/sqs_control.go): key-scoped consulted before wildcard, an exhausted key-scoped seed falling through so it cannot mask a funded wildcard, and counted in occurrences rather than a duration — the simulated clock advances with wall time, so a duration seed would make assertions wall-clock dependent.
    • Three independent counters (putConflicts, copyConflicts, completeConflicts), so a fixture can seed the multipart arm without perturbing plain writes.
    • Consumed only inside the existing conditional branch, and only after the preconditions pass. A 412 or 404 is a determinate observation of current state and must not be overwritten by a seeded race; and burning budget on a request that was going to fail anyway silently spends what the test meant to spend on the conflict. An unconditional write never gets 409, since AWS documents the code only on the two conditional members.
    • The CompleteMultipartUpload arm additionally calls the existing abortUploadState, so a same-ID retry gets NoSuchUpload instead of spinning. This deliberately contradicts the 412 arm's "the upload stays open" behaviour, and for a documented reason: Complete's 409 recovery is re-initiating with CreateMultipartUpload, which is only meaningful if the old ID cannot complete.

    Provenance

    The code and the status are documented (the IfMatch / IfNoneMatch member docs above, plus the user guide). But there is no shape named ConditionalRequestConflict in the API model, and the code appears in no operation's errors list, so no message string is documented anywhere. Per docs/fidelity.md the message will be substrate's own and will say so in the comment beside the literal, and the invalidation of the upload ID is substrate's reading of the documented recovery advice rather than an observed response. Assert on the code and the status.

    Shipping alongside #510, for the reason recorded above — its shared PRNG is the determinism defect that would otherwise undermine the fault tier this touches — plus two wiring gaps found while probing: a default CLI server builds no FaultController at all, so /v1/fault/rules answers 501 (which is why my first probe silently did nothing), and when one exists it is seeded from time.Now().UnixNano() with no way for a config file to pin it.

  4. added this to the v0.93.0 milestone on Aug 6, 2026
  5. scttfrdmn commented on Aug 6, 2026

    @scttfrdmn
    OwnerAuthor

    Shipped in v0.93.0 (#583), as the seed described in the comment above rather than as option 2, for the measured reason: a fault short-circuits at Step 4.5 before the plugin, so it produces the code but leaves the upload ID completable — passing the very loop this issue exists to fail.

    What landed. POST /v1/s3/conditional-conflict seeds how many subsequent conditional writes to a key report ConditionalRequestConflict / 409, keyed bucket/key or by *, with independent putConflicts / copyConflicts / completeConflicts counters; DELETE clears one or all. A conflict is consumed only after the preconditions pass, so a genuine 412 or 404 is still reported as itself and does not spend the budget, and an unconditional write is never affected — the code is documented only on If-Match/If-None-Match.

    The headline arm. Consuming a seeded completeConflicts calls abortUploadState, so the upload ID genuinely dies. Verified against the real AWS CLI: the first complete-multipart-upload returns 409 ConditionalRequestConflict, the retry with the same upload ID returns NoSuchUpload, ListParts on that ID is gone, ListMultipartUploads is empty, and the object is a 404. Control: a 412'd Complete leaves the upload open and completable, which is the pre-existing behaviour and the distinction a caller has to act on.

    Provenance, per docs/fidelity.md. The code, the 409 status and the per-arm recovery advice come from the S3 API model's IfMatch/IfNoneMatch member documentation and the conditional-writes user-guide page. No message text is documented anywhere — the model carries no ConditionalRequestConflict shape and lists the code in no operation's errors — so the message is substrate's own and the comment beside the literal says so. Assert on the code and the status. The upload-ID invalidation is substrate's inference from the documented recovery advice, which is only meaningful if the old ID cannot complete; that too is recorded in the code.

    12 tests in emulator/s3_conditional_conflict_test.go; docs/services.md documents the endpoint, the precedence and fall-through rules, and the multipart asymmetry, and the now-false paragraph claiming 409 was "deliberately not emulated" is gone.

    Mutation-tested where the release's assumptions are load-bearing — abortUploadState dropped from the Complete arm, the seed consulted before evaluateWritePreconditions instead of after, the fall-through continue turned into a return, the cond.any() gate removed, the decrement dropped, and 409 → 412. All CAUGHT.

    One measured defect found on the way and deliberately not fixed here: #582, the same wildcard-double-consume race in consumeQueueMiss that this implementation's dedicated seed mutex avoids. It is milestoned v0.94.0.

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

    area: s3fidelitytype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions