[Flight] Add more cycle protections (#36236)
Sebastian "Sebbie" Silbermann committed
Apr 8, 2026 at 21:01 UTC
404b38c764cf86e6f2ec42f873bb33ce114256d3
1 file changed
+6
-3
packages/react-server/src/ReactFlightReplyServer.js
+6
-3
@@ -1123,8 +1123,9 @@ function createMap(
1123
if ((model as any).$$consumed === true) {
1124
throw new Error('Already initialized Map.');
1125
}
1126
- const map = new Map(model);
1126
+ // This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
1127
(model as any).$$consumed = true;
1128
+ const map = new Map(model);
1129
return map;
1130
}
1131
@@ -1135,8 +1136,9 @@ function createSet(response: Response, model: Array<any>): Set<any> {
1136
if ((model as any).$$consumed === true) {
1137
throw new Error('Already initialized Set.');
1138
}
1138
- const set = new Set(model);
1139
+ // This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
1140
(model as any).$$consumed = true;
1141
+ const set = new Set(model);
1142
return set;
1143
}
1144
@@ -1147,9 +1149,10 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
1149
if ((model as any).$$consumed === true) {
1150
throw new Error('Already initialized Iterator.');
1151
}
1152
+ // This needs to come first to prevent the model from being consumed again in case of a cyclic reference.
1153
+ (model as any).$$consumed = true;
1154
// $FlowFixMe[incompatible-use]: This uses raw Symbols because we're extracting from a native array.
1155
const iterator = model[Symbol.iterator]();
1152
- (model as any).$$consumed = true;
1156
return iterator;
1157
}
1158