fix(sync): handle 'already exists' race condition in try_create#127
Open
berlikm wants to merge 1 commit into
Open
fix(sync): handle 'already exists' race condition in try_create#127berlikm wants to merge 1 commit into
berlikm wants to merge 1 commit into
Conversation
When multiple RQ workers sync devices concurrently, two jobs can both pass find_by_name() (returns empty), then both try to create the same hostgroup/template/tag. Zabbix rejects the second with "already exists". Catch that error and retry via find_by_name() instead of propagating the RuntimeError.
berlikm
force-pushed
the
fix/sync-already-exists-race
branch
from
July 16, 2026 07:34
395b4f7 to
f6ceca2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When multiple RQ workers sync devices concurrently, two jobs can both pass (returns empty), then both call for the same hostgroup/template/tag. Zabbix rejects the second with
"already exists". This propagates as aRuntimeErrorand aborts the entire host sync — including theHostSyncitself, leaving a host with no interfaces, no templates, and no tags.Root cause
ZabbixSyncBase.try_create()callsapi_object().create()without catching the case where the object was created by a concurrent job in the gap betweenfind_by_name()andcreate().Fix
Catch the
"already exists"error intry_create()and retry viafind_by_name()to recover the existing ID:Testing
Verified on dev NetBox + Zabbix 7.0.28 with 173 concurrent device/VM sync jobs (HU + JP sites). Before the fix,
HostGroupSyncfailures cascaded toHostSync, leaving hosts with zero interfaces. After the fix, all concurrent creates resolve cleanly — 0 failures from race conditions.The fix is generic — it applies to all sync classes (
HostGroupSync,TemplateSync,ProxySync, etc.) since it lives in the base class.