Skip to content

[Runtime] Fix a false metadata cycle diagnostic when threads race to instantiate cyclical metadata. #80505

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

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Apr 3, 2025

The metadata creation system detects cycles where metadata depends on other metadata which depends on the first one again and raises a fatal error if the cycle can't be fulfilled.

Some cycles can be fulfilled. The cycle may involve a requirement for a metadata state less than full transitive completeness which can be reached without resolving the entire cycle. We only want to raise a fatal error when we detect a cycle that can't be fulfilled.

Normally this happens because the cycle checking in blockOnMetadataDependency only sees a cycle when it can't be fulfilled. Metadata initialization is advanced as far as it can be at each stage, so a cycle that can be fulfilled will see a fulfilling state and won't generate the dependency in the first place, since we only generate dependencies that haven't yet been met.

However, when two threads race to create types in a cycle, we can end up with such a dependency, because the dependency may be generated before another thread fulfilled yet. The cycle checker doesn't account for this and incorrectly raises a fatal error in that case.

Fix this by checking the cyclic dependency against the metadata's current state. If we have a dependency that's already been fulfilled, then there isn't really a dependency cycle. In that case, don't raise a fatal error.

rdar://135036243

@mikeash mikeash requested a review from rjmccall April 3, 2025 19:28
@mikeash mikeash requested a review from al45tair as a code owner April 3, 2025 19:28
@mikeash
Copy link
Contributor Author

mikeash commented Apr 3, 2025

@swift-ci please test

…instantiate cyclical metadata.

The metadata creation system detects cycles where metadata depends on other metadata which depends on the first one again and raises a fatal error if the cycle can't be fulfilled.

Some cycles can be fulfilled. The cycle may involve a requirement for a metadata state less than full transitive completeness which can be reached without resolving the entire cycle. We only want to raise a fatal error when we detect a cycle that can't be fulfilled.

Normally this happens because the cycle checking in `blockOnMetadataDependency` only sees a cycle when it can't be fulfilled. Metadata initialization is advanced as far as it can be at each stage, so a cycle that can be fulfilled will see a fulfilling state and won't generate the dependency in the first place, since we only generate dependencies that haven't yet been met.

However, when two threads race to create types in a cycle, we can end up with such a dependency, because the dependency may be generated before another thread fulfilled yet. The cycle checker doesn't account for this and incorrectly raises a fatal error in that case.

Fix this by checking the cyclic dependency against the metadata's current state. If we have a dependency that's already been fulfilled, then there isn't really a dependency cycle. In that case, don't raise a fatal error.

rdar://135036243
@mikeash mikeash force-pushed the metadata-cycle-race-fix branch from 5feb410 to 9ad534b Compare April 4, 2025 21:50
@mikeash
Copy link
Contributor Author

mikeash commented Apr 4, 2025

@swift-ci please test

@mikeash
Copy link
Contributor Author

mikeash commented Apr 7, 2025

@swift-ci please test linux platform

@mikeash
Copy link
Contributor Author

mikeash commented Apr 7, 2025

@swift-ci please test windows platform

Copy link
Contributor

@al45tair al45tair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice spot.

@mikeash mikeash merged commit 1f0696a into swiftlang:main Apr 7, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants