jazzband / django-silk

Silky smooth profiling for Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in tests when using silk - transaction is aborted

victoraugusto6 opened this issue · comments

Hello guys. I have a test that when I add Silk's MIDDLEWARE this error appears:
django.db.utils.InternalError: current transaction is aborted, commands ignored until end of transaction block

Strange that when I remove the MIDDLEWARE the error stops. Does anyone know what can it be?

Activating the middleware means you add the mechanic of intercepting SQL queries, you can paste the whole exception to get more info.

@SebCorbin, thank you for your reply.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3433, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-2c7d1e4d2214>", line 1, in <module>
    simple_user_2.profile.save()
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 806, in save
    self.save_base(
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 857, in save_base
    updated = self._save_table(
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 970, in _save_table
    updated = self._do_update(
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 1034, in _do_update
    return filtered._update(values) > 0
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 885, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1783, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/silk/sql.py", line 95, in execute_sql
    query_dict['analysis'] = _explain_query(self.connection, q, params)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/silk/sql.py", line 57, in _explain_query
    cur.execute(prefixed_query, params)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/victor/Documents/Projects/Irricontrol/plataforma-django/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.InternalError: current transaction is aborted, commands ignored until end of transaction block

I would advise you to only add silk's middleware when DEBUG = True so that it does not interfere with your tests

@SebCorbin Yes, I'm doing it.
image

Then your tests should not be run with DEBUG = True as per https://docs.djangoproject.com/en/4.1/topics/testing/overview/#other-test-conditions so please double check you test configuration

i have same issue
when i add silk in debug True, i get error.

This can happen with atomic requests or any transaction started, overall it's just a question of your code having raised an exception, and after the debug view is being rendered, Silk tries to commit info into the database, triggering this error. I wonder if we can detect that in the middleware...