Protect erase_resource and set_resource_health against cyclic sub-resources - #510
Open
ZayanKhan-12 wants to merge 1 commit into
Open
Protect erase_resource and set_resource_health against cyclic sub-resources#510ZayanKhan-12 wants to merge 1 commit into
ZayanKhan-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>
Contributor
|
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. |
3 tasks
Contributor
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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