From ec7231bd4aa2c3a00b4264fbddc235bdc73e23b2 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 1 Apr 2026 18:46:01 -0700 Subject: [PATCH] Lock resources more explicitly when creating databases or backups (#5613) Addresses an issue where the concept of a lock was there, but no actual lock was acquired. --- .../Api/Client/Servers/BackupController.php | 2 +- .../Api/Client/Servers/DatabaseController.php | 11 +++++------ .../Allocations/FindAssignableAllocationService.php | 2 ++ app/Services/Databases/DatabasePasswordService.php | 5 ++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/BackupController.php b/app/Http/Controllers/Api/Client/Servers/BackupController.php index 002f77d8f..723f3c916 100644 --- a/app/Http/Controllers/Api/Client/Servers/BackupController.php +++ b/app/Http/Controllers/Api/Client/Servers/BackupController.php @@ -78,7 +78,7 @@ class BackupController extends ClientApiController } $backup = Activity::event('server:backup.start')->transaction(function ($log) use ($action, $server, $request) { - $server->backups()->lockForUpdate(); + $server->backups()->lockForUpdate()->count(); $backup = $action->handle($server, $request->input('name')); diff --git a/app/Http/Controllers/Api/Client/Servers/DatabaseController.php b/app/Http/Controllers/Api/Client/Servers/DatabaseController.php index ca9ec28a9..946861797 100644 --- a/app/Http/Controllers/Api/Client/Servers/DatabaseController.php +++ b/app/Http/Controllers/Api/Client/Servers/DatabaseController.php @@ -6,6 +6,7 @@ use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\Database; use Pterodactyl\Facades\Activity; +use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Services\Databases\DatabasePasswordService; use Pterodactyl\Transformers\Api\Client\DatabaseTransformer; use Pterodactyl\Services\Databases\DatabaseManagementService; @@ -49,7 +50,9 @@ class DatabaseController extends ClientApiController public function store(StoreDatabaseRequest $request, Server $server): array { $database = Activity::event('server:database.create')->transaction(function ($log) use ($request, $server) { - $server->databases()->lockForUpdate(); + if ($server->databases()->lockForUpdate()->count() >= $server->database_limit) { + throw new DisplayException('Cannot create additional databases on this server: limit has been reached.'); + } $database = $this->deployDatabaseService->handle($server, $request->validated()); @@ -75,11 +78,7 @@ class DatabaseController extends ClientApiController Activity::event('server:database.rotate-password') ->subject($database) ->property('name', $database->database) - ->transaction(function () use ($database) { - $database->lockForUpdate(); - - $this->passwordService->handle($database); - }); + ->transaction(fn () => $this->passwordService->handle($database)); return $this->fractal->item($database->refresh()) ->parseIncludes(['password']) diff --git a/app/Services/Allocations/FindAssignableAllocationService.php b/app/Services/Allocations/FindAssignableAllocationService.php index ffc15d044..76b27ac6b 100644 --- a/app/Services/Allocations/FindAssignableAllocationService.php +++ b/app/Services/Allocations/FindAssignableAllocationService.php @@ -39,6 +39,7 @@ class FindAssignableAllocationService // server. /** @var Allocation|null $allocation */ $allocation = $server->node->allocations() + ->lockForUpdate() ->where('ip', $server->allocation->ip) ->whereNull('server_id') ->inRandomOrder() @@ -102,6 +103,7 @@ class FindAssignableAllocationService /** @var Allocation $allocation */ $allocation = $server->node->allocations() + ->lockForUpdate() ->where('ip', $server->allocation->ip) ->where('port', $port) ->firstOrFail(); diff --git a/app/Services/Databases/DatabasePasswordService.php b/app/Services/Databases/DatabasePasswordService.php index 66c395df4..ebee37bca 100644 --- a/app/Services/Databases/DatabasePasswordService.php +++ b/app/Services/Databases/DatabasePasswordService.php @@ -32,12 +32,11 @@ class DatabasePasswordService $password = Utilities::randomStringWithSpecialCharacters(24); $this->connection->transaction(function () use ($database, $password) { - $this->dynamic->set('dynamic', $database->database_host_id); - - $this->repository->withoutFreshModel()->update($database->id, [ + $database->sharedLock()->update([ 'password' => $this->encrypter->encrypt($password), ]); + $this->dynamic->set('dynamic', $database->database_host_id); $this->repository->dropUser($database->username, $database->remote); $this->repository->createUser($database->username, $database->remote, $password, $database->max_connections); $this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);