Opened 3 weeks ago

Closed 3 weeks ago

#36942 closed Bug (needsinfo)

SimpleTestCase._remove_databases_failures() is not idempotent and crashes on complex MRO.

Reported by: Michele Fiori Owned by: michele0303
Component: Testing framework Version: 6.0
Severity: Normal Keywords:
Cc: Michele Fiori Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michele Fiori)

When using complex test inheritance (e.g., mixing multiple LiveServerTestCase subclasses or dynamic test class generation), the cleanup method
_remove_databases_failures can be called in a state where database connection methods have already been unwrapped or were never wrapped for a specific alias.

Currently, the code assumes the wrapped attribute always exists: setattr(connection, name, method.wrapped)

This leads to an AttributeError: 'function' object has no attribute 'wrapped'.

The proposed fix adds a defensive hasattr(method, "wrapped") check to make the teardown process idempotent and robust against complex class hierarchies.

PR: https://github.com/django/django/pull/20758

Attachments (1)

check_mro.py (705 bytes ) - added by Michele Fiori 3 weeks ago.

Download all attachments as: .zip

Change History (3)

by Michele Fiori, 3 weeks ago

Attachment: check_mro.py added

comment:1 by Michele Fiori, 3 weeks ago

Description: modified (diff)

comment:2 by Jacob Walls, 3 weeks ago

Needs tests: set
Resolution: needsinfo
Status: assignedclosed

Hi Michele,

Thanks for the report. Seems reasonable, but without a example to play with it's a little hard to QA it. Could you provide an example class hierarchy that produces the issue? Happy to take another look then.

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