omab / python-social-auth

Social auth made simple
http://psa.matiasaguirre.net
BSD 3-Clause "New" or "Revised" License
2.83k stars 1.09k forks source link

No longer working with flask_sqlalchemy #945

Closed aoghina closed 7 years ago

aoghina commented 8 years ago

Probably since this commit, python-social-auth no longer works with flask_sqlalchemy:

https://github.com/omab/python-social-auth/commit/cc5908675c95944b68aae038700c7ee901aff491#diff-a622acca45e0e3a8d67139fe21cd35fe

If we pass the db.session (where db is the SQLAlchemy object) instead of db_session to init_social(app, db_session), which is a scoped session, things seem to work fine until python-social-auth has to add a new user, when it timeouts with Lock wait timeout exceeded; try restarting transaction on the INSERT INTO social_auth_usersocialauth statement.

It has something to do with the sequence of flushing and committing, because if we do cls._session().commit() instead of cls._flush() in SQLAlchemyMixin._save_instance, then it works fine.

aoghina commented 8 years ago

A workaround is to do this:

db = SQLAlchemy()

def __bogus_flush():
    raise AssertionError

db.session.flush = __bogus_flush
init_social(app, db.session)

It works because if flush() raises and AssertionError, then python-social-auth does commit.

aoghina commented 8 years ago

Actually this seems to be the offending commit: https://github.com/omab/python-social-auth/commit/a52ee69cc641018bbf32c6fd599a6c30036b3086