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

assertions: Maximum call stack size exceeded when parsing template #26766

Closed
Chriscbr opened this issue Aug 15, 2023 · 2 comments · Fixed by #26767
Closed

assertions: Maximum call stack size exceeded when parsing template #26766

Chriscbr opened this issue Aug 15, 2023 · 2 comments · Fixed by #26767
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@Chriscbr
Copy link
Contributor

Describe the bug

Calling Template.fromStack(stack) on some (CDK) stacks results in a (JavaScript) stack overflow

Expected Behavior

Either it synthesizes a template, or prints a helpful error

Current Behavior

RangeError: Maximum call stack size exceeded
    at Set.values (<anonymous>)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2297)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)
    at recurse (/Users/chrisr/dev/cdk-test/node_modules/aws-cdk-lib/assertions/lib/private/cyclic.js:3:2361)

Reproduction Steps

interface MyTriggerProps extends NodejsFunctionProps {
  executeAfter?: IConstruct[];
  executeBefore?: IConstruct[];
}

class MyTrigger extends Construct {
  public readonly fn: NodejsFunction;
  constructor(scope: Construct, id: string, props: MyTriggerProps) {
    super(scope, id);

    this.fn = new NodejsFunction(this, "Function", {
      entry: props.entry,
      handler: props.handler,
      functionName: props.functionName,
    });

    const trigger = new Trigger(this, "Trigger", {
      handler: this.fn,
    });
    trigger.executeAfter(...(props.executeAfter ?? []));
    trigger.executeBefore(...(props.executeBefore ?? []));
  }
}

export class CdkTestStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const triggerHello = new MyTrigger(this, "TriggerHello", {
      handler: "handler",
      entry: join(__dirname, "lmb/hello.ts"),
      functionName: "hello",
    });

    const triggerGoodbye = new MyTrigger(this, "TriggerGoodbye", {
      handler: "handler",
      entry: join(__dirname, "lmb/goodbye.ts"),
      functionName: "goodbye",
    });

    const triggerMiddle = new MyTrigger(this, "TriggerMiddle", {
      handler: "handler",
      entry: join(__dirname, "lmb/middle.ts"),
      functionName: "middle",
      executeAfter: [triggerHello.fn],
      executeBefore: [triggerGoodbye.fn],
    });
  }
}

const app = new cdk.App();
let stack = new CdkTestStack(app, 'CdkTestStack');
const template = Template.fromStack(stack);

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.92.0

Framework Version

No response

Node.js Version

18.14.1

OS

macOS 13.4

Language

Typescript

Language Version

No response

Other information

No response

@Chriscbr Chriscbr added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 15, 2023
@github-actions github-actions bot added the @aws-cdk/assertions Related to the @aws-cdk/assertv2 package label Aug 15, 2023
@pahud
Copy link
Contributor

pahud commented Aug 16, 2023

Thank you for the report and pull request 👍

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Aug 16, 2023
@mergify mergify bot closed this as completed in #26767 Aug 18, 2023
mergify bot pushed a commit that referenced this issue Aug 18, 2023
Closes #26766

The function `findCycle` tries to find a cycle by using a depth-first search (DFS). The DFS is implemented recursively in the recurse function. For each node, it tries to find a path that eventually leads back to the start of the path. If such a path is found, a cycle exists, and the nodes forming this cycle are returned.

One of the bugs in the current implementation is that it only checks whether the current dependency `dep` is equal to the first node of the current path `path[0]`. This means it will only detect a cycle if the cycle includes the first node of the search, which might not always be the case.

To fix this, the function should check whether the current dependency `dep` is already somewhere in the current path `path`. If it is, then a cycle has been found.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/assertions Related to the @aws-cdk/assertv2 package bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants