Skip to content

fix(store): record an app's organization and forget a user's settings in a detached write - #2301

Merged
Nash0x7E2 merged 8 commits into
mainfrom
fix/ai-3574-detached-cached-writes
Nov 24, 2026
Merged

Nash0x7E2 merged 8 commits into
mainfrom
fix/ai-3574-detached-cached-writes

Conversation

@thesyncim

@thesyncim thesyncim commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem
AppOrganizations.Upsert ran a plain insert on its caller's context and forgot the app's cached rows once, right after. When that context ended while the insert waited on a lock, Postgres could commit after the forget, and a read in between cached the old organization for up to an hour. ForgetUserSettings already ran in a transaction of its own, but forgot once: a COMMIT whose answer was lost got no second forget. The second forget #2209 added for that case was also cut short by Store.Close, which router usage forget-user calls as soon as the write returns.

Fix

  • Both writes run through Store.detachedWrite, as the other cached writes do since fix(store): write a cached row in a detached transaction and forget what it returned #2209. For ForgetUserSettings, its six statements share the store's 10s write timeout, which it never had; a run that exceeds it rolls back and can be run again.
  • A COMMIT whose answer was lost has its keys dropped on each of the next three attempts (100, 300 and 700 ms), not only the first, and Store.Close lets those run for up to 800 ms before it stops them. Close waits that long whenever any retry is pending, a Redis retry included; an attempt that waits on a slow Redis can still be cut short at its third try.
  • The enforcer writes one membership per app at a time; an organization named while one runs waits behind it, the last named winning, and is written on a context of its own rather than that request's. Enforcer.Close waits up to joinTimeout for the write under way and starts none after, a pending one included. Each write now outlives the 2s joinTimeout and is 3 queries where it was 1.
  • Policies.forget had no caller left and is gone.

Tests

  • TestEveryCachedWriteARequestGaveUpOnLeavesNoStaleCopy gains two rows. AppOrganizations.Upsert: a read while the write waits past its caller's deadline leaves no stale organization or app policies (fails on main: the write is cancelled with its caller). ForgetUserSettings: the same for the user's policy and HasUsers.
  • TestAnAppMovedByACommitThatLandsAfterItGaveUpIsForgottenAgain and TestAUsersPolicyForgottenByACommitThatLandsAfterItGaveUpIsForgottenAgain: a proxy holds the COMMIT past the read timeout, a read caches the old rows, and once it lands they are dropped again. The second lets the COMMIT land after the first retry, so it pins the retries after it. On main the first fails only because the upsert sends no COMMIT.
  • TestARunWhoseCommitLandsAfterItGaveUpStillDropsThePolicyARouterCached runs forget-user through such a proxy: the router's copy is gone after the command closes its store.
  • TestAnAppNamingOrganizationsInTurnHasOneMembershipWrittenAtATimeAndTheLastWins (and that the writes end once the last lands), TestAnAppJoiningAnotherOrganizationLaterIsRecordedUnderIt, TestCloseWaitsForAMembershipBeingWritten, TestCloseStopsWaitingForAMembershipAfterJoinTimeout, TestAJoinAfterCloseWritesNoMembership, TestAnOrganizationNamedBehindAWriteIsNotWrittenOnceCloseBegan.

Fixes AI-3574

@thesyncim
thesyncim force-pushed the fix/ai-3574-detached-cached-writes branch 2 times, most recently from 0e684bd to b7978fb Compare November 24, 2026 00:31
@thesyncim
thesyncim marked this pull request as ready for review November 24, 2026 08:39
… in a detached write

AppOrganizations.Upsert and ForgetUserSettings now run through
Store.detachedWrite, so the write outlives its caller and its cached
rows are dropped after the commit, and again after a COMMIT whose
answer was lost.
…, and write one membership per app at a time

Close waits up to forgetGrace for the deletions forgetLater holds, and a
COMMIT whose answer was lost has its keys dropped on every attempt. The
enforcer writes one membership per app at a time, the last organization
named waiting behind it, and Close waits up to joinTimeout for them.
@thesyncim
thesyncim force-pushed the fix/ai-3574-detached-cached-writes branch from b7978fb to 57c00b4 Compare November 24, 2026 10:08
@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 24, 2026
@Nash0x7E2
Nash0x7E2 merged commit 776d387 into main Nov 24, 2026
8 of 12 checks passed
@Nash0x7E2
Nash0x7E2 deleted the fix/ai-3574-detached-cached-writes branch November 24, 2026 11:00
Nash0x7E2 pushed a commit that referenced this pull request Nov 24, 2026
…und (#2352)

**Problem**
Two rules #2301 added to `Enforcer` had no test that failed without
them. `Join` resets an app's pending organization once it starts writing
it; dropping that reset rewrote the membership forever, and no test
noticed, because Close's check of `joinsClosed` ended the loop anyway.
`Enforcer.Close` stops waiting on memberships after `joinTimeout`; the
test of that bound held a row lock, which the server's 3s `lock_timeout`
released before the test's own deadline, so an unbounded wait passed
too.

**Fix**
- Tests only.

**Tests**
-
`TestAnAppNamingOrganizationsInTurnHasOneMembershipWrittenAtATimeAndTheLastWins`
now also checks that the row's `seen_at` stops moving once the
organization named last has landed. It fails when the pending
organization is not reset.
- `TestCloseStopsWaitingForAMembershipAfterJoinTimeout` runs on a store
whose DSN raises `lock_timeout`, so only Close's own bound ends the
wait. It fails when that bound is removed.

Part of AI-3574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review docs project-info

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants