<p>Neels Hofmeyr <strong>uploaded patch set #3</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>Detect osmo_fsm_inst_term() occuring within osmo_fsm_inst_term():<br>- prevent the same FSM instance from entering term twice, and<br>- collect deallocations until the outermost osmo_fsm_inst_term() is done.<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 the struct osmo_fsm_inst that first started terminating.<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>To prevent re-entering osmo_fsm_inst_term() twice for the same osmo_fsm_inst,<br>add flag osmo_fsm_inst.proc.terminating. osmo_fsm_inst_term() sets this to<br>true, or exits if it already is true.<br><br>This is showcased by fsm_dealloc_test.c.<br><br>Enhance fsm_dealloc_test.c to show that now all of the modeled situations solve<br>cleanly.<br><br>Make fsm_dealloc_test a normal regression test in testsuite.at.<br><br>Note: This patch's mechanism is not thread safe. It might be made thread safe<br>if the new global variable osmo_fsm_inst_terminating_root were handled<br>per-thread instead of once globally.<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>Logging of FSM deallocation changes, but only in cases where<br>osmo_fsm_inst_term() triggers other osmo_fsm_inst_term().<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,624 insertions(+), 93 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/3</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: 3 </div>
<div style="display:none"> Gerrit-Owner: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>