Skip to content

Fence cluster reply writes against shard ownership - #10001

Merged
tim-smart merged 15 commits into
mainfrom
agent/nelson/c44548fb044d
Oct 28, 2026
Merged

tim-smart merged 15 commits into
mainfrom
agent/nelson/c44548fb044d

Conversation

@tim-smart

@tim-smart tim-smart commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

A runner that lost its shard lock could still store the final reply of a persisted request. Repro: runner A is paused mid-request and loses its lock (its advisory-lock session is terminated, or its lease expires). Runner B takes the shard and runs the same request. A resumes and finishes first, before its next lock refresh notices the loss. saveReply wrote A's reply unconditionally, so the stale owner decided the stored result. For workflows, that lets storage diverge from what the new owner computed.

Sharding's view of its shards lags lock storage by up to one refresh, or longer while the process is paused. So before storing the WithExit reply of a persisted request, the runner now refreshes that shard's lock through the existing RunnerStorage.refresh, and stores the reply only if the shard comes back. Nothing new on RunnerStorage, and it works with any storage:

  • Advisory locks: a dead lock session fails the refresh.

  • Leases: a lease taken by another runner is missing from the result.

  • Coalescing: concurrent writers on a shard share one refresh of that shard's lock. If the refresh doesn't return the shard, it moves to releasing as in the periodic refresh, so the runner stops serving it straight away.

  • Shutdown: writers are settled from the refresh fiber's exit. A refresh that's interrupted, or never starts because shutdown already closed the sharding scope, rejects the reply rather than leaving its handler waiting and blocking the shard handoff.

  • Rejection: the check runs in the entity manager's Exit branch, for persisted requests only, before the reply write and its retries. A rejection isn't retried. It abandons the request:

    • it's removed from the active requests;
    • local reply handlers are released so callers re-route;
    • its claim is reset so the owner's next poll sees it.
  • WithTransaction requests: a rejected save fails the transaction, rolling back the handler's writes. settle then abandons the request instead of restarting the entity.

  • Cost: one single-shard lock refresh on the reserved lock connection before each persisted final reply, shared between concurrent writers on the same shard.

Choice for review: the reset on rejection. A runner that hasn't applied a lost shard yet could otherwise hide the request from its owner for up to ten minutes. The rejecting refresh already moves the shard to releasing, so the stale runner doesn't pick the request up again.

Not covered here, risk that remains

  • Check-then-write isn't atomic. A stale commit needs the runner to stall between the refresh and the commit while the new owner acquires, resets and reads.
  • Stream chunks and message claims aren't fenced.
  • On Vitess with advisory locks, a forced releaseAll changes the lock connection id behind vtgate (EFF-3009). That's a bug on main, independent of this PR.

Tests

  • ShardFence.integration.test.ts reproduces the stale owner with two runners on PostgreSQL, in advisory and lease mode.
    • How A is paused: A runs on a clock whose timers stop firing, so its lock refreshes, heartbeats and polls stall while its handler can finish.
    • Checks: A's reply must not be stored while it's paused, and B's must be.
    • Result: fails on main and passes here.
  • Sharding.test.ts adds:
    • memory-storage tests for a fenced reply, which must refresh only its own shard, and a fenced transactional reply, whose storage returns no shards from refresh once fenced;
    • a handoff test for a persisted reply that completes while shutdown is unregistering the runner.

Closes EFF-2985

T1 reproduces a runner that lost its shard lock storing its reply.
T2 and T3 target the proposed RunnerStorage.owned API.
A runner that lost its shard lock could still store replies, deciding the
result of requests its new owner was running. Owner-only writes now check
lock storage through the new RunnerStorage.owned, and a rejected write
abandons the request for the owner.
@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 156c41a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

owned must see lease refreshes made after the caller's transaction
started, and must check advisory ownership when its effect runs.
… session current

owned now reads leases outside an ambient transaction, so a REPEATABLE READ
snapshot cannot hide refreshes. Advisory checks read the lock session when
they run. MySQL re-pins its lock session after RELEASE_ALL_LOCKS, as vtgate
moves later locks to a new connection. Document that a storage instance
serves a single runner.
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.31 KB 39.31 KB 0.00 KB (0.00%)
basic.ts 7.16 KB 7.16 KB 0.00 KB (0.00%)
batching.ts 11.04 KB 11.04 KB 0.00 KB (0.00%)
brand.ts 6.84 KB 6.84 KB 0.00 KB (0.00%)
cache.ts 11.47 KB 11.47 KB 0.00 KB (0.00%)
config.ts 22.73 KB 22.73 KB 0.00 KB (0.00%)
differ.ts 21.88 KB 21.88 KB 0.00 KB (0.00%)
http-client.ts 26.33 KB 26.33 KB 0.00 KB (0.00%)
http-router.ts 36.75 KB 36.75 KB 0.00 KB (0.00%)
logger.ts 11.94 KB 11.94 KB 0.00 KB (0.00%)
metric.ts 9.40 KB 9.40 KB 0.00 KB (0.00%)
optic.ts 7.05 KB 7.05 KB 0.00 KB (0.00%)
pubsub.ts 15.93 KB 15.93 KB 0.00 KB (0.00%)
queue.ts 12.72 KB 12.72 KB 0.00 KB (0.00%)
schedule.ts 11.73 KB 11.73 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 13.98 KB 13.98 KB 0.00 KB (0.00%)
schema-binary.ts 40.93 KB 40.93 KB 0.00 KB (0.00%)
schema-class.ts 21.61 KB 21.61 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.47 KB 29.47 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.87 KB 27.87 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.23 KB 15.23 KB 0.00 KB (0.00%)
schema-string.ts 12.23 KB 12.23 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.17 KB 16.17 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.82 KB 38.82 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.04 KB 26.04 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.79 KB 20.79 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 20.93 KB 20.93 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.03 KB 21.03 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.07 KB 26.07 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.12 KB 21.12 KB 0.00 KB (0.00%)
schema.ts 20.71 KB 20.71 KB 0.00 KB (0.00%)
stm.ts 14.13 KB 14.13 KB 0.00 KB (0.00%)
stream.ts 10.28 KB 10.28 KB 0.00 KB (0.00%)

…uisition

owned must not wait for a connection while the caller's transaction holds
the last one, and a shard reacquired after releasing all locks must be
released by a single release.
The T2 lease case now lets A's lease expire in real time before B takes
it, and each runner asks its own storage. A SQLite TestClock case checks
that ownership lasts until the expiration after the last refresh.
In lease mode, owned no longer queries the database. A lease can only
change owner once it has expired, so SqlRunnerStorage keeps a deadline per
shard from its own acquire and refresh results, counted from when each
statement was sent. This avoids both a transaction's stale snapshot and
waiting for a pool connection the caller's transaction holds. Memory
storage now tracks ownership per address.
… batches

A refresh whose result arrives after release or releaseAll must not
restore the lease, and memory refresh must return the shards each runner
holds rather than the last acquisition batch.
Drop the stream, release-race, memory refresh, lazy check and Vitess
reacquisition tests, limit the owned matrix test to pg, and stop asserting
that a fenced request is never redelivered. Restore the memory storage test
from main.
Check lock storage only before storing the WithExit reply of a persisted
request, and abandon the request on rejection, releasing its claim. Revert
the claim, chunk, respond-type and memory storage changes and the Vitess
lock pin. RunnerStorage.owned stays required on the service, and is optional
in Encoded. SqlRunnerStorage answers leases from a local clock keyed by shard,
never cleared on release, and advisory locks from the current session.

T1 now opens A's gate once B holds the shard, since a stale claim by A only
lifts when A abandons the request. T2 uses shards no other test acquires.
T1 now pauses runner A through a clock whose timers stop firing, instead
of faking its lock storage, so A's lock refreshes stall while its handler
finishes. T3's storage fences by returning no shards from refresh. Drop
the tests for RunnerStorage.owned and the SqlRunnerStorage lease clock.
Before storing the final reply of a persisted request, Sharding refreshes
its shard locks and checks the shard is still among them. Concurrent writers
share a refresh that covers their shard, and shards the refresh did not
return move to releasing as in the periodic refresh. Drop RunnerStorage.owned
and every RunnerStorage and SqlRunnerStorage change.

T1 now checks B's reply before resuming A, since A may legitimately own the
shard again once it re-registers.
…efresh

A persisted reply that completes while the runner unregisters, with the
sharding scope already closing, must not hang its entity and block the shard
handoff. A fenced reply must refresh only its own shard's lock.
The fence now refreshes the lock of the reply's shard alone, sharing one
refresh between concurrent writers on that shard. Writers are settled from
the refresh fiber's exit, so a refresh that is interrupted, or never starts
because shutdown closed the sharding scope, rejects the reply instead of
leaving its handler waiting.
`confirmShardLock` keeps the shared refresh as a `Fiber` instead of a
`Deferred` settled from an observer, and never fails: a refresh that
failed, timed out, was interrupted, or never started because shutdown
closed the scope reads as "not owned", so the reply is left for the
next owner instead of being retried and then killing the entity.

`releaseLostShards` now applies a whole refresh result, including the
assignment check and the gauge and latch updates both callers repeated.

In the entity manager the fenced flag is a local of the exit branch
rather than a field on the request, `isShardOwned` is required like the
option next to it, the three places that drop an active request share
one helper, and the transaction settle is a block instead of a nested
ternary.
@effect-janitor effect-janitor Bot added the 4.0 label Oct 28, 2026
@tim-smart
tim-smart merged commit d47478a into main Oct 28, 2026
13 checks passed
@tim-smart
tim-smart deleted the agent/nelson/c44548fb044d branch October 28, 2026 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant