Opened 16 years ago

Last modified 13 years ago

#11134 closed

Signals don't work properly when they are being disconnected during their processing — at Version 2

Reported by: Honza Král Owned by: Honza Král
Component: Uncategorized Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gary Wilson)

This happens because disconnect() method does del self.receivers[index] while self.receivers are being iterated over in _live_receivers().

The attached patch has tests and a fix, the tests fail for me without the fix, passes after the fix is applied.

Change History (3)

by Honza Král, 16 years ago

Attachment: 11134-against-10791.diff added

comment:1 by Honza Král, 16 years ago

milestone: 1.1
Owner: changed from nobody to Honza Král

Bugs shouold be fixed before release and this is simple and very isolated.

comment:2 by Gary Wilson, 16 years ago

Description: modified (diff)

Honza, I've got a few questions about the use case you mentioned in IRC:

"The use case is that we have a FormField, that stores text in markup in external Model, and passes rendered text in its clean, then registers the post_save hook to save the markup somewhere to enable later editing."

You mention that, as shown in the attached tests, that the second receiver doesn't get run or removed and that this causes problems for the next instance that gets saved.

Well, don't you still have a problem if some other thread saves a different instance of the Model before save gets called on the target instance? In this case, the receivers would get called on the different instance, get removed from the post_save hook, then not get called when the target instance is saved. Or do you not care to be thread safe?

Also, what happens if this custom field cleans fine, but the form still had errors in other fields. In this case the receivers are still connected and could cause havoc on the next instance saved.

If all you are doing is registering receivers and then removing them, could you not simply call the receiver functions after you save the instance and not hook them up to the signal at all? For example, in a post_save method of the field that the form calls after successful validation.

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