[TikiWiki-commits] [Git][tikiwiki/tiki][27.x] [FIX] AutoIncrement race condition: try to resolve duplicate values when...

"Victor Emanouilov \(@kroky\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <699c2baebd05c_3b233b98494954@gitlab-sidekiq-low-urgency-cpu-bound-v2-ccd878489-4s5fc.mail>

Victor Emanouilov pushed to branch 27.x at Tiki Wiki CMS Groupware / Tiki


Commits:
20cec3b1 by Victor Emanouilov at 2026-02-23T12:27:52+02:00
[FIX] AutoIncrement race condition: try to resolve duplicate values when records are created at the same time

- - - - -


1 changed file:

- lib/core/Tracker/Field/AutoIncrement.php


Changes:

=====================================
lib/core/Tracker/Field/AutoIncrement.php
=====================================
@@ -137,6 +137,51 @@ class Tracker_Field_AutoIncrement extends \Tracker\Field\AbstractItemField imple
         ];
     }
 
+    /**
+     * After the item is saved, check for duplicate auto-increment values that
+     * can occur when concurrent processes both read the same maximum value
+     * before either has persisted its result. If a duplicate is found,
+     * re-read the current maximum and assign a new unique value.
+     */
+    public function postSaveHook($value)
+    {
+        // itemId mode uses the actual DB item ID — duplicates are impossible
+        if ($this->getOption('itemId') == 'itemId') {
+            return;
+        }
+
+        $itemId = $this->getItemId();
+        $fieldId = $this->getConfiguration('fieldId');
+
+        if (! $itemId || $value === false || $value === null || $value === '') {
+            return;
+        }
+
+        global $prefs;
+        $trklib = TikiLib::lib('trk');
+        $table = TikiDb::get()->table('tiki_tracker_item_fields');
+
+        $maxRetries = 3;
+        for ($i = 0; $i < $maxRetries; $i++) {
+            $duplicateCount = $table->fetchCount([
+                'fieldId' => (int)$fieldId,
+                'value' => (string)$value,
+            ]);
+
+            if ($duplicateCount <= 1) {
+                break;
+            }
+
+            $maxValue = $trklib->get_maximum_value($fieldId);
+            if ($prefs['tracker_autoincrement_resettable'] == 'y') {
+                $value = max($maxValue + 1, $this->getOption('start', 1));
+            } else {
+                $value = $maxValue + 1;
+            }
+            $trklib->modify_field($itemId, $fieldId, $value);
+        }
+    }
+
     public function getTabularSchema()
     {
         $schema = new Tracker\Tabular\Schema($this->getTrackerDefinition());



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/20cec3b166a8af13af717333958c03d15a14c4e9

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/20cec3b166a8af13af717333958c03d15a14c4e9
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.