Skip to content

Commit 04e563e

Browse files
committed
fix(compiler): save and restore effect flow around exclusive branches
Abort-controller sources on flow.sources leaked from one IfStatement/ ConditionalExpression branch into the other and from one SwitchStatement case into subsequent cases. An await that carried a signal in the consequent could incorrectly satisfy protectedWrite for an unsignaled request in the alternate or a later case. Save and restore effectPolicy flow around each exclusive branch of IfStatement/ConditionalExpression (matching the existing guard save/ restore) and around each non-fallthrough case of SwitchStatement.
1 parent 924aff9 commit 04e563e

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎packages/octane/src/compiler/strong-mode.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4148,15 +4148,18 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
41484148
const branchPhase = phaseAfter(node.test, phase);
41494149
const branches = conditionalExpressionBranches(node, scope);
41504150
const guards = currentEffect === null ? undefined : effectPolicy.saveGuards();
4151+
const savedFlow = currentEffect === null ? undefined : effectPolicy.saveFlow();
41514152
if ((branches & 1) !== 0) {
41524153
if (guards !== undefined) effectPolicy.guard(node.test, true);
41534154
visit(node.consequent, scope, branchPhase);
41544155
if (guards !== undefined) effectPolicy.restoreGuards(guards);
4156+
if (savedFlow !== undefined) effectPolicy.restoreFlow(savedFlow);
41554157
}
41564158
if ((branches & 2) !== 0) {
41574159
if (guards !== undefined) effectPolicy.guard(node.test, false);
41584160
visit(node.alternate, scope, branchPhase);
41594161
if (guards !== undefined) effectPolicy.restoreGuards(guards);
4162+
if (savedFlow !== undefined) effectPolicy.restoreFlow(savedFlow);
41604163
}
41614164
return;
41624165
}
@@ -4305,11 +4308,15 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
43054308

43064309
const fallbackPhase = searchPhase;
43074310
searchPhase = initialPhase;
4311+
const savedFlow = currentEffect === null ? undefined : effectPolicy.saveFlow();
43084312
let fallthroughPhase = null;
43094313
for (const branch of branches) {
43104314
if (currentFunctionIsAsync && searchPhase !== 'deferred' && alwaysAwaits(branch.test)) {
43114315
searchPhase = 'deferred';
43124316
}
4317+
if (savedFlow !== undefined && fallthroughPhase === null) {
4318+
effectPolicy.restoreFlow(savedFlow);
4319+
}
43134320
const matchingPhase = branch.test == null ? fallbackPhase : searchPhase;
43144321
const branchPhase =
43154322
matchingPhase === 'deferred' &&
@@ -4326,6 +4333,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
43264333
? null
43274334
: executionPhase;
43284335
}
4336+
if (savedFlow !== undefined) effectPolicy.restoreFlow(savedFlow);
43294337
return;
43304338
}
43314339
case 'VariableDeclaration': {

‎packages/octane/tests/compiler/strong-effects.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,46 @@ describe('Strong effect review regressions', () => {
528528
),
529529
).not.toThrow();
530530
});
531+
532+
it('does not leak abort sources from one if-branch into the other', () => {
533+
const source = component(
534+
`const [data, setData] = useState(null);
535+
useEffect(() => {
536+
const controller = new AbortController();
537+
(async () => {
538+
if (props.useSignal) {
539+
const r = await fetch('/api', { signal: controller.signal });
540+
setData(await r.json());
541+
} else {
542+
setData(await api.get(props.id));
543+
}
544+
})();
545+
return () => controller.abort();
546+
});`,
547+
).replace("from 'octane';", "from 'octane';\nimport { api } from './api';");
548+
rejects(source, FETCH);
549+
});
550+
551+
it('does not leak abort sources from one switch-case into another', () => {
552+
const source = component(
553+
`const [data, setData] = useState(null);
554+
useEffect(() => {
555+
const controller = new AbortController();
556+
(async () => {
557+
switch (props.mode) {
558+
case 'signal':
559+
setData(await fetch('/api', { signal: controller.signal }));
560+
break;
561+
default:
562+
setData(await api.get(props.id));
563+
break;
564+
}
565+
})();
566+
return () => controller.abort();
567+
});`,
568+
).replace("from 'octane';", "from 'octane';\nimport { api } from './api';");
569+
rejects(source, FETCH);
570+
});
531571
});
532572

533573
describe('Strong zero-delay effect updates', () => {

0 commit comments

Comments
 (0)