A free open source IT asset/license management system
Fixed #19744 - Better handle concurrent auto-increments
snipe6cb1cb422cfc
diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.phpindex 50dca65e90..d84dd33c61 100644--- a/app/Observers/AssetObserver.php+++ b/app/Observers/AssetObserver.php@@ -4,13 +4,13 @@ namespace App\Observers;use App\Models\Actionlog;use App\Models\Asset;+use App\Models\CalendarEvent;+use App\Models\CheckoutRequest;use App\Models\Order;use App\Models\OrderItem;use App\Models\Setting;use Carbon\Carbon;use Illuminate\Support\Facades\DB;-use App\Models\CalendarEvent;-use App\Models\CheckoutRequest;class AssetObserver{@@ -97,22 +97,30 @@ class AssetObserver// IF - auto_increment_assets is on, AND (there is no prefix OR the prefix matches the start of the tag)// AND the rest of the string after the prefix is all digits, THEN...if ($settings->auto_increment_assets && ($prefix == '' || strpos($tag, $prefix) === 0) && preg_match('/\d+/', $number) === 1) {- // new way of auto-trueing-up auto_increment ID's+ // intval() rather than cast because $number could be '01234'+ // which would otherwise be read as octal$next_asset_tag = intval($number, 10) + 1;- // we had to use 'intval' because the $number could be '01234' and- // might get interpreted in Octal instead of decimal-- // only modify the 'next' one if it's *bigger* than the stored base- //- if ($next_asset_tag > $settings->next_auto_tag_base && $next_asset_tag < PHP_INT_MAX) {- $settings->next_auto_tag_base = $next_asset_tag;- $settings->save();- }+ // Server-side UPDATE so two concurrent asset+ // creates can't clobber each other's catch-up write. If another+ // request already pushed next_auto_tag_base past our+ // value between our read and save, the update matches+ // zero rows and we leave their (higher) value in place.+ // PHP_INT_MAX guard matches the pre-#19744 behavior so an+ // asset tag at the ceiling can't push the base to a value+ // that overflows on the next bump.+ if ($next_asset_tag < PHP_INT_MAX) {+ Setting::where('id', $settings->id)+ ->where('next_auto_tag_base', '<', $next_asset_tag)+ ->update(['next_auto_tag_base' => $next_asset_tag]);+ // Resync the in-process $_cache from the DB so callers+ // in this request see the authoritative value, whether+ // the winner was us or a concurrent writer.+ $settings->refresh();+ }} else {- // legacy method+ // Legacy method.$settings->increment('next_auto_tag_base');- $settings->save();}}diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.phpindex 77ae6fda6f..22aa4f5ed1 100644--- a/tests/Unit/AssetTest.php+++ b/tests/Unit/AssetTest.php@@ -160,6 +160,47 @@ class AssetTest extends TestCase$this->assertEquals(Setting::getSettings()->next_auto_tag_base, $matches[0] + 1, "Next auto increment number should be the last normally-saved one plus one, but isn't");}+ public function test_concurrent_asset_create_does_not_regress_next_auto_tag_base()+ {+ // Regression test for issue #19744. Two parallel asset-create+ // requests both read next_auto_tag_base, then both try to write+ // their own catch-up value. If the second writer naively saves,+ // it clobbers the first writer's higher value. The fix (and+ // this test) uses a conditional UPDATE that leaves the higher+ // value alone.+ $this->settings->enableAutoIncrement();++ // Prime the in-process Setting cache so the observer later reads+ // this stale object instead of refetching from the DB.+ $settings = Setting::getSettings();+ $staleBase = $settings->next_auto_tag_base;++ // Simulate a concurrent writer that already won the race and+ // bumped next_auto_tag_base past our stale read. Direct DB+ // update bypasses the Setting-save cache invalidation, which+ // is exactly what a sibling PHP process on the same DB would+ // do from this process's point of view.+ $winnerBase = $staleBase + 500;+ DB::table('settings')->where('id', $settings->id)->update([+ 'next_auto_tag_base' => $winnerBase,+ ]);++ // Create an asset at the stale value. The observer will see+ // the cached $settings (which still thinks the base is+ // $staleBase) and try to catch next_auto_tag_base up to+ // $staleBase + 1.+ $asset = Asset::factory()->create([+ 'asset_tag' => 'ABCD'.$staleBase,+ ]);+ $this->assertModelExists($asset);++ // The DB value must still be the higher value from the+ // simulated concurrent writer, not clobbered down to our+ // smaller catch-up.+ $actual = DB::table('settings')->where('id', $settings->id)->value('next_auto_tag_base');+ $this->assertEquals($winnerBase, $actual, 'next_auto_tag_base was regressed by a stale catch-up write');+ }+public function test_auto_increment_almost_big(){// TODO: this looks pretty close to the one above, could we maybe squish them together?