Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#33616 closed New feature (fixed)

Supporting robust on_commit handlers.

Reported by: Josh Smeaton Owned by: Abhinav Yadav
Component: Database layer (models, ORM) Version: 4.0
Severity: Normal Keywords:
Cc: Adam Johnson, David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Josh Smeaton)

I recently tracked down an issue in my application where some on_commit handlers didn't execute because one of the previous handlers raised an exception. There appears to be no way to execute on_commit handlers *robustly* as you're able to do with signals [0] using send_robust.

I could sprinkle try/catches around the place, but I'd like to avoid doing so because not all functions that are used as handlers should always swallow exceptions, but could do so when run as on_commit handlers.

Targeting which handlers can be robust or not would be really useful, for example:

def update_search(user):
    # if updating search fails, it's fine, we'll bulk update later anyway
    transaction.on_commit(lambda: search.update(user), robust=True)

def trigger_background_task_one(user):
    # if this task fails, we want to crash
    transaction.on_commit(lambda: mytask.delay(user_id=user.id))

Here if search fails to update it doesn't prevent the background task from being scheduled.

I'm proposing to add a robust kwarg that defaults to False, for backward compatibility, but allows a user to tag specific handlers as such.

[0] https://docs.djangoproject.com/en/4.0/topics/signals/#sending-signals

Change History (21)

comment:1 by Josh Smeaton, 3 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 3 years ago

Cc: Adam Johnson added
Component: UncategorizedDatabase layer (models, ORM)
Summary: Supporting robust on_commit handlersSupporting robust on_commit handlers.
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

Sounds reasonable. Please take into account that the current behavior is documented.

comment:3 by Mariusz Felisiak, 3 years ago

Josh, Would you like to prepare a patch?

comment:4 by Josh Smeaton, 3 years ago

I haven't got the time to put a patch together *right now* but I could do so in the near future. Consider tagging this as "easy pickings" for a budding contributor?

comment:5 by Josh Smeaton, 3 years ago

Easy pickings: set

comment:6 by Josh Smeaton, 3 years ago

I've started an easy pickings thread on -developers ML and would be happy to review and guide someone to make the change: https://groups.google.com/g/django-developers/c/Hyqd1Rz6cFs

comment:7 by Adam Johnson, 3 years ago

Good feature suggestion. The execution in captureOnCommitCallbacks would need extending too :)

comment:8 by Dulalet, 3 years ago

Owner: changed from nobody to Dulalet
Status: newassigned

comment:9 by Mariusz Felisiak, 3 years ago

Has patch: set
Needs documentation: set
Needs tests: set

comment:10 by Shivan Sivakumaran, 3 years ago

Can this ticket be closed? Seems like the PR was accepted.

in reply to:  10 comment:11 by Josh Smeaton, 3 years ago

Replying to Shivan Sivakumaran:

Can this ticket be closed? Seems like the PR was accepted.

Not until the PR is merged, then it'll be closed as fixed

comment:12 by David Wobrock, 3 years ago

Cc: David Wobrock added

comment:13 by Abhinav Yadav, 3 years ago

Owner: changed from Dulalet to Abhinav Yadav

comment:14 by Sarah Abderemane, 3 years ago

comment:15 by Abhinav Yadav, 2 years ago

Needs tests: unset

comment:16 by David Wobrock, 2 years ago

Needs tests: set
Patch needs improvement: set

comment:17 by David Wobrock, 2 years ago

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:18 by Mariusz Felisiak, 2 years ago

Needs tests: set
Patch needs improvement: set

comment:19 by Mariusz Felisiak, 2 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:20 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 4a1150b4:

Fixed #33616 -- Allowed registering callbacks that can fail in transaction.on_commit().

Thanks David Wobrock and Mariusz Felisiak for reviews.

comment:21 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

In 3a08483:

Refs #33616 -- Updated BaseDatabaseWrapper.run_on_commit comment.

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