-
Notifications
You must be signed in to change notification settings - Fork 240
Fix topological sort NPE when re-enqueueing shapes #2785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Another potential solution may be to change the way that ReplaceShapes works, such that a shape with a duplicate Id is never enqueued in to the topological sorter more than once. Though the sorter should probably document somewhere that it expects each shape to be enqueued exactly once and throw, if this is an expected restriction. Or maybe return a boolean |
smithy-model/src/main/java/software/amazon/smithy/model/loader/TopologicalShapeSort.java
Show resolved
Hide resolved
Hi Jacob, thank you for the fix, we will get someone assigned to review it, but can you please fix the build failures, should be as easy as running |
Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a pr up that created a centralized dependency graph implementation - perhaps it should be used here. But in the meantime this looks good.
Agreed about switching to your implementation when it is ready.. There's only one or two classes that use this topological sort class, so it shouldn't be too hard to adapt them to use your implementation. |
Background
I have, on several occasions, run in to issues with an NPE coming out of the TopologicalShapeSort class. Specifically:
In some of these situations, I was able to alleviate the problem by changing the order in which I perform my model transformations, but I finally ran in to a case where I could not. I decided to look a little deeper.
When running
dequeueSortedShapes
, each shape that has no dependencies is processed in the order that they are enqueued. Upon dequeuing, they are removed from the forward dependencies map. This is fine whensatisfiedShapes
only contains satisfied shapes, but presents issues when something with an unsatisfied dependency is marked as satisfied. In this scenario, a given dependent may be processed before its dependencies and results in the case below:dequeueSortedShapes
is calledThe solution winds up being very simple. When a shape is enqueued with dependencies, we should check if it was previously marked as satisfied, and remove it from the queue if it was.
A related/opposite case was identified here: #2785 (comment)
This PR fixes that case as well.
Testing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.