From b6f3f1837cff9aadca37756ca695fc5303a547aa Mon Sep 17 00:00:00 2001 From: Will McGinnis Date: Sun, 1 Nov 2026 20:56:06 -0500 Subject: [PATCH] Clarify concurrent settings writes in extension guidance --- skills/javascript/browser-extensions/SKILL.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/skills/javascript/browser-extensions/SKILL.md b/skills/javascript/browser-extensions/SKILL.md index d37777c..4533f18 100644 --- a/skills/javascript/browser-extensions/SKILL.md +++ b/skills/javascript/browser-extensions/SKILL.md @@ -68,8 +68,10 @@ absent from storage, code that spreads a local variable persists a **partial** settings record — and the next reader's `stored || DEFAULTS` keeps the truthy partial, so a missing nested key throws. -Re-read inside the write path, and rebuild over the defaults rather than over -whatever snapshot you happen to be holding: +Re-read inside the write path to avoid carrying an old selection-time snapshot, +and rebuild over the defaults rather than over whatever snapshot you happen to +be holding. This narrows the stale-read window; it does **not** make the update +atomic: ```js async function persistLastIndex(category, index) { @@ -94,6 +96,18 @@ Two things that snippet is doing deliberately: global shared by worker and popup. Mutating the stored copy later reaches back and rewrites your defaults for the rest of the process. +A fresh read still races: worker A reads, popup B saves, then A writes its +whole object and overwrites B's change. Awaiting A's `set` proves completion, +not isolation. If both contexts can update the same key, route their writes +through one owner and serialize the **whole read–merge–write sequence** there; +serializing only `set` leaves both callers free to read the same old value. +Every writer must use that route, including the popup's save handler. + +Test this with a controlled interleaving: hold A after its read, request B's +save, then release A and await both acknowledgements. Assert the final settings +contain both independent changes. A synchronous storage mock or a test that +places B's save only *before* A's fresh read cannot expose the remaining race. + The cleanest version of this rule: **make selection pure and persistence explicit.** A function that picks a random item should return `{ item, category, index }` and write nothing; the caller awaits the persist.