Skip to content

fix: prevent deadlock when cross-region CDK stacks share the same stack name - #652

Merged
go-to-k merged 1 commit into
mainfrom
fix/cross-region-same-stackname-deadlock
Jul 9, 2026
Merged

fix: prevent deadlock when cross-region CDK stacks share the same stack name#652
go-to-k merged 1 commit into
mainfrom
fix/cross-region-same-stackname-deadlock

Conversation

@go-to-k

@go-to-k go-to-k commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Problem

delstack cdk deadlocks when two cross-region stacks share the same CloudFormation stack name:

INF Cross-region dependencies detected. Deleting stacks in dependency order...
fatal error: all goroutines are asleep - deadlock!

This happens with a common CDK pattern: a CloudFront us-east-1 support stack reuses the main stack's CloudFormation name via the stackName property. Both stacks resolve to the same StackName (e.g. AiApi), living in different regions.

Root cause

CdkDeleter builds its reverse-topological dependency graph keyed by StackName. When two distinct stacks share a name:

  1. stackMap collapses to a single entry.
  2. The main stack's dependency on the support stack resolves (by name) to the shared key, giving reverseInDegree == 1.
  3. No stack ever reaches in-degree 0, so no initial deletion starts and the completion loop blocks forever.

Additionally, Dependencies were stored as resolved stack names, which are ambiguous when names collide across regions.

Fix

Use the unique Cloud Assembly artifact key as the graph identity:

  • StackInfo gains an Identifier field, and Dependencies now hold artifact keys instead of resolved stack names.
  • CdkDeleter keys its graph by identity (stackIdentity, falling back to StackName when unset) and deletes by StackName + Region.
  • Stack selection (-s) dedups by identity, fixing a related latent bug where only one of two same-named cross-region stacks was selectable.

Tests

  • TestCdkDeleter_DeleteStacks_CrossRegionDeps_SameStackName reproduces the reported deadlock using a mock executor (no real AWS calls).
  • TestParseManifest_CrossRegionSameStackName covers parsing a manifest where stackName override causes a name collision.
  • Existing dependency/selection assertions updated for the artifact-key identity.

No real stacks were deleted during verification; the regression test with mocks confirms the fix.

…ck name

When two cross-region stacks share the same CloudFormation stack name (e.g.
a CloudFront us-east-1 support stack reusing the main stack name via the
stackName property), the dependency graph was keyed by StackName. The two
distinct stacks collapsed into one map entry, so no stack ever reached
in-degree 0 and the deletion loop blocked forever (deadlock).

Use the unique Cloud Assembly artifact key as the graph identity instead:
- StackInfo gains an Identifier field and stores dependencies as artifact
  keys (unambiguous when stack names collide).
- CdkDeleter keys its graph by identity and deletes by StackName + Region.
- Stack selection dedups by identity so both same-named cross-region stacks
  are selectable via -s.
@go-to-k
go-to-k merged commit 845c90f into main Jul 9, 2026
5 checks passed
@go-to-k
go-to-k deleted the fix/cross-region-same-stackname-deadlock branch July 9, 2026 10:46
@github-actions github-actions Bot mentioned this pull request Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant