Skip to content

Commit fb2177c

Browse files
unstubbableeps1lon
andauthored
[Flight] Fix pending chunks count for streams & async iterables in DEV (facebook#35143)
In DEV, we need to prevent the response from being GC'd while there are still pending chunks for ReadableSteams or pending results for AsyncIterables. Co-authored-by: Sebastian "Sebbie" Silbermann <[email protected]>
1 parent 647e133 commit fb2177c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,12 @@ function createInitializedStreamChunk<
813813
value: T,
814814
controller: FlightStreamController,
815815
): InitializedChunk<T> {
816+
if (__DEV__) {
817+
// Retain a strong reference to the Response while we wait for chunks.
818+
if (response._pendingChunks++ === 0) {
819+
response._weakResponse.response = response;
820+
}
821+
}
816822
// We use the reason field to stash the controller since we already have that
817823
// field. It's a bit of a hack but efficient.
818824
// $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
@@ -3075,7 +3081,6 @@ function resolveStream<T: ReadableStream | $AsyncIterable<any, any, void>>(
30753081
// We already resolved. We didn't expect to see this.
30763082
return;
30773083
}
3078-
releasePendingChunk(response, chunk);
30793084

30803085
const resolveListeners = chunk.value;
30813086

@@ -3375,6 +3380,14 @@ function stopStream(
33753380
// We didn't expect not to have an existing stream;
33763381
return;
33773382
}
3383+
if (__DEV__) {
3384+
if (--response._pendingChunks === 0) {
3385+
// We're no longer waiting for any more chunks. We can release the strong
3386+
// reference to the response. We'll regain it if we ask for any more data
3387+
// later on.
3388+
response._weakResponse.response = null;
3389+
}
3390+
}
33783391
const streamChunk: InitializedStreamChunk<any> = (chunk: any);
33793392
const controller = streamChunk.reason;
33803393
controller.close(row === '' ? '"$undefined"' : row);

0 commit comments

Comments
 (0)