diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.php index 7c38c91f0..4f33ed542 100644 --- a/app/Services/Social/ConnectionVerifier.php +++ b/app/Services/Social/ConnectionVerifier.php @@ -20,6 +20,7 @@ use App\Services\Social\Discord\DiscordClient; use App\Services\Social\Meta\GraphError; use App\Services\Social\Telegram\TelegramApi; +use Illuminate\Contracts\Cache\LockTimeoutException; use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; @@ -204,7 +205,18 @@ public function refreshToken(SocialAccount $account): bool $lock = Cache::lock("token_refresh:{$account->id}", self::REFRESH_LOCK_SECONDS); if (! $lock->get()) { - // Another process is already refreshing this token. + // Another process is already refreshing this token. Wait for it + // to finish before re-reading the row: re-reading immediately + // usually finds the not-yet-persisted old token (both jobs fire + // on the same cron tick) and needlessly fails the caller with a + // transient error even though a fresh token lands moments later. + try { + $lock->block(15); + $lock->release(); + } catch (LockTimeoutException) { + // Fall through — the row is re-read either way. + } + $account->refresh(); if ($account->is_token_expired) {