Repository navigation
Description
Sibling providers of the same context unnecessarily refresh consumers below memo
Changing one provider's value can execute consumers beneath a separate sibling provider of the same context, even when their own nearest provider value and memoized ancestor props are unchanged. The rendered value stays correct, but the retained subtree does unnecessary render work. Depending on sibling order, that work happens during the provider update or the next otherwise unrelated root update.
Verified version
Reproduced with the unmodified published [email protected] browser runtime, Node 22.22.3, [email protected], and [email protected]. This is a synthetic runtime-only reproduction using public APIs; it does not invoke the Octane compiler or depend on an application.
All 697 installed Octane package files were compared byte-for-byte with the official registry tarball, and its SHA-512 integrity was checked against registry metadata. Tarball SHA-256: c500671442762dd4e9ec07dcd85643fcb8f513786aeb41f84505948bc3545060.
The official npm provenance identifies source commit f7b41dcccb77848e9649f3f55de7b4e583205545. Its subject digest matches the registry metadata, and that commit's package manifest declares 0.13.5. The provenance payload was decoded and cross-checked; its Sigstore signature was not independently verified.
Reproduction
In an empty directory, install the exact versions below, save the following code as repro.mjs, and run it:
npm init -y
npm install --save-exact [email protected] [email protected] [email protected]
node repro.mjsThe runner bundles the published browser entry with production conditions, mounts two sibling providers, and counts calls to a reader beneath an unchanged memo boundary. It checks the retained DOM node and nearest-provider text after every update. Initial provider-version stamps are settled before the measured steps.
import assert from 'node:assert/strict';
import { build } from 'esbuild';
import { Window } from 'happy-dom';
const window = new Window();
for (const name of ['window', 'document', 'Node', 'Element', 'HTMLElement', 'SVGElement', 'Comment', 'Text', 'Event', 'MouseEvent', 'MutationObserver']) {
globalThis[name] = name === 'window' ? window : window[name];
}
const { outputFiles } = await build({
stdin: {
contents: "export {createRoot, createElement, createContext, useContext, memo, flushSync} from 'octane';",
resolveDir: import.meta.dirname,
},
bundle: true, write: false, platform: 'browser', format: 'esm',
define: { 'process.env.NODE_ENV': '"production"', __OCTANE_PROFILE_ENABLED__: 'false' },
});
const runtime = await import('data:text/javascript;base64,' + Buffer.from(outputFiles[0].text).toString('base64'));
const { createRoot, createElement: h, createContext, useContext, memo, flushSync } = runtime;
const Context = createContext('default');
const OtherContext = createContext('other');
let reads = 0;
function Reader() {
reads++;
return h('span', { 'data-reader': '' }, useContext(Context));
}
const Wall = memo(function Wall() { return h(Reader); });
function App({ retainedFirst, own, sibling, other, tick }) {
const retained = h(Context, { key: 'retained', value: own }, h(Wall));
const changing = h(Context, { key: 'sibling', value: sibling }, h('aside', null, sibling));
return h('main', { 'data-tick': tick },
...(retainedFirst ? [retained, changing] : [changing, retained]),
h(OtherContext, { value: other }, h('i', null, other)));
}
const observations = [];
for (const retainedFirst of [true, false]) {
const host = document.createElement('div');
document.body.append(host);
const root = createRoot(host);
let tick = 0;
const update = (own, sibling, other = 'other-a') => {
reads = 0;
flushSync(() => root.render(App, { retainedFirst, own, sibling, other, tick: tick++ }));
return reads;
};
update('retained', 'a');
update('retained', 'a'); // Settle the initial provider-version stamps.
const node = host.querySelector('[data-reader]');
const steps = [
['unchanged control', 'retained', 'a', 'other-a'],
['sibling changes', 'retained', 'b', 'other-a'],
['next root update', 'retained', 'b', 'other-a'],
['unrelated context changes', 'retained', 'b', 'other-b'],
['own provider changes', 'changed', 'b', 'other-b'],
];
const result = { order: retainedFirst ? 'retained, sibling' : 'sibling, retained', steps: [] };
for (const [step, own, sibling, other] of steps) {
const readerCalls = update(own, sibling, other);
assert.equal(host.querySelector('[data-reader]'), node);
assert.equal(node.textContent, own);
result.steps.push({ step, readerCalls, text: node.textContent });
}
observations.push(result);
root.unmount();
host.remove();
}
console.log(JSON.stringify(observations, null, 2));
await window.happyDOM.close();Observed and expected
Counts below are additional calls to Reader for each update, not timings. Each order uses a fresh root, and its sibling order stays fixed throughout the scenario.
| Update | Retained provider first | Changing sibling first | Expected |
|---|---|---|---|
| Unchanged control | 0 | 0 | 0 |
Sibling provider changes a → b |
0 | 1 | 0 |
| Next root update; both provider values unchanged | 1 | 0 | 0 |
| Different context changes | 0 | 0 | 0 |
| Retained consumer's own provider changes | 1 | 1 | 1 |
All DOM identity and text assertions pass. The problem is avoidable consumer execution, not stale text. Because every context-reading leaf below the memo boundary can be refreshed, the unnecessary work can grow with the retained subtree.
Suspected mechanism and fix direction
provideContext increments the context object's global $$version. Direct dependencies record that context/version pair, and ctxDirectChanged treats a version mismatch as a reason to refresh the consumer without first distinguishing which provider supplied its value. A sibling instance of the same context therefore invalidates an unchanged consumer.
Retain the conservative global epoch and transitive dependency checks, but refine successful local direct reads using their resolved provider and observed value. A consumer whose nearest provider and value remain unchanged should stay skipped; real nearest-provider changes must still propagate. Ambiguous multiple-scope reads, hosted/default reads, reparenting, and suspension/rollback need to retain their correctness guarantees.
Related work includes #1091, which skips dependency scans when no provider has committed, #602, which avoids unrelated scoped-JSX cache rebuilds, and #1977, which preserves context tracking when retained subtrees become memoized. None of those inspected changes appears to address this same-context sibling-provider case.
Activity