fix(rds): reject a duplicate create while provisioning is in flight (#2525)

CreateDBCluster registers the new cluster only after the backing
container is provisioned, and a cold-cache image pull holds that
window open for about ninety seconds in the issue's log — long enough
for the CLI's retry to pass the exists-check again and start a second
container for the same identifier. Both containers then serve one
cluster, with the cluster endpoint wired to one database and the
instance endpoint to the other, and no error anywhere.
CreateDBInstance has the identical check-then-provision-then-register
race.

Serialize creates per identifier with a provisioning sentinel: the
wrapper claims the identifier before the exists-check and releases it
after registration, so a duplicate arriving mid-provision fails fast
with the same fault a completed create produces
(DBClusterAlreadyExistsFault / DBInstanceAlreadyExists) instead of
provisioning again. A failed create releases the identifier on the
way out, so a clean retry still works — the failed path registers
nothing, so a retained sentinel would wedge the identifier with no
resource to delete. Cluster and instance identifiers stay
independent namespaces, as in the issue's own reproduction, which
provisions both under one name.

The reject arm of the issue's suggested fix is deliberate: joining
the in-flight create would park the retrying client's connection on
the remainder of the pull, and CreateDBCluster is not documented as
idempotent-return on AWS either.

Closes #2051
This commit is contained in:
soreavis authored and GitHub committed 2026-08-25 22:53:28 -05:00
1 parent ce4f6d1c48
commit 7ef698c1f4
2 files changed
+272

No files matched your search

@@ -130,6 +130,13 @@ public class RdsService implements Resettable, ResourceProvider {
private final StorageBackend<String, DbProxyTargetGroup> proxyTargetGroups;
private final RdsContainerManager containerManager;
private final RdsProxyManager proxyManager;
// CreateDBCluster/CreateDBInstance register the new resource only after the
// backing container is provisioned, and an image pull can hold that window
// open long enough for a client-side retry to pass the exists-check again
// and provision a second container for the same identifier. The sentinel
// serializes creates per identifier so the duplicate fails fast with the
// same fault a completed create produces.
private final Set<String> provisioningIds = ConcurrentHashMap.newKeySet();
private final Ec2Service ec2Service;
private final RegionResolver regionResolver;
private final EmulatorConfig config;
@@ -470,6 +477,36 @@ public class RdsService implements Resettable, ResourceProvider {
String optionGroupName,
String region,
boolean autoMinorVersionUpgrade) {
String provisioningKey = "instance:" + currentAccountId() + ":"
+ dbResourceKey(effectiveRegion(region), id);
if (!provisioningIds.add(provisioningKey)) {
throw new AwsException("DBInstanceAlreadyExists",
"DB instance " + id + " already exists.", 400);
}
try {
return doCreateDbInstance(id, engineParam, engineVersion, masterUsername,
masterPassword, dbName, dbInstanceClass, allocatedStorage, iamEnabled,
paramGroupName, dbSubnetGroupName, dbClusterIdentifier, availabilityZone,
multiAz, manageMasterUserPassword, masterUserSecretKmsKeyId, tags,
vpcSecurityGroupIds, optionGroupName, region, autoMinorVersionUpgrade);
} finally {
provisioningIds.remove(provisioningKey);
}
}
private DbInstance doCreateDbInstance(String id, String engineParam, String engineVersion,
String masterUsername, String masterPassword,
String dbName, String dbInstanceClass,
int allocatedStorage, boolean iamEnabled,
String paramGroupName, String dbSubnetGroupName,
String dbClusterIdentifier, String availabilityZone,
boolean multiAz, boolean manageMasterUserPassword,
String masterUserSecretKmsKeyId,
Map<String, String> tags,
List<String> vpcSecurityGroupIds,
String optionGroupName,
String region,
boolean autoMinorVersionUpgrade) {
String effectiveRegion = effectiveRegion(region);
String dbiResourceId = "db-" + java.util.UUID.randomUUID().toString()
.replace("-", "").substring(0, 24).toUpperCase();
@@ -1149,6 +1186,29 @@ public class RdsService implements Resettable, ResourceProvider {
String availabilityZone, boolean multiAz, String region,
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
Integer serverlessV2SecondsUntilAutoPause) {
String provisioningKey = "cluster:" + currentAccountId() + ":"
+ dbResourceKey(effectiveRegion(region), id);
if (!provisioningIds.add(provisioningKey)) {
throw new AwsException("DBClusterAlreadyExistsFault",
"DB cluster " + id + " already exists.", 400);
}
try {
return doCreateDbCluster(id, engineParam, engineVersion, masterUsername, masterPassword,
databaseName, iamEnabled, paramGroupName, dbSubnetGroupName, availabilityZone,
multiAz, region, serverlessV2MinCapacity, serverlessV2MaxCapacity,
serverlessV2SecondsUntilAutoPause);
} finally {
provisioningIds.remove(provisioningKey);
}
}
private DbCluster doCreateDbCluster(String id, String engineParam, String engineVersion,
String masterUsername, String masterPassword,
String databaseName, boolean iamEnabled,
String paramGroupName, String dbSubnetGroupName,
String availabilityZone, boolean multiAz, String region,
Double serverlessV2MinCapacity, Double serverlessV2MaxCapacity,
Integer serverlessV2SecondsUntilAutoPause) {
String effectiveRegion = effectiveRegion(region);
String clusterResourceId = "cluster-" + java.util.UUID.randomUUID().toString()
.replace("-", "").substring(0, 24).toUpperCase();