Skip to content

Commit

Permalink
Merge pull request #975 from Superpedestrian/feature/user_create_race
Browse files Browse the repository at this point in the history
Added exception handling for user creation race condition in Django
  • Loading branch information
omab authored Aug 6, 2016
2 parents 5bea35c + 9f86059 commit 11c152b
Showing 1 changed file with 16 additions and 1 deletion.
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

0 comments on commit 11c152b

Please sign in to comment.