How to to debug DB errors in flaky frontend tests with django, htmx and playwright

I'm running a larger Django app that has a test suite that runs both on Debian Bookworm and Trixie. The tests use the test_server fixture and sqlite. We're currently experimenting with HTMX based views, which we test using playwright.

A few weeks ago the test covering our new HTMX view started occasionally failing, but only ever on Trixie. The only relevant packages provided by the Debian Trixie host are sqlite 3.46 and python 3.13. Everything else comes in via pip and pyproject.toml in the same versions as on Bookworm.

The test does not fail reliably and when it does fail, it doesn't always fail with the same error. The only commonality is that it always fails while trying to get the current session, in order to authenticate the request user (the tested view has an auth decorator).

The error messages include

  • django.contrib.auth.models.User.MultipleObjectsReturned: get() returned more than one User -- it returned 2!
  • django/db/models/sql/compiler.py stepping out with List index out of range when trying to load the session cache
  • django.db.utils.InterfaceError: bad parameter or other API misuse when trying to get the session cache.

They mostly end up with AttributeError: 'SessionStore' object has no attribute '_session_cache' but there can be different issues causing the retrieval of the session cache to fail.

The way I see it, there is something going wrong with the database here. I am using tox and pytest, the database is reset for every test, the test suite has about two hundred other tests, all of which run fine.

So, all I know is there is an issue that only occurs when I use playwright to test a frontend that loads content using HTMX and only when I do so on Debian Trixie. When it happens, it's always connected to Django wanting to retrieve session data and the database failing to provide them in some way.

I suspect a good next step would be to get more information on what that database does during the tests, or what errors it runs into, but I'm not sure how to get that info.

I'm aware this is a horribly convoluted issue, but I'm kind of at my wits end with it.

Would be very grateful for any hint or tips on what I could do to get more info to somehow get to the bottom of this.

Back to Top