Skip to content

Commit aad7c66

Browse files
authored
[Flight] Don't try to close debug channel twice (facebook#34340)
When the debug channel was already closed, we must not try to close it again when the Response gets garbage collected. **Test plan:** 1. reduce the Flight fixture `App` component to a minimum [^1] - remove everything from `<body>` - delete the `console.log` statement 2. open the app in Firefox (seems to have a more aggressive GC strategy) 3. wait a few seconds On `main`, you will see the following error in the browser console: ``` TypeError: Can not close stream after closing or error ``` With this change, the error is gone. [^1]: It's a bit concerning that step 1 is needed to reproduce the issue. Either GC is behaving differently with the unmodified App, or we may hold on to the Response under certain conditions, potentially creating a memory leak. This needs further investigation.
1 parent 3fe51c9 commit aad7c66

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,10 +1010,15 @@ export function reportGlobalError(
10101010
if (__DEV__) {
10111011
const debugChannel = response._debugChannel;
10121012
if (debugChannel !== undefined) {
1013-
// If we don't have any more ways of reading data, we don't have to send any
1014-
// more neither. So we close the writable side.
1013+
// If we don't have any more ways of reading data, we don't have to send
1014+
// any more neither. So we close the writable side.
10151015
closeDebugChannel(debugChannel);
10161016
response._debugChannel = undefined;
1017+
// Make sure the debug channel is not closed a second time when the
1018+
// Response gets GC:ed.
1019+
if (debugChannelRegistry !== null) {
1020+
debugChannelRegistry.unregister(response);
1021+
}
10171022
}
10181023
}
10191024
}
@@ -2434,7 +2439,7 @@ function ResponseInstance(
24342439
// When a Response gets GC:ed because nobody is referring to any of the
24352440
// objects that lazily load from the Response anymore, then we can close
24362441
// the debug channel.
2437-
debugChannelRegistry.register(this, debugChannel);
2442+
debugChannelRegistry.register(this, debugChannel, this);
24382443
}
24392444
}
24402445
}

0 commit comments

Comments
 (0)