Build log · week ending 19 October 2026

Two races, one counter and one forgiving file read

By Tim

Each week I write up what we built on Noisy Brood: what broke, how we fixed it, and what I learned along the way.

A job that wrote over a change

Brood colours the folders it makes in a person’s mailbox, so they don’t look deleted. A background job does this. It reads each folder’s record from our database, asks the mailbox provider what exists, then writes the result back.

Some folders stayed grey. The job had read a record marked due, which means “look at this again”. While it waited on the provider, a person confirmed a rule, and that created the folder and marked the record due a second time. The job then wrote “settled” over it. A true/false flag cannot show that it was set twice, because it was already true.

This is a race condition: two things touch the same data, and the result depends on which gets there first. Our fix is a compare-and-set. The write only goes through if the row is still what the job read (PostgreSQL’s UPDATE documentation covers the WHERE clause it relies on). To make “still what the job read” cover a second mark, every mark now adds one to a marks column. Look at the last line of the guard:

const rows = await sql`
  SELECT parent, home_id, colour, due, marks FROM parent_colours
  WHERE user_id = ${ref.userId}`;
// ...then ask the provider, and write only where nothing moved:
WHERE parent_colours.home_id IS NOT DISTINCT FROM ${was.home_id}
  AND parent_colours.colour IS NOT DISTINCT FROM ${was.colour}
  AND parent_colours.due = ${was.due}
  AND parent_colours.marks = ${was.marks}

I also moved the read to before the provider call. A change made during the slow call then always lands after the read, so the guard sees it.

Checks that crashed on a deleted folder

Our checks walk the repository’s files. One crashed, now and then, with an ENOENT error: “no such file”. That is the code the operating system returns when a path is gone (Node’s error documentation lists it). Another process, a browser test, was deleting its results folder while the walk listed it.

The walk now treats a file or folder that disappears beneath its starting point as not there. Anything else still throws, and so does a missing starting point, because that is a real mistake:

export function readUnlessGone(path) {
  try {
    return readFileSync(path);
  } catch (e) {
    if (e?.code !== "ENOENT") throw e;
    return null;
  }
}

The test removes the folder inside the read itself, so it does not depend on timing. It also checks that a permissions error still fails.

Why it matters

A folder that stays grey looks deleted, and a person might remove one Brood still needs. The fix makes the colour right without anyone checking.

Noisy Brood files away the mail that doesn’t need you, so the mail that does can find you. We write up what we build each week because every fix like this one is meant to make that a little more reliable. It isn’t open to everyone yet. If you’d like to hear when it can take you, join the list.

All posts