Skip to content

Commit 3d05080

Browse files
authored
[Fizz] Construct the render lifetime controller only when it is needed (#37357)
This follows #37315, which added the render lifetime controller to bound the abort listener that `attachAbortSignal` attaches to a caller's signal. `RequestInstance` constructed one for every request, so a render that is given no signal allocated a controller, aborted it on completion, and nothing ever observed either. The controller is now created in `attachAbortSignal`, and the three places that end the lifetime go through `endRenderLifetime`, which does nothing when there is no controller. Callers that pass a signal are unaffected. Callers that do not no longer allocate one, and `signal` is optional in every browser, edge and static entry point, while `renderToPipeableStream` and `resumeToPipeableStream` accept no signal at all. They also no longer reach `AbortController` at all, which matters more than the allocation. Fizz had no runtime dependency on it before #37315, and an unconditional one reaches environments that provide the API through a polyfill. An incomplete polyfill can then fail a render that never asked for abort support. The new test asserts that no controller is constructed when no signal is passed. It fails with the eager construction restored, since nothing else in the suite would notice a regression to it.
1 parent b939280 commit 3d05080

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎packages/react-dom/src/__tests__/ReactDOMFizzStaticBrowser-test.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,32 @@ describe('ReactDOMFizzStaticBrowser', () => {
15841584
expect(lifetimes[0].aborted).toBe(true);
15851585
});
15861586

1587+
it('constructs no abort controller when no signal is passed', async () => {
1588+
// The render lifetime exists only to bound the caller's abort listener,
1589+
// so a render that is given no signal has nothing to bound and should not
1590+
// pay for a controller. It also means such a render never requires
1591+
// AbortController to exist or to work.
1592+
const RealAbortController = globalThis.AbortController;
1593+
let constructed = 0;
1594+
globalThis.AbortController = class extends RealAbortController {
1595+
constructor() {
1596+
super();
1597+
constructed++;
1598+
}
1599+
};
1600+
1601+
try {
1602+
const stream = await serverAct(() =>
1603+
ReactDOMFizzServer.renderToReadableStream(<div>hello world</div>),
1604+
);
1605+
await readContent(stream);
1606+
} finally {
1607+
globalThis.AbortController = RealAbortController;
1608+
}
1609+
1610+
expect(constructed).toBe(0);
1611+
});
1612+
15871613
it('attaches no listener when the signal is already aborted', async () => {
15881614
const controller = new AbortController();
15891615
controller.abort();

‎packages/react-server/src/ReactFizzServer.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@ export opaque type Request = {
435435
onFatalError: (error: mixed) => void,
436436
// Aborted once the render ends, whether it completed, failed fatally or was
437437
// aborted. Bounds the lifetime of anything that must not outlive the render.
438-
renderLifetimeController: AbortController,
438+
// Null until attachAbortSignal creates it, so a render that is given no
439+
// signal constructs no controller.
440+
renderLifetimeController: null | AbortController,
439441
// Form state that was the result of an MPA submission, if it was provided.
440442
formState: null | ReactFormState<any, any>,
441443
// DEV-only, warning dedupe
@@ -589,7 +591,7 @@ function RequestInstance(
589591
this.onShellReady = onShellReady === undefined ? noop : onShellReady;
590592
this.onShellError = onShellError === undefined ? noop : onShellError;
591593
this.onFatalError = onFatalError === undefined ? noop : onFatalError;
592-
this.renderLifetimeController = new AbortController();
594+
this.renderLifetimeController = null;
593595
this.formState = formState === undefined ? null : formState;
594596
if (__DEV__) {
595597
this.didWarnForKey = null;
@@ -1455,7 +1457,7 @@ function fatalError(
14551457
}
14561458
onFatalError(error);
14571459
}
1458-
request.renderLifetimeController.abort(RENDER_ENDED);
1460+
endRenderLifetime(request);
14591461
if (request.destination !== null) {
14601462
request.status = CLOSED;
14611463
closeWithError(request.destination, error);
@@ -6352,7 +6354,7 @@ function flushCompletedQueues(
63526354
}
63536355
}
63546356
// We're done.
6355-
request.renderLifetimeController.abort(RENDER_ENDED);
6357+
endRenderLifetime(request);
63566358
request.status = CLOSED;
63576359
close(destination);
63586360
// We need to stop flowing now because we do not want any async contexts which might call
@@ -6516,6 +6518,13 @@ function finishAbort(request: Request, abortableTasks: Set<Task>): void {
65166518
}
65176519
}
65186520

6521+
function endRenderLifetime(request: Request): void {
6522+
const renderLifetimeController = request.renderLifetimeController;
6523+
if (renderLifetimeController !== null) {
6524+
renderLifetimeController.abort(RENDER_ENDED);
6525+
}
6526+
}
6527+
65196528
// Aborts the request when the caller's signal aborts. The render lifetime
65206529
// bounds the listener, so the runtime removes the listener as soon as the
65216530
// render ends. From that point on abort() returns early, so the listener has
@@ -6533,12 +6542,14 @@ export function attachAbortSignal(request: Request, signal: AbortSignal): void {
65336542
abort(request, signal.reason);
65346543
return;
65356544
}
6545+
const renderLifetimeController = new AbortController();
6546+
request.renderLifetimeController = renderLifetimeController;
65366547
signal.addEventListener(
65376548
'abort',
65386549
() => {
65396550
abort(request, signal.reason);
65406551
},
6541-
{signal: request.renderLifetimeController.signal},
6552+
{signal: renderLifetimeController.signal},
65426553
);
65436554
}
65446555

@@ -6552,7 +6563,7 @@ export function abort(request: Request, reason: mixed): void {
65526563
// can be aborted. in practice this makes abort callable at most once per render.
65536564
return;
65546565
}
6555-
request.renderLifetimeController.abort(RENDER_ENDED);
6566+
endRenderLifetime(request);
65566567
const isRecoverableReason =
65576568
typeof reason === 'object' &&
65586569
reason !== null &&

0 commit comments

Comments
 (0)