Skip to content

feat(s3): a seedable 409 ConditionalRequestConflict on conditional writes (#540) - #583

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/540-s3-conditional-conflict
Aug 6, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/540-s3-conditional-conflict

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The gap

S3 conditional writes in Substrate could produce two of the three outcomes AWS
documents — 412 PreconditionFailed and 404 NoSuchKey — but never the 409 ConditionalRequestConflict S3 returns when a concurrent operation interferes with
a conditional write between its evaluation and its completion.

The three are not interchangeable, and the recovery each demands differs:

Outcome Meaning Documented recovery
412 PreconditionFailed another writer won re-read, recompute, retry the CAS
404 NoSuchKey on If-Match the object is gone re-upload rather than retry the CAS
409 on PutObject / CopyObject a delete interleaved retry as-is (for If-Match, fetch the current ETag first)
409 on CompleteMultipartUpload a delete interleaved abandon the upload ID — re-do CreateMultipartUpload and every UploadPart

A CAS loop that answers the last case like the first re-sends Complete with an
upload ID that can never complete again and spins until it gives up. Against
Substrate that loop looked correct, because only 412 was reachable.

Why a seed, and not the fault rule the issue proposed

#540 prefers making the code fault-injectable. Measured: that produces the code
but not its consequence.
InjectFault runs at step 4.5 (server.go:636),
before registry.RouteRequest, so a fault never reaches the plugin and writes no
state. With a rule armed on CompleteMultipartUpload, the first Complete returned
the injected 409 and the immediate retry with the same upload ID returned 200
with ETag "0b5160c2adf21a2967b0bb17bb704b12-1" — passing the exact loop the code
exists to catch. (Control: a genuinely-successful Complete retried gives
NoSuchUpload.) Full evidence in
#540 (comment).

So this is a plugin-level seed, which reaches the plugin and can therefore call
abortUploadState. The issue's option 1 (a per-key recently-deleted window) is
rejected for the reason the issue itself gives: it makes Substrate keep state to
model a timing accident.

What this adds

POST /v1/s3/conditional-conflict seeds how many subsequent conditional writes to
a key report the code; DELETE clears one (?bucket=&key=) or all.

curl -X POST localhost:4566/v1/s3/conditional-conflict \
  -d '{"bucket":"cond","key":"big","completeConflicts":1}'

Keyed bucket/key or by the * wildcard, with independent counters per operation
(putConflicts, copyConflicts, completeConflicts) so a fixture can seed the
multipart arm — the one with the expensive recovery — without perturbing plain
writes. It follows the established seed pattern (sqs_control.go) down to its
reasoning: key-scoped consulted before the wildcard, falling through when
exhausted
so a spent key-scoped seed cannot mask a funded wildcard, and counted
in occurrences rather than measured as a duration, because TimeController.Now
advances with wall time and a duration window would make assertions wall-clock
dependent.

Consuming a seeded multipart conflict invalidates the upload. A same-ID retry
gets NoSuchUpload, and ListParts on that ID is gone. This deliberately
contradicts the 412 arm, which leaves the upload open, and the comment at the call
site explains why: AWS documents the 409 recovery as re-initiating and re-uploading
every part, advice only meaningful if the old ID cannot complete. The inference from
that advice to "the ID is dead" is Substrate's, and the comment says so.

Nothing changes for an unseeded caller

  • A conflict is consumed only after evaluateWritePreconditions passes. A 412
    or 404 is a determinate observation of the destination's current state, is still
    reported as itself, and does not spend the budget — otherwise a request that
    was going to fail anyway silently consumes what the test armed for the conflict.
  • An unconditional write to a seeded key is untouched and spends nothing; AWS
    documents this code only on the two conditional headers.
  • bucket and key must be given together (a 400 otherwise), because such a
    seed would be stored under a key no write can match: it would look armed, never
    fire, and the fixture would pass vacuously.

Provenance

The code and the 409 status are documented — they appear in the IfMatch and
IfNoneMatch member docs for all three operations, and in the user guide's
conditional-writes page. No message text is documented anywhere: the model
carries no ConditionalRequestConflict shape and lists the code in no operation's
errors. Per docs/fidelity.md the message is Substrate's own and the comment
beside the literal says so. Assert on the code and the status.

Concurrency

Consumption takes a dedicated mutex rather than the per-key stripe the caller
already holds. The stripe is chosen by hashing bucket/key, but the wildcard seed is
shared across every key, so two concurrent writes to different keys hold
different stripes and could both read a budget of 1 and both decrement — the
read-modify-write hazard s3KeyMutex exists to close, since StateManager has no
compare-and-swap. Lock order is always key-stripe → seed mutex.

The same window exists in consumeQueueMiss on the SQS side; filed separately as
#582 rather than fixed here.

Tests

Twelve tests in emulator/s3_conditional_conflict_test.go. The headline gate seeds
the multipart arm, asserts 409, then asserts the same-ID retry is NoSuchUpload
with ListParts and the upload listing gone — plus its control, that a 412'd
Complete still leaves the upload open. Also: both headers on the Put arm, Copy, the
counters' independence, key-scoped-beats-wildcard and the fall-through, an
unconditional write untouched, a failed precondition keeping its budget, a refused
write leaving the object byte-identical, the bare <Error> wire shape (a wrapped
document would hide the code from an SDK), and the endpoint's validation.

Mutation tested, 7 mutants, all CAUGHT: abortUploadState dropped from the
Complete arm; the seed consulted before the preconditions; continue → return on
an exhausted counter; the cond.any() gate removed; the decrement dropped;
StatusConflict → StatusPreconditionFailed; the bucket-XOR-key validation
dropped.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0
issues), make test (race), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go vet -C test/e2e ./..., go test -C test/e2e ./... — all clean.

End to end against substrate server --address :4621 with the real AWS CLI:

$ aws s3api complete-multipart-upload --bucket cond --key big --upload-id "$UP" --if-none-match '*' ...
An error occurred (ConditionalRequestConflict) when calling the CompleteMultipartUpload operation: ...
$ aws s3api complete-multipart-upload --bucket cond --key big --upload-id "$UP" --if-none-match '*' ...
An error occurred (NoSuchUpload) when calling the CompleteMultipartUpload operation: ...
$ aws s3api list-parts --bucket cond --key big --upload-id "$UP"
An error occurred (NoSuchUpload) when calling the ListParts operation: ...

The plain arm (409 then 200), the copy arm, an unconditional write to a seeded key
(200, budget unspent), and both 400 validations were checked the same way.

Docs

docs/services.md's "Conditional writes" section gains the outcome, the recovery
table, the endpoint with curl examples, the precedence and
counted-in-occurrences rules, and the upload-invalidation asymmetry. The paragraph
claiming 409 is "deliberately not emulated" is gone — it is now false. The three
operation rows cross-reference it.

Closes #540

…ites (#540)

S3 conditional writes could reach two of the three outcomes AWS documents —
412 PreconditionFailed and 404 NoSuchKey — but never the 409 returned when a
concurrent operation interferes. The three are not interchangeable: a 412 means
re-read, recompute and retry the compare-and-swap; a 409 on PutObject means
retry the request as-is; a 409 on CompleteMultipartUpload means the upload ID is
finished and the caller must re-do CreateMultipartUpload and every UploadPart. A
CAS loop that answers the third case like the first spins until it gives up, and
against Substrate that loop looked correct.

POST /v1/s3/conditional-conflict seeds how many subsequent conditional writes to
a key report the code, keyed bucket/key or by the "*" wildcard, with independent
counters per operation; DELETE clears one or all. Follows the established seed
pattern: key-scoped consulted before the wildcard, falling through when
exhausted, counted in occurrences rather than measured as a duration.

Consuming a seeded multipart conflict invalidates the upload, so a same-ID retry
gets NoSuchUpload and ListParts on that ID is gone. That is why this is a
plugin-level seed and not a FaultController rule as the issue proposed: faults
are evaluated at step 4.5, before a request reaches its plugin, so a faulted
Complete writes no state and its upload ID stays completable — reproducing the
code but not the consequence, and passing the very loop the code exists to catch.

Nothing changes for an unseeded caller. A conflict is consumed only after the
preconditions pass, so a 412 or 404 is still reported as itself and does not
spend the budget, and an unconditional write to a seeded key is untouched, since
AWS documents the code only on the If-Match and If-None-Match members.

Consumption takes a dedicated mutex rather than the per-key stripe the caller
already holds: the wildcard seed is shared across keys, so two writes to
different keys hold different stripes and could double-consume one conflict.
Lock order is always key-stripe then seed mutex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant