stale-read
A local holds an object read from a parameter, then a call replaces that object, and the local is used again: it still points at the object from before.
Why: const room = client.state.room copies a reference, not the place it came from. If a function called afterwards assigns client.state.room = next (or client.state = ...), the local keeps the old room: the code goes on reading data nobody updates any more, and writes into an object nobody reads. Rust's borrow checker rejects this outright (E0502: a reference into client kept across a call that changes client). In JavaScript most of those calls are fine, so only the real bug is reported: a call that replaces the object, not one that changes it in place. Guide: State without globals.
✕ Rejectedstale-read
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Room = { readonly sentAt: number; readonly slowModeUntil: number };
type Client = { state: { room: Option<Room>; online: boolean } };
function replaceRoom(client: Mut<Client>, room: Room): void {
client.state.room = room;
}
/** Online, the server can answer with an updated room, put in place first. */
export function send(client: Mut<Client>, text: string): boolean {
const room = client.state.room;
if (client.state.online && room) replaceRoom(mut(client), { ...room, sentAt: room.sentAt + 1 });
return text === "/slow";
}
export function post(client: Mut<Client>, text: string): Option<string> {
const room = client.state.room;
if (!room) return "Join a room first.";
const slowed = send(mut(client), text);
return slowed ? `Wait ${room.slowModeUntil - room.sentAt} seconds.` : undefined;
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Room = { readonly sentAt: number; readonly slowModeUntil: number };
type Client = { state: { room: Option<Room>; online: boolean } };
function replaceRoom(client: Mut<Client>, room: Room): void {
client.state.room = room;
}
/** Online, the server can answer with an updated room, put in place first. */
export function send(client: Mut<Client>, text: string): boolean {
const room = client.state.room;
if (client.state.online && room) replaceRoom(client, { ...room, sentAt: room.sentAt + 1 });
return text === "/slow";
}
export function post(client: Mut<Client>, text: string): Option<string> {
const room = client.state.room;
if (!room) return "Join a room first.";
const slowed = send(client, text);
return slowed ? `Wait ${room.slowModeUntil - room.sentAt} seconds.` : undefined;
}The message names the local, the call and what it assigns: room was read from client.state.room before send(), which may assign client.state.room (in replaceRoom): here it can still hold the old value.
✅ Accepted: read it again after the call
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Room = { readonly sentAt: number; readonly slowModeUntil: number };
type Client = { state: { room: Option<Room>; online: boolean } };
function replaceRoom(client: Mut<Client>, room: Room): void {
client.state.room = room;
}
export function send(client: Mut<Client>, text: string): boolean {
const room = client.state.room;
if (client.state.online && room) replaceRoom(mut(client), { ...room, sentAt: room.sentAt + 1 });
return text === "/slow";
}
export function post(client: Mut<Client>, text: string): Option<string> {
if (!client.state.room) return "Join a room first.";
const slowed = send(mut(client), text);
const room = client.state.room;
return slowed && room ? `Wait ${room.slowModeUntil - room.sentAt} seconds.` : undefined;
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Room = { readonly sentAt: number; readonly slowModeUntil: number };
type Client = { state: { room: Option<Room>; online: boolean } };
function replaceRoom(client: Mut<Client>, room: Room): void {
client.state.room = room;
}
export function send(client: Mut<Client>, text: string): boolean {
const room = client.state.room;
if (client.state.online && room) replaceRoom(client, { ...room, sentAt: room.sentAt + 1 });
return text === "/slow";
}
export function post(client: Mut<Client>, text: string): Option<string> {
if (!client.state.room) return "Join a room first.";
const slowed = send(client, text);
const room = client.state.room;
return slowed && room ? `Wait ${room.slowModeUntil - room.sentAt} seconds.` : undefined;
}Or have the call return the new value, and use that.
✅ Accepted: changes made inside the object are fine, the local sees them
✓ Accepted0 diagnostics
ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Room = { messages: string[] };
type Client = { readonly state: { readonly room: Room } };
function addMessage(client: Mut<Client>, text: string): void {
client.state.room.messages.push(text);
}
export function postTwo(client: Mut<Client>): number {
const room = client.state.room;
addMessage(mut(client), `message ${room.messages.length}`);
addMessage(mut(client), "bye");
return room.messages.length;
}ts
import { type Mut, mut } from "@evobug_com/safe-ts-std/mut";
type Room = { messages: string[] };
type Client = { readonly state: { readonly room: Room } };
function addMessage(client: Mut<Client>, text: string): void {
client.state.room.messages.push(text);
}
export function postTwo(client: Mut<Client>): number {
const room = client.state.room;
addMessage(client, `message ${room.messages.length}`);
addMessage(client, "bye");
return room.messages.length;
}How far it looks
- The local is read from a parameter (
client.state.room,const { room } = client.state), or from a function that returns such a path (const room = currentRoom(client)wherecurrentRoomreturnsclient.state.room). - The call replaces it if it assigns that path or one above it (
client.state = ...), itself or in any function it passesclient(orclient.state, ...) to, across files. - The local is used after the call, on a path that can run after it: not in the other branch of an
if, not after areturnthat follows the call.
Not reported
When the checker can't tell, it assumes nothing was replaced:
- a callee it can't see (a package, a callback parameter, a method of an object), a computed key (
client.rooms[i]), or a value stored elsewhere and changed through that; - an assignment of a number, string, boolean or
undefined: a local holding a number is a copy, not a reference; - a local that is only used after the call, never before it: that's keeping the value from before on purpose (
const before = client.state.members; reload(client); compare(before, ...)). So is a local namedprevious...,old...,before...,saved...,initial...,last...ororiginal..., a comparison with the new value (room === client.state.room), and putting it back (client.state.room = previous); - a
letthat is assigned again, and a call made later in a callback.
The same question across an await instead of a call, state read before it and written back from what was read, is stale-across-await's.