{"record":{"id":"d0b427a8860de664","repo":"facebook/react","slug":"572","errorCode":"572","errorMessage":"Already initialized Map.","messagePattern":"Already initialized Map\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react-server/src/ReactFlightReplyServer.js","lineNumber":1180,"sourceCode":"          reason: chunk.reason,\n          deps: 0,\n          errored: true,\n        };\n      }\n      // Placeholder\n      return null as any;\n  }\n}\n\nfunction createMap(\n  response: Response,\n  model: Array<[any, any]>,\n): Map<any, any> {\n  if (!isArray(model)) {\n    throw new Error('Invalid Map initializer.');\n  }\n  if ((model as any).$$consumed === true) {\n    throw new Error('Already initialized Map.');\n  }\n  // This needs to come first to prevent the model from being consumed again in case of a cyclic reference.\n  (model as any).$$consumed = true;\n  const map = new Map(model);\n  return map;\n}\n\nfunction createSet(response: Response, model: Array<any>): Set<any> {\n  if (!isArray(model)) {\n    throw new Error('Invalid Set initializer.');\n  }\n  if ((model as any).$$consumed === true) {\n    throw new Error('Already initialized Set.');\n  }\n  // This needs to come first to prevent the model from being consumed again in case of a cyclic reference.\n  (model as any).$$consumed = true;\n  const set = new Set(model);\n  return set;","sourceCodeStart":1162,"sourceCodeEnd":1198,"githubUrl":"https://github.com/facebook/react/blob/eafeac097ba51e1eab809c07102126bd5f8e5425/packages/react-server/src/ReactFlightReplyServer.js#L1162-L1198","documentation":"createMap sets $$consumed = true on the model array before building the Map, specifically so a cyclic reference cannot consume the same model twice. If the same serialized Map model reaches createMap again, the guard throws instead of producing wrong data or looping.","triggerScenarios":"The same Map model is initialized twice - a cyclic structure the encoder failed to outline (a Map containing itself), duplicate references to one model in a crafted payload, or version-skewed encoders.","commonSituations":"Passing self-referential Maps through server actions; replayed or edited FormData where one Map id is referenced from two initializer positions.","solutions":["Avoid passing self-referential/cyclic Maps as server action arguments; reference members by id instead.","Catch the decode error and reject the request as malformed.","Align React versions across the boundary if skew is suspected."],"exampleFix":"// before\nconst m = new Map();\nm.set('self', m); // cyclic Map serialized into a self-referencing model\nawait save(m);\n\n// after\nconst m = new Map();\nm.set('rootId', 'map-1'); // reference by identifier, resolve on the server\nawait save({id: 'map-1', entries: [...m]});","handlingStrategy":"try-catch","validationCode":"function hasCycle(v: unknown, seen = new WeakSet()): boolean {\n  if (v && (typeof v === 'object' || typeof v === 'function')) {\n    if (seen.has(v)) return true;\n    seen.add(v);\n    if (v instanceof Map) {\n      for (const [k, val] of v) if (hasCycle(k, seen) || hasCycle(val, seen)) return true;\n    } else if (v instanceof Set) {\n      for (const item of v) if (hasCycle(item, seen)) return true;\n    } else if (Array.isArray(v)) {\n      for (const item of v) if (hasCycle(item, seen)) return true;\n    } else {\n      for (const k in v) if (hasCycle((v as any)[k], seen)) return true;\n    }\n  }\n  return false;\n}\n// before invoking the action\nif (hasCycle(args)) throw new Error('Cyclic data cannot be serialized');","typeGuard":null,"tryCatchPattern":"try {\n  const args = await decodeReply(formData);\n} catch (e) {\n  return new Response('Bad request', {status: 400});\n}","preventionTips":["Do not pass self-referential Maps as server action arguments; reference members by id.","Run a cycle check on complex client state before submitting it to an action.","Rebuild cyclic structures server-side from flat records."],"tags":["react","server-actions","deserialization","map","cyclic-reference"],"backgroundTag":"cyclic-reference-in-serialized-data","analyzedSha":"eafeac097ba51e1eab809c07102126bd5f8e5425","analyzedAt":"2026-08-21T22:01:08.818Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}