Boost C++ Libraries: Ticket #9103: execution of slots on deletion of signals https://svn.boost.org/trac10/ticket/9103 <p> This code: </p> <blockquote> <p> struct A { public: </p> <blockquote> <p> A() { </p> <blockquote> <p> sig.connect(0, boost::bind(&amp;A::a, this)); sig.connect(1, &amp;A::b); </p> </blockquote> <p> } void a() {delete this;} static void b() {std::cout &lt;&lt; "hello\n";} boost::signals2::signal&lt;void(void)&gt; sig; </p> </blockquote> <p> }; A *a = new A; a-&gt;sig(); </p> </blockquote> <p> Does not print 'hello'. </p> <p> Yet, the documentation of boost.signals2 states: </p> <p> Signal/slot disconnections occur when any of these conditions occur: </p> <p> (1) The connection is explicitly disconnected via the connection's disconnect method directly, or indirectly via the signal's disconnect method, or scoped_connection's destructor. </p> <p> (2) An object tracked by the slot is destroyed. </p> <p> (3) The signal is destroyed. </p> <p> [Note: we're in case (3)] </p> <p> These events can occur at any time without disrupting a signal's calling sequence. If a signal/slot connection is disconnected at any time during a signal's calling sequence, the calling sequence will still continue but will not invoke the disconnected slot. Additionally, a signal may be destroyed while it is in a calling sequence, and which case it will complete its slot call sequence but may not be accessed directly. </p> <p> Since the slot is deleted in the calling sequence, I expect the calling sequence to continue, and 'hello' should be printed. This behaviour would be consistent with boost.signals (the deprecated version). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9103 Trac 1.4.3 anonymous Mon, 16 Sep 2013 15:25:58 GMT component changed; owner set https://svn.boost.org/trac10/ticket/9103#comment:1 https://svn.boost.org/trac10/ticket/9103#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Frank Mori Hess</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">signals2</span> </li> </ul> Ticket Frank Mori Hess Mon, 16 Sep 2013 19:57:38 GMT <link>https://svn.boost.org/trac10/ticket/9103#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9103#comment:2</guid> <description> <p> Replying to <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9103" title="#9103: Bugs: execution of slots on deletion of signals (closed: fixed)">wim@…</a>: </p> <blockquote class="citation"> <p> These events can occur at any time without disrupting a signal's calling sequence. If a signal/slot connection is disconnected at any time during a signal's calling sequence, the calling sequence will still continue but will not invoke the disconnected slot. Additionally, a signal may be destroyed while it is in a calling sequence, and which case it will complete its slot call sequence but may not be accessed directly. </p> <p> Since the slot is deleted in the calling sequence, I expect the calling sequence to continue, and 'hello' should be printed. This behaviour would be consistent with boost.signals (the deprecated version). </p> </blockquote> <p> The docs are contradictory here. The signal destructor is documented in the reference to disconnect all slots. A disconnection in Signals2 takes effect for all slots which have not yet begun to be executed (even if earlier slots in an invocation have already run when the disconnection happens). This is a different behavior from the original Signals. </p> <p> So the options as I see them are: </p> <ol><li>Delete the sentence describing a signal being destroyed in its calling sequence. Technically, the slot call sequence does complete but since all the slots were disconnected by the signal destructor, no further slots are executed. Also, there should be a paragraph added to the "Porting" section of the documentation noting the changed behavior of disconnect from the original Signals. </li><li>Delete the sentence describing the signal destructor as disconnecting all connected slots. Instead, the slots would be disconnected at some later point once all concurrent signal invocations are complete. It would be possible for a series of overlapping signal invocations (via the weak references to the destroyed signal's implementation used by signals-used-as-slots) to prevent the slots from ever being disconnected. The implementation also adds a slight overhead of once atomic increment and one decrement per signal invocation, for the reference counting to determine when the disconnects should be finally performed. </li><li>Make Signals2 disconnection behave consistently with the original Signals. Every signal invocation would have to make a local copy of the disconnected/blocked state for all the slots before executing any of them, and then ignore any changes that occur during invocation. This may cause issues with existing Signals2 code, and it's not clear to me that this behavior is actually more desirable than the existing behavior. Although, it would ease porting for those who haven't already. It would also be make disconnects more symmetrical with connects, since newly connected slots are ignored by signals2 signal invocations if the invocation has already begun executing slots. </li></ol><p> I'm currently inclined to go with option 1. </p> </description> <category>Ticket</category> </item> <item> <author>wim@…</author> <pubDate>Wed, 18 Sep 2013 16:40:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9103#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9103#comment:3</guid> <description> <p> The question to answer is: how do you want to support signal/slot connections where slots may modify the data structure in such a way that the signalling object is deleted. </p> <p> In Wt (www.webtoolkit.eu) we regularly encounter these 'delete this' cases (directly or indirectly), e.g. in situations where the 'Ok' button of a dialog or a widget causes the dialog or widget to be closed or removed. With signals2, the behaviour changed when multiple slots are connected to such a self terminating signal, which lead to a bug report. In this report, a signal from a single-shot timer was connected to a method that executed the 'delete this', in addition to multiple connections that are handled by other objects that are not affected by this deletion. That's just another example of a self-destructing signal. </p> <p> I'd prefer compatibility with boost.signals is implemented. As you indicate, it makes the signals disconnect behaviour more consistent with the connect case, and it would also make signals2 more compatible with the original signals library. Projects moving from signals to signals2 (because of the official deprecation of the original signals library) will run in this subtle, difficult to debug use case. As I understand it, the boost.signals behaviour is slightly different from what you describe in (3): disconnected slots will not be executed when disconnects occur during the calling sequence. I have no idea what happens with blocked slots. </p> <p> Or can you suggest a work-around (copying the signal to the stack before invoking it?) that would have compatible behaviour? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Frank Mori Hess</dc:creator> <pubDate>Thu, 19 Sep 2013 03:18:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9103#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9103#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9103#comment:3" title="Comment 3">wim@…</a>: </p> <blockquote class="citation"> <p> In Wt (www.webtoolkit.eu) we regularly encounter these 'delete this' cases (directly or indirectly), e.g. in situations where the 'Ok' button of a dialog or a widget causes the dialog or widget to be closed or removed. With signals2, the behaviour changed when multiple slots are connected to such a self terminating signal, which lead to a bug report. In this report, a signal from a single-shot timer was connected to a method that executed the 'delete this', in addition to multiple connections that are handled by other objects that are not affected by this deletion. That's just another example of a self-destructing signal. </p> </blockquote> <p> Thanks for clarifying your use case. I see how the disconnect in the signal destructor is annoying for you. </p> <blockquote class="citation"> <p> As I understand it, the boost.signals behaviour is slightly different from what you describe in (3): disconnected slots will not be executed when disconnects occur during the calling sequence. I have no idea what happens with blocked slots. </p> </blockquote> <p> Thanks for pointing that out. I'll play around with the original Signals library when I get the time, to refresh my memory of its behavior. Maybe the most compatible and easiest change will turn out to be to just drop the call to disconnect_all_slots in the Signals2 signal destructor. </p> <blockquote class="citation"> <p> Or can you suggest a work-around (copying the signal to the stack before invoking it?) that would have compatible behaviour? </p> </blockquote> <p> signals are non-copyable so you can't do that. If your signal was owned by shared_ptr, you could prevent the signal's destruction during invocation by copying an owning shared_ptr to the stack. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Frank Mori Hess</dc:creator> <pubDate>Sun, 22 Sep 2013 21:21:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9103#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9103#comment:5</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85836" title="Don't force disconnection of all slots in signal destructor. Refs #9103 ">[85836]</a>) Don't force disconnection of all slots in signal destructor. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9103" title="#9103: Bugs: execution of slots on deletion of signals (closed: fixed)">#9103</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Frank Mori Hess</dc:creator> <pubDate>Sun, 22 Sep 2013 21:23:57 GMT</pubDate> <title>status, milestone changed https://svn.boost.org/trac10/ticket/9103#comment:6 https://svn.boost.org/trac10/ticket/9103#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> <p> Too late to merge into release for 1.55, change will be in 1.56. </p> Ticket Frank Mori Hess Tue, 12 Nov 2013 03:02:09 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9103#comment:7 https://svn.boost.org/trac10/ticket/9103#comment:7 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/86632" title="Merge from trunk. Fixes #9103 ">[86632]</a>) Merge from trunk. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9103" title="#9103: Bugs: execution of slots on deletion of signals (closed: fixed)">#9103</a> </p> Ticket