jazzband / django-silk

Silky smooth profiling for Django
MIT License
4.35k stars 333 forks source link

Error in tests when using silk - transaction is aborted #628

Open victoraugusto6 opened 1 year ago

victoraugusto6 commented 1 year ago

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?

SebCorbin commented 1 year ago

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

victoraugusto6 commented 1 year ago

@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
victoraugusto6 commented 1 year ago

image

image

SebCorbin commented 1 year ago

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

victoraugusto6 commented 1 year ago

@SebCorbin Yes, I'm doing it. image

SebCorbin commented 1 year ago

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

mositafan10 commented 5 months ago

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

SebCorbin commented 5 months ago

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...