fix(s3): check CompleteMultipartUpload conditions atomically with the write (#6286)

* fix(s3): check CompleteMultipartUpload conditions atomically with the write

CompleteMultipartUpload judged If-Match and If-None-Match only in the
controller, before any lock, and then stored the object with options that
carried neither header. The recheck storeObject does under the bucket
monitor for PutObject therefore never ran for a completion, so concurrent
completions with If-None-Match: * on one key could all return 200, the last
body winning.

completeMultipartUpload now takes ifMatch and ifNoneMatch and passes them on
the store options, so they are rechecked under the bucket monitor together
with the write. The controller's early check stays, to spare assembling
parts for a request that will fail.

* test(s3): race conditional CompleteMultipartUpload over HTTP

16 barrier-synced CompleteMultipartUpload requests with If-None-Match: *
on one key: exactly one 200, the rest 412, and the stored body is the
winner's. Mirrors the existing concurrent PutObject test.

* test(s3): share the barrier race harness between conditional-write tests

The concurrent PutObject and CompleteMultipartUpload If-None-Match tests
now use one raceStatusCodes helper for the barrier, executor, timeouts and
shutdown, so the two cannot drift.
This commit is contained in:
Ben Wart authored and GitHub committed 2026-10-22 17:32:50 -04:00
1 parent d20084ade3
commit 107da3e75a
4 files changed
+211 -30

No files matched your search

@@ -1697,7 +1697,7 @@ public class S3Controller {
S3Checksum expectedChecksum = extractChecksum(httpHeaders);
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers,
completedPartETags(completedParts), completedPartChecksums(completedParts), checksumType,
expectedChecksum, multipartObjectSize(httpHeaders, uriInfo));
expectedChecksum, multipartObjectSize(httpHeaders, uriInfo), ifMatch, ifNoneMatch);
String baseUrl = uriInfo.getBaseUri().toString();
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
@@ -4695,6 +4695,18 @@ public class S3Service implements Resettable, ResourceProvider {
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
Map<Integer, String> partETags, Map<Integer, S3Checksum> partChecksums,
String checksumType, S3Checksum expectedChecksum, Long expectedObjectSize) {
return completeMultipartUpload(bucket, key, uploadId, partNumbers, partETags, partChecksums,
checksumType, expectedChecksum, expectedObjectSize, null, null);
}
/**
* {@code ifMatch} and {@code ifNoneMatch} are judged by storeObject under the bucket's monitor,
* together with the write, so concurrent conditional completions on one key cannot all succeed.
*/
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
Map<Integer, String> partETags, Map<Integer, S3Checksum> partChecksums,
String checksumType, S3Checksum expectedChecksum, Long expectedObjectSize,
String ifMatch, String ifNoneMatch) {
S3Object completed = withMultipartOperationLock(bucket, uploadId, () -> {
MultipartUpload upload = getMultipartUpload(bucket, key, uploadId);
@@ -4788,7 +4800,9 @@ public class S3Service implements Resettable, ResourceProvider {
.withObjectLockMode(upload.getObjectLockMode())
.withRetainUntilDate(upload.getRetainUntilDate())
.withLegalHoldStatus(upload.getLegalHoldStatus())
.withTagging(upload.getTagging()),
.withTagging(upload.getTagging())
.withIfMatch(ifMatch)
.withIfNoneMatch(ifNoneMatch),
compositeETag,
upload.getInitiatorAccountId() != null ? upload.getInitiatorAccountId() : ownerId());
} finally {