Django Signal stuck in loop

Viewed 453

I have 3 signal functions and one is getting stuck in a loop when it is called. It is called when an object inside of a model class is updated on the Django Admin page...

# models.py
class Item(models.Model):
    ...
    foo = models.BooleanField(
        default=False,
        editable=True,
        blank=True,
        help_text='Blah Blah...'    
    )
# signals.py
@receiver(pre_save, sender=Item)
def foobar(sender, instance, **kwargs):
    try:
        pass
    except:
        pass

I literally put pass in each try and except with a call to a log just to see if it is repeating even without code in them, and it is. Constantly with no end. This happens using an if / else statement, too, instead of try / except.

How the heck can I stop the signal from getting stuck in a loop? It is supposed to send an email each time, but now when the object is updated, the user gets blasted with infinite emails until I reboot Django.

I am really at a loss here...

BTW, this doesn't happen on my test server, only on my live/staging server.

EDIT:

If you are viewing this in the future, this is how I debugged my issue.

Look for anything that is running/hitting your server and potentially sparking a signal to run. For me, it was a specific object that was set as the sender for a signal to run, and each time the webhook I was using was causing a function to run, which was modifying the specific sender object.

1 Answers

So I am hesitant to post this as an "Answer," as this potentially only gives reason to my own issue here, but I have figured out what is going on...

I recently connected a Webhook to my Django app, one that is constantly hitting it and updating information... I have fixed my webhook function to only hit the Model Object (above, it is Class Item) so that it lowers the number of times the signals run.

However, this is the literal fix for the signal running to completion:

# signals.py
...

obj = sender.objects.get(pk=instance.pk)

# We are checking if the instance value is different than what the object value
# is. Remember, this is PRE_SAVE, so the object should be different than the
# instance
if instance.variable and not obj.variable:
    ...

...
Related