New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ensure_for_user create IntegrityError race condition #6
Comments
|
psagers
added a commit
that referenced
this issue
Feb 10, 2021
What do you think about #7? |
Agreed - yep #7 looks good to me! |
Version 1.0.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi - we are experiencing a race condition where concurrent requests can cause
user.agentsettings = self.create(user=user)
to throw an integrity error due to the user ID already existing in the database.This shows up in our sentry error logs as follows:
duplicate key value violates unique constraint "django_agent_trust_agentsettings_user_id_key"
DETAIL: Key (user_id)=(1) already exists.
This was caused by the following commit which removed the get_or_create
71de1ac#diff-36c097cacb670d42993df6c72f8f2aae021eb52fb353e9cab14ca71f2afb54ac
#2
Timeline of this exception occurring as follows:
I see two options to address this.
def ensure_for_user(self, user): """ Loads a user's AgentSettings instance, creating a default if necessary. """ try: user.agentsettings except self.model.DoesNotExist: try: user.agentsettings = self.create(user=user) except IntegrityError: pass
The text was updated successfully, but these errors were encountered: