Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: drop a message detail read whose message is no longer open (#7242)

[._.]/ Adam Eivy0764b4a797b3
diff --git a/client/src/asyncEffectDisposalConventions.test.js b/client/src/asyncEffectDisposalConventions.test.js
index 6f8457dad0..e403b187f3 100644
--- a/client/src/asyncEffectDisposalConventions.test.js
+++ b/client/src/asyncEffectDisposalConventions.test.js
@@ -258,10 +258,12 @@ function flipFlagEnd(body) {
const end = match.index + match[0].length;
const rest = body.slice(end);
if (!flip.test(rest)) continue;
- // Occurrences outside the declaration: the flip is one, so a flag that is
- // never consulted has exactly one. Any more means it is read somewhere.
- const uses = rest.match(new RegExp(`\\b${name}\\b`, 'g')) || [];
- if (uses.length > 1) return end;
+ // A READ — an occurrence not being assigned to. Counting bare occurrences
+ // would let a second write stand in for a read (`active = true; … active =
+ // false;`), which is the cosmetic guard this check exists to reject. `==`
+ // and `===` are comparisons, so they stay reads.
+ const read = new RegExp(`\\b${name}\\b(?!\\s*=[^=])`);
+ if (read.test(rest)) return end;
}
return -1;
}
@@ -318,6 +320,31 @@ export function unguardedEffects(src) {
return found;
}
+/**
+ * Spans of the nested `{…}` blocks in an effect body that contain a `return`.
+ *
+ * Anything inside one is on a path that leaves the effect, so it cannot reach —
+ * and therefore cannot cancel — a request set up after it. The callback's own
+ * outermost block is excluded: it holds the cleanup's `return`, so counting it
+ * would swallow the whole body.
+ *
+ * Nesting matters (`if (a) { if (b) { setX(); } return; }` bails too), so every
+ * enclosing block is reported, not just the innermost.
+ */
+function bailingBlocks(body) {
+ const spans = [];
+ const open = [];
+ for (let i = 0; i < body.length; i += 1) {
+ if (body[i] === '{') open.push(i);
+ else if (body[i] === '}' && open.length) {
+ const start = open.pop();
+ // depth 1 is the callback body itself
+ if (open.length >= 1 && /\breturn\b/.test(body.slice(start, i))) spans.push([start, i]);
+ }
+ }
+ return spans;
+}
+
/**
* The mirror-image mistake: a lifetime-scoped flip flag in an effect that
* SYNCHRONOUSLY writes a state value appearing in its own dependency array.
@@ -338,8 +365,11 @@ export function unguardedEffects(src) {
* The fix is a request-generation ref: the re-run early-returns without
* bumping, so the in-flight request stays current.
*
- * Only a SYNCHRONOUS write counts. The same setter called inside `.then()` runs
- * after the response has already been applied, so it cannot cancel itself.
+ * Only a SYNCHRONOUS write counts, and only one that can actually reach the
+ * request. The same setter inside `.then()` runs after the response has already
+ * been applied, so it cannot cancel itself; and one inside a block that returns
+ * (`if (!enabled) { setOffset(0); return; }`) is on a path that bails before any
+ * request exists.
*/
export function selfDisposingEffects(src) {
const blanked = blankLexical(src);
@@ -352,16 +382,14 @@ export function selfDisposingEffects(src) {
const { body, deps } = split;
const async = ASYNC_BOUNDARY.exec(body);
if (!async) continue;
- const flagEnd = flipFlagEnd(body);
- if (flagEnd === -1) continue;
+ if (flipFlagEnd(body) === -1) continue;
+ const bailing = bailingBlocks(body);
const depNames = new Set(deps.split(',').map((d) => d.trim()).filter(Boolean));
for (const setter of body.matchAll(SETTER_CALL)) {
if (TIMER_SETTERS.has(setter[2])) continue;
const at = setter.index + setter[1].length;
- // Only between the flag and the request. A setter ABOVE the declaration
- // sits in an early-return branch that bails before the flag exists, so it
- // can never be on the same path as the request it would cancel.
- if (at < flagEnd || at > async.index) continue;
+ if (at > async.index) continue;
+ if (bailing.some(([open, close]) => at > open && at < close)) continue;
// `setLoadingOutput` → `loadingOutput`, the state value it writes.
const stateName = setter[2][3].toLowerCase() + setter[2].slice(4);
if (!depNames.has(stateName)) continue;
@@ -574,6 +602,25 @@ describe('identity-keyed async effects drop superseded responses', () => {
return () => { active = false; };
}, [id]);
`)).toHaveLength(1);
+
+ // A second WRITE is not a read; it must not stand in for one.
+ expect(unguardedEffects(`
+ useEffect(() => {
+ let active = true;
+ active = true;
+ load(id).then(setValue);
+ return () => { active = false; };
+ }, [id]);
+ `)).toHaveLength(1);
+
+ // A comparison IS a read — `==`/`===` must not be mistaken for assignment.
+ expect(unguardedEffects(`
+ useEffect(() => {
+ let active = true;
+ load(id).then((v) => { if (active === true) setValue(v); });
+ return () => { active = false; };
+ }, [id]);
+ `)).toEqual([]);
});
it('does not accept a mounted guard as an identity guard', () => {
@@ -671,6 +718,31 @@ describe('identity-keyed async effects drop superseded responses', () => {
}, [enabled, offset]);
`)).toBe(0);
+ // …but a bare `return` guard above it does NOT put the write on a bailing
+ // path: `setLoading(true)` here still runs, and still re-triggers.
+ expect(flagged(`
+ useEffect(() => {
+ if (loading) return;
+ setLoading(true);
+ let active = true;
+ load(id).then((v) => { if (active) setValue(v); });
+ return () => { active = false; };
+ }, [id, loading]);
+ `)).toBe(1);
+
+ // Nested inside a block that bails, two levels down.
+ expect(flagged(`
+ useEffect(() => {
+ if (a) {
+ if (b) { setOffset(0); }
+ return;
+ }
+ let active = true;
+ load(offset).then((v) => { if (active) setItems(v); });
+ return () => { active = false; };
+ }, [a, b, offset]);
+ `)).toBe(0);
+
// A generation ref is the sanctioned fix, not a violation.
expect(flagged(`
useEffect(() => {
diff --git a/client/src/components/messages/InboxTab.jsx b/client/src/components/messages/InboxTab.jsx
index 7ec2bc8f3e..7106f10454 100644
--- a/client/src/components/messages/InboxTab.jsx
+++ b/client/src/components/messages/InboxTab.jsx
@@ -235,8 +235,13 @@ export default function InboxTab({ accounts }) {
// The URL is the source of truth for the open message. Resolve it from the
// current list when possible, then fetch it directly for a filtered-out or
// not-yet-loaded message so a copied URL still opens on a fresh load.
- // Supersession token for the message-detail read below.
+ // Supersession token plus the live message identity for the detail read
+ // below. Assigned during render so the comparison always sees the CURRENTLY
+ // open message — including on the paths that return without starting a read,
+ // which never reach the token.
const detailRequestRef = useRef(0);
+ const openMessageRef = useRef('');
+ openMessageRef.current = `${messageAccountId}/${messageId}`;
useEffect(() => {
if (!messageId) {
@@ -265,14 +270,20 @@ export default function InboxTab({ accounts }) {
// effect at once and a lifetime-scoped flag would be flipped by its own
// cleanup before the detail arrived. The re-run bumps the token, so the
// newest read is the one that lands however the two resolve.
+ //
+ // The identity check is the other half: selecting an ALREADY-LISTED message
+ // returns above without bumping the token, so without it a late rejection
+ // here would call closeMessage() on the message the user just opened.
const req = ++detailRequestRef.current;
+ const forMessage = openMessageRef.current;
+ const current = () => req === detailRequestRef.current && openMessageRef.current === forMessage;
setLoadedMessage(null);
api.getMessageDetail(messageAccountId, messageId)
.then(message => {
- if (req === detailRequestRef.current) setLoadedMessage(message);
+ if (current()) setLoadedMessage(message);
})
.catch(() => {
- if (req === detailRequestRef.current) closeMessage();
+ if (current()) closeMessage();
});
}, [messageId, messageAccountId, messages, loading, loadedMessage, closeMessage]);