Skip to content
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

Added exception handling for user creation race condition in Django #975

Merged
merged 1 commit into from
Aug 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion social/storage/django_orm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Django ORM models for Social Auth"""
import base64
import six
import sys
from django.db import transaction
from django.db.utils import IntegrityError

from social.storage.base import UserMixin, AssociationMixin, NonceMixin, \
CodeMixin, BaseStorage
Expand Down Expand Up @@ -58,7 +60,20 @@ def create_user(cls, *args, **kwargs):
username_field = cls.username_field()
if 'username' in kwargs and username_field not in kwargs:
kwargs[username_field] = kwargs.pop('username')
return cls.user_model().objects.create_user(*args, **kwargs)
try:
user = cls.user_model().objects.create_user(*args, **kwargs)
except IntegrityError:
# User might have been created on a different thread, try and find them.
# If we don't, re-raise the IntegrityError.
exc_info = sys.exc_info()
# If email comes in as None it won't get found in the get
if kwargs.get('email', True) is None:
kwargs['email'] = ''
try:
user = cls.user_model().objects.get(*args, **kwargs)
except cls.user_model().DoesNotExist:
six.reraise(*exc_info)
return user

@classmethod
def get_user(cls, pk=None, **kwargs):
Expand Down