Skip to content
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

fix: fix data race in schema merging logic by creating a copy instead of modifying the original struct instance #397

Merged
merged 1 commit into from
Jun 5, 2024

Conversation

ansgarm
Copy link
Member

@ansgarm ansgarm commented Jun 5, 2024

Different jobs in terraform-ls use the same hcl.Block instance simultaneously and the schema merger modified it in the past.
We fixed this by creating a copy first which uncovered another bug: The buildDynamicBlockSchema function implicitly read that change from the dependent schema, which we fixed by reading from the mergedSchema instead. This in turn required us to also pass the dependent schema into the function as the mergedSchema also contains the static lifecycle block which can't be set via a dynamic block. This means we now iterate over all blocks in the dependent schema but copy their bodies from the mergedSchema.

Fixes #395

@ansgarm ansgarm force-pushed the b-fix-data-race-schema-merging branch from a1758a4 to 38e6601 Compare June 5, 2024 13:42
@ansgarm ansgarm changed the title fix: improve dynamic block schema merging logic to not modify in place fix: fix data race in schema merging logic by creating a copy instead of modifying the original struct instance Jun 5, 2024
@ansgarm ansgarm marked this pull request as ready for review June 5, 2024 13:57
@ansgarm ansgarm requested a review from a team as a code owner June 5, 2024 13:57
@ansgarm ansgarm force-pushed the b-fix-data-race-schema-merging branch from 38e6601 to e632be0 Compare June 5, 2024 14:47
… of modifying the original struct instance

Different jobs in terraform-ls use the same `hcl.Block` instance simultaneously and the schema merger modified it in the past.
We fixed this by creating a copy first which uncovered another bug: The `buildDynamicBlockSchema` function implicitly read
that change from the dependent schema, which we fixed by reading from the `mergedSchema` instead. This in turn required us
to also pass the dependent schema into the function as the `mergedSchema` also contains the static `lifecycle` block which
can't be set via a dynamic block. This means we now iterate over all blocks in the dependent schema but copy their bodies
from the `mergedSchema`.
@ansgarm ansgarm force-pushed the b-fix-data-race-schema-merging branch from e632be0 to e29c68b Compare June 5, 2024 14:58
Copy link
Contributor

@jpogran jpogran left a comment

Choose a reason for hiding this comment

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

Especially appreciate the doc comments on the buildDynamicBlockSchema func

@jpogran jpogran merged commit 0e930f4 into main Jun 5, 2024
5 checks passed
@jpogran jpogran deleted the b-fix-data-race-schema-merging branch June 5, 2024 15:04
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.

Data Race when merging block schema body extensions
2 participants