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.
This commit is contained in:
Dane Everitt
2026-04-01 18:46:01 -07:00
committed by GitHub
parent 56fe10fdd6
commit ec7231bd4a
4 changed files with 10 additions and 10 deletions
@@ -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'));
@@ -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'])
@@ -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();
@@ -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);