0

I have a custom signal and there is a classmethod which updates some objects where I need this signal to be disconnected so changes are not synced to pipedrive because this method is used only by a view for webhooks.

@classmethod
def backsync_all_stages(cls):
    s = sync_object.disconnect(sync_object_receiver)
    stages_json = PipeDriveWrapper.get_all_stages()
    if stages_json.get('success'):

And there is a view which calls this method if request is POST:

def post(self,request,*args,**kwargs):
    data = json.loads(request.body)
    model_alias = data['meta']['object']
    Model = PipedriveSync.WEBHOOK_MODEL_MAP[model_alias]
    if Model == LeadStage:
        PipedriveSync.backsync_all_stages()
    ...

The problem is that sometimes it disconnects the signal and sometimes it doesn't. When I call PipedriveSync.backsync_all_stages() inside shell_plus, it works correctly.

But according to view - I see in debugger that in both cases, the method is called from the same view. I absolutely don't understand what is happening.

So I invoke the webhook and set breakpoint under the disconnect command so you can see it returns True and traceback.

So invoking the webhook and waiting...

enter image description here

You can clearly see that the method is called from view and that signal wasn't disconnected (returned False).

Meanwhile there was probably a couple of another posts since the webhook sends multiple requests. So I switch to Thread-8 in debugger. There I see that signal was disconnected properly.

enter image description here

The traceback is the same so why it's once True and another time False? This problem causes cycling through Webhook -> Client -> Webhook -> Client....

I noticed that everytime I call this method through shell_plus, the signal is correctly disconnected.

Do you have any ideas?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • Your mention of threads leads to the suspicion that this is a thread-safety issue. How exactly are you running Django? How are you starting those threads? – Daniel Roseman Mar 19 '19 at 18:20
  • @DanielRoseman It's runserver in debug mode in Pycharm Configuration. The thread with True value was some special case, I think the most weird thing is that shell_plus disconnects signal and the view don't. – Milano Mar 19 '19 at 18:31

0 Answers0