{"record":{"id":"fb08cb9faa97ebb0","repo":"anomalyco/sst","slug":"cannot-reuse-the-this-name-state-states-cann","errorCode":null,"errorMessage":"Cannot reuse the \"${this.name}\" state. States cannot be reused in Map or Parallel branches.","messagePattern":"Cannot reuse the \"(.+?)\" state\\. States cannot be reused in Map or Parallel branches\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"platform/src/components/aws/step-functions/state.ts","lineNumber":315,"sourceCode":"\n    states.set(this.name, this);\n\n    this._nextState?.assertStateNameUnique(states);\n    this._catches?.forEach((c) => c.next.assertStateNameUnique(states));\n    this._childGraphStates.forEach((c) => c.assertStateNameUnique(states));\n  }\n\n  /**\n   * Assert that the state is not reused.\n   * @internal\n   */\n  public assertStateNotReused(\n    states: Map<State, string> = new Map(),\n    graphId: string = \"main\",\n  ) {\n    const existing = states.get(this);\n    if (existing && existing !== graphId)\n      throw new Error(\n        `Cannot reuse the \"${this.name}\" state. States cannot be reused in Map or Parallel branches.`,\n      );\n\n    states.set(this, graphId);\n\n    this._nextState?.assertStateNotReused(states, graphId);\n    this._catches?.forEach((c) => c.next.assertStateNotReused(states, graphId));\n    this._childGraphStates.forEach((c) => {\n      const childGraphId = randomBytes(16).toString(\"hex\");\n      c.assertStateNotReused(states, childGraphId);\n    });\n  }\n\n  /**\n   * Get the permissions required for the state.\n   * @internal\n   */\n  public getPermissions(): FunctionPermissionArgs[] {","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/platform/src/components/aws/step-functions/state.ts#L297-L333","documentation":"A single State instance can appear in only one place in a Step Functions state machine graph. `State.assertStateNotReused` (platform/src/components/aws/step-functions/state.ts:309) tracks which graph (main vs each Map/Parallel branch) has visited each state object and throws when the same instance is reached from more than one graph, i.e. shared across branches.","triggerScenarios":"Using the same State instance inside two different Map item selector chains or two different Parallel branches (e.g. `const shared = task.next(notify); branch1.start(shared); branch2.start(shared);`). The compile-time counterpart is `addChildGraph` throwing for a state already attached to another parent graph.","commonSituations":"Extracting a common 'notify'/'cleanup' chain into a variable and appending it to multiple Parallel branches or Map bodies; refactoring shared steps into a helper function that returns the same state objects for reuse.","solutions":["Create a separate State instance per branch, even if the configuration is identical.","Extract a factory function that constructs fresh states on each call instead of returning shared instances.","If the logic is truly identical and reused, consider a nested state machine definition or duplicate the chain with unique names."],"exampleFix":"// before\nconst notify = snsPublish.next(finalize);\nparallel.branch(notify);\nparallel.branch(notify);\n\n// after\nconst makeNotify = (name: string) =>\n  new sst.aws.StepFunctionsTask(name, { ... });\nparallel.branch(makeNotify(\"Notify1\").next(new sst.aws.StepFunctionsTask(\"Finalize1\", { ... })));\nparallel.branch(makeNotify(\"Notify2\").next(new sst.aws.StepFunctionsTask(\"Finalize2\", { ... })));","handlingStrategy":"validation","validationCode":"const seen = new Set<object>();\nfunction assertNotShared(state: object) {\n  if (seen.has(state))\n    throw new Error(\"state instance reused across branches\");\n  seen.add(state);\n}","typeGuard":"null","tryCatchPattern":"try {\n  parallel.branch(sharedChain);\n} catch (err) {\n  if (String(err).includes(\"Cannot reuse\")) {\n    // build fresh state instances per branch via a factory function\n  } else throw err;\n}","preventionTips":["Use factory functions that construct new state instances per call instead of sharing state variables across branches.","Treat State objects as single-use; never assign the same instance to two Map/Parallel branches.","For genuinely shared logic, duplicate the definition with unique names or use a nested state machine."],"tags":["step-functions","state-machine","parallel","map"],"backgroundTag":"state-reuse-across-branches","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}