Repository navigation
fix(store): record an app's organization and forget a user's settings in a detached write - #2301
Merged
Merged
Conversation
thesyncim
force-pushed
the
fix/ai-3574-detached-cached-writes
branch
2 times, most recently
from
November 24, 2026 00:31
0e684bd to
b7978fb
Compare
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.
…start none once Close began
thesyncim
force-pushed
the
fix/ai-3574-detached-cached-writes
branch
from
November 24, 2026 10:08
b7978fb to
57c00b4
Compare
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AppOrganizations.Upsertran 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.ForgetUserSettingsalready 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 byStore.Close, whichrouter usage forget-usercalls as soon as the write returns.Fix
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. ForForgetUserSettings, 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.Store.Closelets 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.Enforcer.Closewaits up tojoinTimeoutfor the write under way and starts none after, a pending one included. Each write now outlives the 2sjoinTimeoutand is 3 queries where it was 1.Policies.forgethad no caller left and is gone.Tests
TestEveryCachedWriteARequestGaveUpOnLeavesNoStaleCopygains 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 andHasUsers.TestAnAppMovedByACommitThatLandsAfterItGaveUpIsForgottenAgainandTestAUsersPolicyForgottenByACommitThatLandsAfterItGaveUpIsForgottenAgain: 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.TestARunWhoseCommitLandsAfterItGaveUpStillDropsThePolicyARouterCachedrunsforget-userthrough 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