From 2f9cb8a2f5872f82d0ea63c6bae0c7803d8b791a Mon Sep 17 00:00:00 2001 From: XuJian <265865031+xujiantop-crypto@users.noreply.github.com> Date: Thu, 3 Dec 2026 10:35:11 +0800 Subject: [PATCH] fix(dynamodb): refuse a CreateTable that races another creator (#10035) --- .../services/dynamodb/DynamoDbService.java | 11 ++++++- .../dynamodb/DynamoDbServiceTest.java | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java index 3f26537495..4f213002de 100644 --- a/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java +++ b/src/main/java/io/floci/aws/services/dynamodb/DynamoDbService.java @@ -732,12 +732,21 @@ public class DynamoDbService { lastCheck.run(); synchronized (capacityLock) { + // The check above is only a fast path: a creator that ran its whole validation while + // this one was still in it can have stored the table by now, so the name is decided + // here, next to the write that makes it visible. + if (tableStore.get(storageKey).isPresent()) { + throw new AwsException("ResourceInUseException", + "Table already exists: " + tableName, 400); + } if (!"PAY_PER_REQUEST".equals(billingMode)) { DynamoDbCapacityQuotas.requireNewTableWithinQuotas(table, reservedCapacity(region), region); } + // The items go in before the table is visible, so a write another caller acknowledges + // against it can never be dropped by this call replacing the map afterwards. + itemsByTable.put(scopedItemsKey(storageKey), new ConcurrentSkipListMap<>()); tableStore.put(storageKey, table); } - itemsByTable.put(scopedItemsKey(storageKey), new ConcurrentSkipListMap<>()); LOG.infov("Created table: {0} in region {1}", tableName, region); return table; } diff --git a/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java index fee39a57a2..a4f4b6308e 100644 --- a/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java +++ b/src/test/java/io/floci/aws/services/dynamodb/DynamoDbServiceTest.java @@ -5060,6 +5060,36 @@ class DynamoDbServiceTest { assertEquals("ResourceInUseException", e.getErrorCode()); } + @Test + void createTable_secondCreatorWinsTheName_returnsResourceInUseException() { + // A rival creator that stores the table between this call's existence check and its store + // write is what a second client calling CreateTable at the same moment does. + TableDefinition[] rival = new TableDefinition[1]; + + AwsException refused = assertThrows(AwsException.class, () -> service.createTable("Users", + List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), + 5L, 5L, List.of(), List.of(), List.of(), null, "us-east-1", + () -> rival[0] = createUsersTable("us-east-1"))); + + assertEquals("ResourceInUseException", refused.getErrorCode()); + assertEquals(rival[0].getTableId(), service.describeTable("Users", "us-east-1").getTableId()); + } + + @Test + void createTable_secondCreatorWinsTheName_keepsTheAcknowledgedWrite() { + assertThrows(AwsException.class, () -> service.createTable("Users", + List.of(new KeySchemaElement("userId", "HASH")), + List.of(new AttributeDefinition("userId", "S")), + 5L, 5L, List.of(), List.of(), List.of(), null, "us-east-1", + () -> { + createUsersTable("us-east-1"); + service.putItem("Users", item("userId", "u1"), "us-east-1"); + })); + + assertNotNull(service.getItem("Users", item("userId", "u1"), "us-east-1")); + } + @Test void exportTable_whileCreating_returnsTableNotFoundException() { createUsersTableInCreating(service);