Add atomic transaction around social_auth creation. #770
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.
Since Django 1.6, a
transaction.atomic
decorator has been available. There's a pattern that's supported in python_social_auth that performs a Django ORM operation and then handles a possible IntegrityError raised by the operation - see here:https://github.com/omab/python-social-auth/blob/master/social/pipeline/social_auth.py#L40
This pattern is even documented in the Django docs:
https://docs.djangoproject.com/en/1.6/topics/db/transactions/#django.db.transaction.atomic
However, if an IntegrityError is raised without being in a
transaction.atomic
block (via decorator or context manager), the transaction itself is left in a "broken" state - and any subsequent transaction operations will fail with the following exception:An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
This condition is not detected in the current unittest suite because the IntegrityError scenario is tested by using a simple raise with no actual DB transaction:
python-social-auth/social/tests/test_pipeline.py
Line 21 in aa66b83
This PR attempts to fix the broken transaction scenario. I've tested it with our https://github.com/edx/edx-platform test suite and it fixes the broken transaction. As a workaround for our Django 1.8 upgrade, I've monkey-patched edx-platform to fix the issue - that commit can be seen here:
edx/edx-platform@699ae37
The fix in this PR may not be the preferred one - but it does at least fix the issue. I'm happy to provide more information if needed.