Skip to content

ref: --reuse-db to avoid potential issues with pytest database teardown at session-end#106999

Merged
joshuarli merged 1 commit intomasterfrom
ref-reuse-db-to-skip-pytest-teardown
Jan 27, 2026
Merged

ref: --reuse-db to avoid potential issues with pytest database teardown at session-end#106999
joshuarli merged 1 commit intomasterfrom
ref-reuse-db-to-skip-pytest-teardown

Conversation

@joshuarli
Copy link
Member

@joshuarli joshuarli commented Jan 26, 2026

we've been having some maybe-test-pollution-related issues (example https://github.com/getsentry/sentry/actions/runs/20912887952/job/60079407114 but tests/acceptance/test_trace_view_waterfall.py::TraceViewWaterfallTest::test_trace_view_waterfall_loads is a known flake) tearing down pytest-django's test db:

src/sentry/db/postgres/base.py:96: in execute
    return self.cursor.execute(sql)
E   django.db.utils.OperationalError: database "test_region" is being accessed by other users
E   DETAIL:  There are 2 other sessions using the database.
E   
E   SQL: DROP DATABASE "test_region"

During handling of the above exception, another exception occurred:
.venv/lib/python3.13/site-packages/pytest_django/fixtures.py:157: in django_db_setup
    request.node.warn(
E   pytest.PytestWarning: Error when trying to teardown test databases: OperationalError('database "test_region" is being accessed by other users\nDETAIL:  There are 2 other sessions using the database.\n')

--reuse-db is the only reasonable way i've found to skip db teardown at pytest session-end, and would prevent this entire class of errors

since our test shards are just one pytest invocation (and therefore one pytest test session) this is safe to do

@joshuarli joshuarli requested review from a team as code owners January 26, 2026 21:13
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 26, 2026
@joshuarli joshuarli changed the title ref: --reuse-db ref: --reuse-db to avoid potential issues with pytest database teardown at session-end Jan 26, 2026
Copy link
Member

@hubertdeng123 hubertdeng123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like data is preserved in the database across test runs with this approach after reading. CI passed though, so guess that isn't a problem?

@joshuarli
Copy link
Member Author

It seems like data is preserved in the database across test runs with this approach after reading. CI passed though, so guess that isn't a problem?

It's preserved across pytest invocations but each testing shard is one pytest invocation.

@hubertdeng123
Copy link
Member

My point is to my understanding it should be best practice to clear data in the db between test runs to ensure the tests are stateless. This seems to not be aligned with this, so wondering if that's really the case. It seems like the root cause here may be different threads accessing the db at the same time.

@hubertdeng123
Copy link
Member

Unless you're saying that since pytest doesn't teardown the db until end of session entirely, and just rolls back transactions between test runs. Then, we'll just be skipping db teardown entirely

@joshuarli
Copy link
Member Author

joshuarli commented Jan 27, 2026

The root cause, which this PR is working around, is that pytest-django is trying to drop the database at session-end but there are still open db connections.

That is a problem with the tests themselves but I'm saying it's fine to paper over because these problems (which are not tied to test incorrectness) come up at db teardown at session-end, and there is only 1 session-end.

--reuse-db is mainly used locally if you want repeated pytest runs to be faster, but can also be used in CI to skip over session-end db teardown.

@joshuarli joshuarli merged commit c513290 into master Jan 27, 2026
69 checks passed
@joshuarli joshuarli deleted the ref-reuse-db-to-skip-pytest-teardown branch January 27, 2026 19:40
priscilawebdev pushed a commit that referenced this pull request Feb 2, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants