Repository navigation
Description
What happens
Calling Entity.keepAlive(false) and then Entity.keepAlive(true) without yielding in between leaves the entity with no keep-alive request held. Both the old request and the new one get a reply, while the caller believes keep-alive is on. The entity is then passivated when idle and is not re-activated after its runner restarts or its shard moves.
This is the same area as #10708 (fixed in #10709, released in 4.1.5), and it is still present after that fix.
Why
Entity.keepAlive(false) only opens the latch (Entity.ts#L802). The held request is released later, by the fiber that waits on the latch in the keep-alive branch of sendLocal (entityManager.ts#L801-L810):
// Redelivery must not acknowledge the request held until the latch opens.
if (server.keepAliveRequestId === message.envelope.requestId) return Effect.void
if (server.keepAliveRequestId !== undefined) return reply
server.keepAliveRequestId = message.envelope.requestId
return server.keepAliveLatch.whenOpen(Effect.suspend(() => {
server.keepAliveRequestId = undefined
return reply
})).pipe(
Effect.forkIn(server.scope, { startImmediately: true }),
Effect.asVoid
)server.keepAliveRequestId is cleared only when that waiting fiber runs again. Entity.keepAlive(true) does not wait for it: it closes the latch and sends a new request (Entity.ts#L809-L810).
- Request A is held, so
keepAliveRequestId === A. keepAlive(false)opens the latch. The waiting fiber is resumed but has not run yet.keepAlive(true)closes the latch and sends request B.- B reaches
sendLocalbefore the waiting fiber has run.keepAliveRequestIdis still A and B has a different id, soif (server.keepAliveRequestId !== undefined) return replyanswers B as a redundant keep-alive. - The waiting fiber runs, clears the id, and replies to A.
A and B both have replies, and nothing is held.
Impact
Anything that releases a keep-alive and takes it again in the same turn loses it silently: for example one piece of work ends and the next starts in the same handler, or a holder count drops to zero and goes back to one. Nothing fails or logs. The entity is passivated after entityMaxIdleTime although work is running, and after a runner restart it is not re-activated, so the work is not resumed.
Reproduction
[email protected] and node:test only. A single in-memory runner, a MessageStorage wrapper that records keep-alive requests and the replies to them, and TestClock. One entity releases and re-enables keep-alive at once; the control entity yields once in between.
import assert from "node:assert/strict"
import { test } from "node:test"
import { Effect, Layer } from "effect"
import {
ClusterSchema,
Entity,
MessageStorage,
RunnerHealth,
Runners,
RunnerStorage,
Sharding,
ShardingConfig
} from "effect/cluster"
import { Rpc } from "effect/rpc"
import { TestClock } from "effect/testing"
const persisted = (tag) => Rpc.make(tag).annotate(ClusterSchema.Persisted, true)
const Holder = Entity.make("Holder", [persisted("Hold"), persisted("Swap")])
// Hold: enable keep-alive
// Swap: release it and enable it again at once; a "yield" entity yields in between
const HolderLive = Holder.toLayer(
Effect.gen(function*() {
const { entityId } = yield* Entity.CurrentAddress
// The keep-alive latch is in the context the handlers are built in
const context = yield* Effect.context()
const keepAlive = (enabled) => Effect.provideContext(Entity.keepAlive(enabled), context)
return Holder.of({
Hold: () => keepAlive(true),
Swap: () =>
keepAlive(false).pipe(
Effect.andThen(entityId === "yield" ? Effect.yieldNow : Effect.void),
Effect.andThen(keepAlive(true))
)
})
})
)
// Advance in 1s steps so every storage poll due on the way runs
const advance = (seconds) =>
Effect.forEach(Array.from({ length: seconds }), () => TestClock.adjust("1 second"), { discard: true })
// Sends Hold, then Swap, and reports the keep-alive requests saved and still unanswered
const swap = (entityId) => {
const requests = new Set()
const answered = new Set()
// Memory storage that records keep-alive requests and the replies to them
const recordingStorage = Layer.effect(
MessageStorage.MessageStorage,
Effect.gen(function*() {
const inner = yield* MessageStorage.MessageStorage
return MessageStorage.MessageStorage.of({
...inner,
saveRequest: (message) => {
if (message.envelope.tag === Entity.KeepAliveRpc._tag) requests.add(String(message.envelope.requestId))
return inner.saveRequest(message)
},
saveReply: (reply) => {
const requestId = String(reply.reply.requestId)
if (requests.has(requestId)) answered.add(requestId)
return inner.saveReply(reply)
}
})
})
).pipe(Layer.provide(MessageStorage.layerMemory))
const ClusterLive = HolderLive.pipe(
Layer.provideMerge(
Sharding.layer.pipe(
Layer.provideMerge(Runners.layerNoop),
Layer.provideMerge(recordingStorage),
Layer.provide([RunnerStorage.layerMemory, RunnerHealth.layerNoop]),
Layer.provide(ShardingConfig.layer({ shardsPerGroup: 4 }))
)
)
)
const held = () => requests.size - answered.size
return Effect.gen(function*() {
const client = (yield* Holder.client)(entityId)
yield* client.Hold(undefined, { discard: true })
yield* advance(10)
assert.deepEqual({ sent: requests.size, held: held() }, { sent: 1, held: 1 }, "after Hold")
yield* client.Swap(undefined, { discard: true })
yield* advance(10)
return { sent: requests.size, held: held() }
}).pipe(Effect.provide(ClusterLive), Effect.provide(TestClock.layer()), Effect.scoped, Effect.runPromise)
}
test("keepAlive(false) then keepAlive(true) leaves one keep-alive held", async () => {
assert.deepEqual(await swap("at-once"), { sent: 2, held: 1 })
})
test("control: with a yield in between, one keep-alive is held", async () => {
assert.deepEqual(await swap("yield"), { sent: 2, held: 1 })
})Run with npm i [email protected] && node --test repro.test.mjs.
Observed (effect 4.1.5, Node 24.16.0)
✖ keepAlive(false) then keepAlive(true) leaves one keep-alive held
+ actual - expected
+ held: 0,
- held: 1,
sent: 2
✔ control: with a yield in between, one keep-alive is held
ℹ pass 1
ℹ fail 1
The result is the same on every run. Two keep-alive requests are saved and both are answered. With one Effect.yieldNow between the two calls, the second request stays held.
Expected
After Entity.keepAlive(true) returns, exactly one keep-alive request is unanswered, whatever was called just before it. Both tests pass.
Versions
[email protected] (current latest on npm). The same lines are on main as of 2026-11-24 (296c20943b): keepAliveRequestId in packages/effect/src/cluster/internal/entityManager.ts.
Possible fix
Not tested. The id of the held request should stop being "held" at the moment the latch is opened, not when the waiting fiber next runs. Two directions:
- Let the latest request win: when a keep-alive with a different id arrives while one is held, answer the old one and hold the new one, and have the waiting fiber clear the id and reply only if
keepAliveRequestIdis still its own request. - Or have
Entity.keepAlive(true)wait until a pending release has been processed before it sends the new request.
Workaround
Yield once between the two calls:
yield* Entity.keepAlive(false)
yield* Effect.yieldNow
yield* Entity.keepAlive(true)
Activity