Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/Actions/CreateCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function handle(User $user, string $body, ?Commit $parent = null, ?Commit
}

if ($commit->status === CommitStatus::Approved) {
dispatch(new CheckUserLevel($user))->afterCommit();
// Outside the transaction, like CreateCommitStar: on Postgres, taking a unique-job lock that
// is already held fails the insert and aborts this transaction (25P02), which 500s the create.
DB::afterCommit(fn () => dispatch(new CheckUserLevel($user)));
}

if ($commit->status === CommitStatus::Pending && ! $hasGoodReputation) {
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/Controllers/CommitControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
use App\Enums\CommitStatus;
use App\Enums\Log;
use App\Http\Middleware\HandleInertiaRequests;
use App\Jobs\CheckUserLevel;
use App\Jobs\DetectCommitLanguage;
use App\Jobs\UnfurlLink;
use App\Models\Commit;
use App\Models\CommitImage;
use App\Models\LinkPreview;
use App\Models\Notification;
use App\Models\User;
use Illuminate\Database\DatabaseTransactionsManager;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
Expand All @@ -35,6 +38,36 @@
expect($user->commits()->sole()->body)->toBe('Hello, devnull!');
});

it('creates a commit while a unique level-check job already holds a database cache lock', function (): void {
config(['cache.default' => 'database']);
config(['app.admins' => ['nunomaduro']]);

$user = User::factory()->create(['handle' => 'nunomaduro']);
$lock = Cache::lock('laravel_unique_job:'.CheckUserLevel::class.':'.$user->id, 3600);

expect($lock->get())->toBeTrue();

// The test transaction manager runs after-commit callbacks before its wrapping transaction ends.
// Use the production manager to keep the callback pending until the outer transaction commits.
$connection = DB::connection();
$testManager = $this->app['db.transactions'];
$manager = new DatabaseTransactionsManager;
$manager->begin($connection->getName(), $connection->transactionLevel());

$connection->setTransactionManager($manager);

try {
$this->actingAs($user)
->fromRoute('log.show', Log::Following)
->post(route('commit.store'), ['body' => 'Second commit'])
->assertRedirectToRoute('log.show', Log::Following);
} finally {
$connection->setTransactionManager($testManager);
}

expect($user->commits()->sole()->body)->toBe('Second commit');
});

it('flashes the created commit', function (): void {
$user = User::factory()->create();

Expand Down