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

Disable session.remove on flask_sqlalchemy.SQLAlchemy #18

Open
janbuchar opened this issue May 21, 2019 · 1 comment
Open

Disable session.remove on flask_sqlalchemy.SQLAlchemy #18

janbuchar opened this issue May 21, 2019 · 1 comment

Comments

@janbuchar
Copy link

With Flask-SQLAlchemy 2.4.0, SQLAlchemy 1.3.3 and pytest-flask-sqlalchemy 1.0.2, I sometimes (non-deterministically) got DetachedInstanceError because the session instantiated in the flask_sqlalchemy.SQLAlchemy extension already terminated the session (https://github.com/pallets/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L849).

Adding something like this to conftest.py helped.

mocker.patch.object(sqla.session, "remove", lambda: None)
@jeancochrane
Copy link
Owner

Sorry it took me so long to get to this @Teyras! That seems like a pretty nasty race condition. I'd welcome a PR making this change if you've had some success with it. We'll have to be careful to preserve the remove() function somewhere, since we use it ourselves to tear down the session:

@request.addfinalizer
def teardown_transaction():
# Delete the session
session.remove()

But we should probably be able to get away with preserving the method the same way we do close(), commit(), and rollback():

# Preserve close/commit/rollback methods
connection.connection.force_close = connection.connection.close
connection.connection.force_commit = connection.connection.commit
connection.connection.force_rollback = connection.connection.rollback

testeddoughnut added a commit to objectrocket/opsy that referenced this issue Oct 4, 2019
This adds a quick workaround for the issues with the inventory tests
that we've been running into. This should be reverted once this
issues is fixed:
jeancochrane/pytest-flask-sqlalchemy#18

Signed-off-by: M. David Bennett <mdavidbennett@syntheticworks.com>
testeddoughnut added a commit to objectrocket/opsy that referenced this issue Oct 4, 2019
This adds a quick workaround for the issues with the inventory tests
that we've been running into. This should be reverted once this
issues is fixed:
jeancochrane/pytest-flask-sqlalchemy#18

Signed-off-by: M. David Bennett <mdavidbennett@syntheticworks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants