fix(service): serialize RepopulateUsers with an asyncio lock#44
Open
lArtiquel wants to merge 1 commit into
Open
fix(service): serialize RepopulateUsers with an asyncio lock#44lArtiquel wants to merge 1 commit into
lArtiquel wants to merge 1 commit into
Conversation
RepopulateUsers reads the full user set, updates every user, then removes any storage user not in the incoming set — all without serialization. SyncUsers and other streams mutate the same xray user table concurrently, so when the panel opens overlapping streams (its normal reconnect behavior) two RepopulateUsers passes can interleave: one adds a user the other is midway through removing, or both race the add/remove of the same email. The observable result is duplicate-add errors and users flapping in and out until a pass happens to win cleanly. Guard the whole RepopulateUsers body with a per-service asyncio.Lock so passes run one at a time. This is complementary to making add_user idempotent: the lock removes the interleaving, the idempotent add absorbs the residual duplicate-add cases (reconnects, SyncUsers overlap).
|
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.



Problem
RepopulateUsersreads the full user set, updates every user, then removes any storage user not in the incoming set — all without serialization:SyncUsersand other streams mutate the same xray user table concurrently. When the panel opens overlapping streams (its normal reconnect behavior), twoRepopulateUserspasses can interleave: one adds a user the other is midway through removing, or both race the add/remove of the same email. The observable result is duplicate-add errors and users flapping in and out until a pass happens to win cleanly.Fix
Guard the whole
RepopulateUsersbody with a per-serviceasyncio.Lockso passes run one at a time.asynciois stdlib; no new dependency.Testing
python -m py_compileclean.Note
Companion to #43 (make
add_useridempotent). This PR removes the interleaving; #43 absorbs the residual duplicate-add cases (reconnects, SyncUsers/RepopulateUsers overlap). Complementary but each stands alone.@khodedawsh @erfjab — review appreciated whenever you have time. If you'd rather serialize at the storage layer than in the service, happy to rework it that way.