<p>Neels Hofmeyr <strong>uploaded patch set #6</strong> to this change.</p><p><a href="https://gerrit.osmocom.org/13392">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">fsm: support graceful osmo_fsm_inst_term() cascades<br><br>Add global flag osmo_fsm_term_safely() -- if set to true, enable the following<br>behavior:<br><br>Detect osmo_fsm_inst_term() occuring within osmo_fsm_inst_term():<br>- collect deallocations until the outermost osmo_fsm_inst_term() is done.<br>- call osmo_fsm_inst_free() *after* dispatching the parent event.<br><br>If a struct osmo_fsm_inst enters osmo_fsm_inst_term() while another is already<br>within osmo_fsm_inst_term(), do not directly deallocate it, but talloc-reparent<br>it to a separate talloc context, to be deallocated with the outermost FSM inst.<br><br>The effect is that all osmo_fsm_inst freed within an osmo_fsm_inst_term()<br>cascade will stay allocated until all osmo_fsm_inst_term() are complete and all<br>of them will be deallocated at the same time.<br><br>Mark the deferred deallocation state as __thread in an attempt to make cascaded<br>deallocation handling threadsafe.  Keep the enable/disable flag separate, so<br>that it is global and not per-thread.<br><br>The feature is showcased by fsm_dealloc_test.c: with this feature, all of those<br>wild deallocation scenarios succeed.<br><br>Make fsm_dealloc_test a normal regression test in testsuite.at.<br><br>Rationale:<br><br>It is difficult to gracefully handle deallocations of groups of FSM instances<br>that reference each other. As soon as one child dispatching a cleanup event<br>causes its parent to deallocate before fsm.c was ready for it, deallocation<br>will hit a use-after-free. Before this patch, by using parent_term events and<br>distinct "terminating" FSM states, parent/child FSMs can be taught to wait for<br>all children to deallocate before deallocating the parent. But as soon as a<br>non-child / non-parent FSM instance is involved, or actually any other<br>cleanup() action that triggers parent FSMs or parent talloc contexts to become<br>unused, it is near impossible to think of all possible deallocation events<br>ricocheting, and to avoid running into freeing FSM instances that were still in<br>the middle of osmo_fsm_inst_term(), or FSM instances to enter<br>osmo_fsm_inst_term() more than once. This patch makes deallocation of "all<br>possible" setups of complex cross referencing FSM instances easy to handle<br>correctly, without running into use-after-free or double free situations, and,<br>notably, without changing calling code.<br><br>Change-Id: I8eda67540a1cd444491beb7856b9fcd0a3143b18<br>---<br>M include/osmocom/core/fsm.h<br>M src/fsm.c<br>M tests/Makefile.am<br>M tests/fsm/fsm_dealloc_test.c<br>M tests/fsm/fsm_dealloc_test.err<br>M tests/testsuite.at<br>6 files changed, 3,503 insertions(+), 301 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/92/13392/6</pre><p>To view, visit <a href="https://gerrit.osmocom.org/13392">change 13392</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/13392"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newpatchset </div>
<div style="display:none"> Gerrit-Change-Id: I8eda67540a1cd444491beb7856b9fcd0a3143b18 </div>
<div style="display:none"> Gerrit-Change-Number: 13392 </div>
<div style="display:none"> Gerrit-PatchSet: 6 </div>
<div style="display:none"> Gerrit-Owner: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>
<div style="display:none"> Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-CC: Harald Welte <laforge@gnumonks.org> </div>