From 60acb23d72c7c333d8a9f5a9a0df901ddfb8e91e Mon Sep 17 00:00:00 2001 From: DrakiaXYZ <565558+DrakiaXYZ@users.noreply.github.com> Date: Sun, 26 Oct 2025 10:27:30 -0700 Subject: [PATCH] Move cooldown check inside the lock for better thread safety (#663) Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com> --- .../Services/BackupService.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BackupService.cs b/Libraries/SPTarkov.Server.Core/Services/BackupService.cs index cd110b20..4b450f68 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BackupService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BackupService.cs @@ -92,15 +92,7 @@ public class BackupService return; } - // Make sure we don't back up too often by using a configurable Cooldown - var currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); - if (currentTimestamp < LastBackupTimestamp + BackupConfig.BackupCooldown) - { - return; - } - LastBackupTimestamp = currentTimestamp; - - // If the backup lock is already locked, skip backup. This is to stop TOC/TOU race conditions above + // If the backup lock is already locked, skip backup. This stops multiple backups running at once // Passing 0 is a non-blocking Wait, will return false if the lock can't be acquired bool lockAcquired = await BackupLock.WaitAsync(0); if (!lockAcquired) @@ -110,6 +102,14 @@ public class BackupService try { + // Make sure we don't back up too often by using a configurable Cooldown + var currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (currentTimestamp < LastBackupTimestamp + BackupConfig.BackupCooldown) + { + return; + } + LastBackupTimestamp = currentTimestamp; + var targetDir = GenerateBackupTargetDir(); // Fetch all profiles in the profile directory.