Protect erase_resource and set_resource_health against cyclic sub-resources - #510
Protect erase_resource and set_resource_health against cyclic sub-resources#510ZayanKhan-12 wants to merge 1 commit into
Conversation
…ources A resource that is directly or indirectly listed as a sub-resource of itself caused unbounded recursion and a stack overflow, both in erase_resource (as reported in sony#403) and in set_resource_health, which is reached via insert_resource when the cyclic relationship already exists in the registry. Track the ids already visited during the recursive descent and skip any id seen before, so cycles of any length are handled, per the discussion in sony#403. Event order and results for well-formed sub-resource trees are unchanged. Closes sony#403 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
As pointed out in #403, a sub-resource cycle is an application bug, therefore further discussion needed to decide whether to accept this workaround which hides the actual problem as-is or to modify it to throw a logic_error which the application developer must handle. I'm not sure if it's possible to detect the insertion or modification of resources that introduce sub-resource cycles. That would be useful. |
|
On reflection, I think adding this cost to every erase and health check is the wrong approach, better to prevent the obvious ways an application could have got itself into that state (trying to insert a resource with the same id as one of its ancestors). I propose #516. |
|
Thank you, @ZayanKhan-12 and @garethsb, for your contributions. Your suggestions have inspired @garethsb to create #516, which has now been merged into the master. |
Closes #403
Problem
As reported in #403,
nmos::erase_resourcerecurses intosub_resourcesbefore the resource itself is marked erased, so a resource that is (directly or indirectly) listed as a sub-resource of itself causes unbounded recursion and a stack overflow.While writing the regression test, it turned out
set_resource_healthhas the same defect — and it is actually hit first:insert_resourcecalls it to propagate health, so merely inserting a resource whose cyclic relationship already exists in the registry crashes before any erase happens.Fix
Both functions now delegate to internal helpers that track the ids already visited during the recursive descent and skip any id seen before. Per the discussion in #403, this handles not just self-reference but cycles of any length (A→B→A etc.). Public signatures, event order, and results for well-formed sub-resource trees are unchanged.
Testing
New
nmos/test/resources_test.cpp:set_resource_healthviainsert_resource, then inerase_resource)Full local suite: all 170 test cases / 2349 assertions pass (macOS arm64, apple-clang, conan dependencies).
🤖 Generated with Claude Code