Opened 4 years ago

Closed 4 years ago

#32248 closed Bug (invalid)

QuerySet.get() leaves postgres in 'current transaction is aborted...' state if invalid data is passed.

Reported by: Asit Kumar Singh Owned by: Nayan sharma
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords: postgres transaction_aborted get_object_or_404
Cc: Asit Kumar Singh Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

calling get_object_or_404() with invalid data (e.g. invalid UUID) in any of the fields causes Postgres to stay in the same transaction. On Postgres, this leads to the failure of any new query using the same connection. The reason for this is Postgres on getting invalid input doesn't rollback the transaction automatically and neither does Django explicitly rollbacks such get calls, which leaves the database in an unstable state.
Example code:

     def invalid_data(model):
          try:
                 obj=get_object_or_404(model, id=f"{invalid_data}")
          except:
                 # do something
         model.objects.first().field # this will fail with current transaction is aborted error.
          

Change History (4)

comment:1 by Asit Kumar Singh, 4 years ago

Cc: Asit Kumar Singh added

comment:2 by Nayan sharma, 4 years ago

Owner: changed from nobody to Nayan sharma
Status: newassigned

comment:3 by Nayan sharma, 4 years ago

can anyone help me through this as this is going to be my first contribution to Django.

comment:4 by Mariusz Felisiak, 4 years ago

Component: UtilitiesDatabase layer (models, ORM)
Easy pickings: unset
Resolution: invalid
Status: assignedclosed
Summary: get_object_or_404() leaves postgres in 'current transaction is aborted...' state if invalid data is passed as input to the get_object_or_404.QuerySet.get() leaves postgres in 'current transaction is aborted...' state if invalid data is passed.

get_object_or_404() calls QuerySet.get() without anything fancy, so it's not related with its implementation. It looks that you call invalid_data() inside an atomic transaction, in such cases you shouldn't catch exceptions, see "Avoid catching exceptions inside atomic!". If it's not the case, please provide a sample Django 3.1+ project that reproduces this issue (Django 2.2 doesn't receive bugfixes anymore).

Note: See TracTickets for help on using tickets.
Back to Top