Bug Fix: Avoid Stack Overflow when provided data model relationships are included and contain circular references #32
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.
Description of Bug
This fix relates to a bug seen downstream in go-tfe. In short, when the provided data model has circular references within it's relations AND those relations are specified in "included" we can run into stack overflows. Specifically if two included items have included relations to each other. Such a situation leads to the unmarshal process getting stuck in a endless recursive loop.
For a data model like so:
When we specify the included as "project" and "organization", if within the included there is a project that is also the
DefaultProject
of an includedOrganization
we see a call stack like so:As we forever try to render the included relationships.
The Fix
When unmarshalling a nodes relationships, if the relationship is given in the included, we store a pointer to the results of it's unmarshalling within the
includedNode
struct that is passed throughout the call stack. If this same item is in a relationship and included elsewhere, we opt to use the stored result to avoid the potential infinite loop we see above.Testing
Added a test, with associated data model edits, that captures this behavior. If you wish to see the stack overflow that is caused without the fix, you can simply comment out this code section:
And run
TestUnmarshalMany_relationships_with_circular_inclusion
to see the stack overflow occur.