Skip to content

fix(domain): in-memory key maps keep the entry another fiber made first - #3779

Open
StvyaSharma wants to merge 1 commit into
alchemy-run:mainfrom
StvyaSharma:fix/domain-inmemory-view-get-race
Open

StvyaSharma wants to merge 1 commit into
alchemy-run:mainfrom
StvyaSharma:fix/domain-inmemory-view-get-race

Conversation

@StvyaSharma

Copy link
Copy Markdown
Contributor

With InMemory, a view no longer loses an event when a read touches its key for the first time while the view applies that key's first event.

const receipt = yield* api.Tally({ id: "a", by: 1 });
// a read of "a" runs while the view applies the event
yield* api.Tallies.get("a", { atLeast: receipt });
// was, rarely: ConsistencyTimeout after 10s, and "a" stays at { total: 0 }

A view key's get checks the keys map, makes the key's SubscriptionRefs, then sets the key. Making the refs can yield, and the view's reads call get without the view's lock, while apply holds it. When both miss the same key, the one that sets last replaces the other's entry. If the read sets last, the event lands in an orphaned entry.

Each key is now set only if no other fiber set it first:

Effect.flatMap((created) =>
  Effect.sync(() => {
    const filled = keys.get(key);
    if (filled) return filled;
    keys.set(key, created);
    return created;
  }),
),

The stream key's get and the aggregate's versionOf used the same pattern and get the same fix.

The yield comes from Effect's scheduler after MaxOpsBeforeYield ops, so at default settings it is rare. The new "Domain InMemory: view keys" test sets Scheduler.MaxOpsBeforeYield to 1 so it happens on every run.

A view key's get, a stream key's get and an aggregate's version ref were
made after a check and a yield, then set unconditionally. A read (which does
not take the view's lock) touching a key for the first time while the view
applied its first event replaced the entry the event was applied to: the
event was lost and a read atLeast its receipt timed out.
@alchemy-version-bot

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

alchemy

pnpm install https://pkg.alchemy.run/alchemy/pr:3779:ea4f061
@alchemy.run (15)
pnpm install https://pkg.alchemy.run/@alchemy.run/acme/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/hetzner/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/railway/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/fly/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/gcp/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/aws/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/domain/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/kubernetes/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/docker/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/redis/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/sql/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare-runtime/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/frontend-frameworks/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/node-utils/pr:3779:ea4f061
Transitive Dependencies (14)
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/acme/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/core/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/s3/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/hetzner/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/railway/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/fly-io/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws4signer/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/cloudflare/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/iceberg/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/gcp/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/effect-cluster/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@alchemy.run/rivet/pr:3779:ea4f061
pnpm install https://pkg.alchemy.run/@distilled.cloud/rivet/pr:3779:ea4f061

Published Nov 25, 2026, 8:06 AM UTC. Expires Dec 2, 2026, 8:06 AM UTC, extended while this pull request is open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant