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

[engine] Check for old resources first by URN and then aliases #13883

Merged
merged 1 commit into from Sep 6, 2023

Conversation

justinvp
Copy link
Member

@justinvp justinvp commented Sep 6, 2023

This change fixes a regression in the step generator when looking for old resources. When generating steps for a register resource event, we previously looked for old resources first by the resource's URN and then by aliases.

This regressed with #10819:

-	for _, urnOrAlias := range append([]resource.URN{urn}, goal.Aliases...) {
+	aliases[urn] = struct{}{}
+	for urnOrAlias := range aliases {

Previously, aliases were in a slice and we always looked for the URN first, then aliases.

With #10819, aliases changed to being stored in a map (a set). The URN was added to the map before iterating over it, but there's no guarantee it will be looked at first (iteration order for maps is unspecified), and with the current behavior when there are aliases in the map, the URN very likely won't come first.

This can lead to duplicate resources in the state (stack corruption) when the wrong old resource is chosen.

The fix is to move back to always checking for old resources using the URN first. We also move back to maintaining aliases in a slice for consistent ordering.

Fixes #13848

@pulumi-bot
Copy link
Contributor

Changelog

[uncommitted] (2023-09-06)

Bug Fixes

  • [engine] Check for old resources first by URN and then aliases
    #13883

snap, res = TestOp(Update).Run(project, p.GetTarget(t, snap), p.Options, false, p.BackendClient, nil)
assert.Nil(t, res)
assert.NotNil(t, snap)
assert.Nil(t, snap.VerifyIntegrity())
Copy link
Member Author

Choose a reason for hiding this comment

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

Prior to the fix, this fails because we end up with two resources with the same URN (urn:pulumi:test::test::pkgA:m:typA::resB) in the snapshot.

This change fixes a regression in the step generator when looking for
old resources. When generating steps for a register resource event, we
previously looked for old resources first by the resource's URN and then
by aliases.

This regressed with #10819:

```diff
-	for _, urnOrAlias := range append([]resource.URN{urn}, goal.Aliases...) {
+	aliases[urn] = struct{}{}
+	for urnOrAlias := range aliases {
```

Previously, aliases were in a slice and we always looked for the URN
first, then aliases.

With #10819, aliases changed to being stored in a map (a set). The URN
was added to the map before iterating over it, but there's no guarantee
it will be looked at first (iteration order for maps is unspecified),
and with the current behavior when there are aliases in the map, the URN
very likely won't come first.

This can lead to duplicate resources in the state (stack corruption)
when the wrong old resource is chosen.

The fix is to move back to always checking for old resources using the
URN first. We also move back to maintaining aliases in a slice for
consistent ordering.
@justinvp justinvp requested a review from a team September 6, 2023 11:47
@justinvp justinvp added this pull request to the merge queue Sep 6, 2023
Merged via the queue into master with commit 4c0b08d Sep 6, 2023
46 checks passed
@justinvp justinvp deleted the justin/aliasaftercreatefails branch September 6, 2023 12:59
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.

error: snapshot integrity failure; refusing to use it: duplicate resource <urn> (not marked for deletion)
3 participants