From da652e4b19f310ee4ff001330d95388eb63a2959 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:59:47 +0100 Subject: [PATCH 1/2] fix(storage): remove the bucket document when its files collection cannot be created Bucket creation writes the bucket document, then creates the bucket_ collection. The second step is DDL, cannot share a transaction with the first, and needs the document's sequence for its name. Any failure between the two (a lock wait timeout, a killed pod) left a bucket document with no collection under it: a bucket that can never hold a file, and that every later request fails on with "Collection not found". Cloud found 227 such buckets across regions. Delete the document when collection creation throws, then rethrow, so a failed create leaves nothing behind. The test asserts the buckets collection is empty after the failed request, and fails on the unpatched code. Co-Authored-By: Claude Fable 5.1 --- .../Modules/Storage/Http/Buckets/Create.php | 11 ++- .../Storage/Http/Buckets/CreateTest.php | 80 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php index c7101cf191..ea8c798ccd 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php @@ -153,7 +153,16 @@ class Create extends Action $bucket = $dbForProject->getDocument('buckets', $bucketId); - $dbForProject->createCollection('bucket_' . $bucket->getSequence(), $attributes, $indexes, permissions: $permissions, documentSecurity: $fileSecurity); + // The files collection is DDL and cannot share a transaction with the + // document insert above, and its name needs the document's sequence, + // so the document is committed first. A bucket without its collection + // can never hold a file: remove it rather than leave an orphan. + try { + $dbForProject->createCollection('bucket_' . $bucket->getSequence(), $attributes, $indexes, permissions: $permissions, documentSecurity: $fileSecurity); + } catch (\Throwable $th) { + $dbForProject->deleteDocument('buckets', $bucketId); + throw $th; + } } catch (DuplicateException) { throw new Exception(Exception::STORAGE_BUCKET_ALREADY_EXISTS); } diff --git a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php new file mode 100644 index 0000000000..b9edcbfc67 --- /dev/null +++ b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php @@ -0,0 +1,80 @@ +createStub(Database::class); + $dbForProject->method('createDocument')->willReturnCallback( + static function (string $collection, Document $document) use (&$buckets): Document { + $buckets[$document->getId()] = $document->setAttribute('$sequence', '7'); + + return $buckets[$document->getId()]; + } + ); + $dbForProject->method('getDocument')->willReturnCallback( + static fn (string $collection, string $id): Document => $buckets[$id] ?? new Document() + ); + $dbForProject->method('deleteDocument')->willReturnCallback( + static function (string $collection, string $id) use (&$buckets): bool { + unset($buckets[$id]); + + return true; + } + ); + $dbForProject->method('createCollection')->willThrowException( + new RuntimeException('Lock wait timeout exceeded') + ); + + $response = $this->createStub(Response::class); + $queueForEvents = $this->createStub(Event::class); + + try { + (new Create())->action( + 'photos', + 'Photos', + null, + false, + true, + 30000000, + [], + null, + null, + true, + true, + $response, + $dbForProject, + $queueForEvents, + ); + $this->fail('A failed collection creation must fail the request.'); + } catch (RuntimeException) { + } + + $this->assertSame([], $buckets, 'A bucket whose files collection was never created must not survive the request.'); + } +} From d4abb88c8777d8f8223d57221ca8e27878ccf38c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 17:05:23 +0100 Subject: [PATCH 2/2] test(storage): drop the bucket create unit test It asserted the double's bookkeeping of a deleteDocument call rather than anything a user can observe. Co-Authored-By: Claude Fable 5.1 --- .../Storage/Http/Buckets/CreateTest.php | 80 ------------------- 1 file changed, 80 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php diff --git a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php deleted file mode 100644 index b9edcbfc67..0000000000 --- a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php +++ /dev/null @@ -1,80 +0,0 @@ -createStub(Database::class); - $dbForProject->method('createDocument')->willReturnCallback( - static function (string $collection, Document $document) use (&$buckets): Document { - $buckets[$document->getId()] = $document->setAttribute('$sequence', '7'); - - return $buckets[$document->getId()]; - } - ); - $dbForProject->method('getDocument')->willReturnCallback( - static fn (string $collection, string $id): Document => $buckets[$id] ?? new Document() - ); - $dbForProject->method('deleteDocument')->willReturnCallback( - static function (string $collection, string $id) use (&$buckets): bool { - unset($buckets[$id]); - - return true; - } - ); - $dbForProject->method('createCollection')->willThrowException( - new RuntimeException('Lock wait timeout exceeded') - ); - - $response = $this->createStub(Response::class); - $queueForEvents = $this->createStub(Event::class); - - try { - (new Create())->action( - 'photos', - 'Photos', - null, - false, - true, - 30000000, - [], - null, - null, - true, - true, - $response, - $dbForProject, - $queueForEvents, - ); - $this->fail('A failed collection creation must fail the request.'); - } catch (RuntimeException) { - } - - $this->assertSame([], $buckets, 'A bucket whose files collection was never created must not survive the request.'); - } -}