Opened 7 months ago

Closed 3 weeks ago

#36487 closed Bug (fixed)

Database on commit error logging fails for partials

Reported by: Krishnaprasad MG Owned by: Krishnaprasad MG
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The on commit handler here: https://github.com/django/django/blob/main/django/db/backends/base/base.py#L763 accepts both callback functions and partials but the error logging has a bug which expects property __qualname__ on the callback method but this doesn't work with partials.

A fix is implemented here: https://github.com/django/django/pull/19609

Change History (8)

comment:1 by Simon Charette, 7 months ago

Has patch: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

There is effectively a bug here as not all callable will have a __qualname__. Maybe we could simply name = getattr(func, "__qualname__", func) instead?

comment:2 by Krishnaprasad MG, 7 months ago

This also can be done, the current fix effectively prints the wrapped function name in case of partial, but yes may be this is fine (copied from the tests)

def robust_callback():
raise ForcedError("robust callback")

robust_callback_partial = partial(robust_callback)

>>> getattr(robust_callback_partial, "__qualname__", robust_callback_partial)
functools.partial(<function robust_callback at 0x101287600>)
>>> getattr(robust_callback, "__qualname__", robust_callback)
'robust_callback'

I could update the PR

Version 0, edited 7 months ago by Krishnaprasad MG (next)

comment:3 by Krishnaprasad MG, 6 months ago

One more possible improvement would be to replace the f-string with %s placeholder in log messages if that makes sense

logger.exception(f"Error calling {name} in on_commit() (%s).", e) to logger.exception("Error calling %s in on_commit() (%s).", name, e)

because this helps log collecting systems like Sentry to effectively aggregate log messages without creating separate log messages. Also mentioned in google style guide: https://google.github.io/styleguide/pyguide.html#3101-logging

comment:4 by Jacob Walls, 4 months ago

Patch needs improvement: unset

comment:5 by Krishnaprasad MG, 4 weeks ago

I have updated the PR. The logging improvements (switching to %s placeholders) are now addressed. Tests are passing on the PR.

comment:6 by Jacob Walls, 4 weeks ago

Needs tests: set

Very close -- just asking to evaluate the feasibility of a third test.

comment:7 by Jacob Walls, 3 weeks ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Jacob Walls <jacobtylerwalls@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 7a2f35b:

Fixed #36487 -- Fixed logger error message with partial callbacks.

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