Repository navigation
Description
Description
When using ClusterCron.make() with SQL storage, the InitialRun singleton's message gets silently rejected on pod/process restart if a previous "initial" message still exists in the database. This breaks the cron chain and requires manual database cleanup to fix.
Current Behavior
- First deployment: InitialRun singleton creates a message with
entity_id="initial"andmessage_id="ClusterCron/{name}/initial/run/" - Cron chain works correctly, scheduling subsequent runs with timestamp-based entity_ids
- Pod restarts (or deployment update)
- InitialRun singleton attempts to create a new "initial" message
- Message is silently rejected due to UNIQUE constraint on
message_idincluster_messagestable - Cron chain is broken - no new executions occur
Expected Behavior
Pod restarts should not break the cron chain. Either:
- InitialRun should detect an existing cron chain and skip re-initialization
- InitialRun messages should have unique message_ids (e.g., include timestamp)
- The message should use upsert semantics
Root Cause
The CronPayload class has an empty PrimaryKey:
// From @effect/cluster/src/ClusterCron.ts
class CronPayload extends Schema.Class<CronPayload>("@effect/cluster/ClusterCron/CronPayload")({
dateTime: Schema.DateTimeUtc
}) {
[PrimaryKey.symbol]() {
return "" // Empty string means all InitialRun messages have same message_id
}
// ...
}This results in a deterministic message_id:
ClusterCron/{name}/initial/run/
When the InitialRun singleton runs again, it generates the same message_id, which violates the UNIQUE constraint.
Reproduction Steps
- Create a ClusterCron with SQL storage:
const MyCron = ClusterCron.make({
name: "MyCron",
cron: Cron.parse("*/5 * * * * *").pipe(Either.getOrThrow),
execute: Effect.log("Cron executed")
})- Run the cluster - observe cron executing every 5 seconds
- Stop the process
- Wait a moment
- Start the process again
- Observe that cron no longer executes (InitialRun message rejected)
Workaround
Manually delete the "initial" messages from the database:
DELETE FROM cluster_messages
WHERE entity_id = 'initial'
AND entity_type LIKE 'ClusterCron%';Suggested Fix
Option A: Include timestamp in InitialRun's entity_id or primary key:
const InitialRun = Singleton.make(
`ClusterCron/${options.name}`,
Effect.gen(function*() {
const client = (yield* CronEntity.client)(`initial-${Date.now()}`) // Unique each time
// ...
})
)Option B: Check if cron chain is already active before creating InitialRun message:
const InitialRun = Singleton.make(
`ClusterCron/${options.name}`,
Effect.gen(function*() {
// Check if there's already a pending/scheduled message for this cron
const hasPending = yield* checkPendingMessages(options.name)
if (hasPending) return
const client = (yield* CronEntity.client)("initial")
// ...
})
)Option C: Use upsert semantics for persisted messages (might have broader implications)
Environment
- @effect/cluster version: 0.56.x
- Storage: SQL (PostgreSQL)
- Runtime: Bun
- Platform: Kubernetes
Additional Context
This issue particularly affects Kubernetes deployments where pods are frequently restarted due to:
- Rolling updates
- Node scaling
- Pod evictions
- Crash restarts
This edge case might not be obvious in development but becomes problematic in production Kubernetes environments with frequent pod restarts.
Activity
Necmttn commentedon Jan 7, 2026
update;
temporary deleting 'initial' doesn't help either it happens again.
Necmttn commentedon Apr 14, 2026
Encountered the same empty-PK root cause as a multi-replica race today on
@effect/[email protected]with 2 runner replicas behind HttpRunner +@effect/sql-pg.Repro:
>= 2runner replicas, anyClusterCron. Both replicas independently computeCron.next(...)and callclient.run({ dateTime: next })for the same firing instant. WithCronPayload[PrimaryKey.symbol]() => "", both submits collapse to identical(request_id, kind)tuples on insert intocluster_replies.Postgres error:
Trace evidence (one cron tick, ~1 second apart):
The fix proposed in Smerly/effect-ts-clone#5 (add
id: Schema.StringtoCronPayload, source fromcrypto.randomUUID()at bothclient.runcallsites, returnidfrom PrimaryKey) resolves both the original restart-stall variant of #5960 and this multi-replica race because every submission now produces a uniquemessage_id.We've shipped the same patch via bun patch on our side as a stopgap. Happy to help land an upstream PR if a maintainer can confirm the approach.
nick-inkeep commentedon Oct 15, 2026
On effect 4.0.5 (
mainunchanged), onlycalculateNextRunFromPrevious: truestays broken. Since #5775 the default mode seeds at the next run's ISO time.truestill seeds at the fixedClusterCron/<name>/initial/run/(ClusterCron.ts:95; empty primary key at :160-162), which never matches a chain message. A restart in which the cron registers in time breaks neither mode: the pending next-run row carries the chain.The chain breaks when Sharding fails the pending run because
ClusterCron/<name>is not registered by the deadline (Sharding.ts:782-790; defect reply at :836-845). No database edit is needed for that. A deploy without the cron does it. So does a boot that registers the cron more thanentityRegistrationTimeout(default 1 minute) after its first entity. On v3 the deadline counts from Sharding start (0.60.2 Sharding.ts:554, :580-587), so any boot that slow does it. The default mode then re-seeds.truere-sendsinitial, which hits the processed row and is ignored (SqlMessageStorage.ts:352; :295 on pg), so the cron never runs again.The same address fails the other way. ClusterCron never deletes its processed rows, and if a cleanup job purges them, the next boot's seed runs beside the live chain and that tick runs twice.
Setup for the table below:
effect,@effect/sql-sqlite-nodeand@effect/platform-node4.0.5.effect3.22.2 and@effect/cluster0.60.2.SingleRunner.layer, memory runner storage) over one SQLite file, and one process per deploy (3.5 s, 3 s and 4.5 s after boot).* * * * * *,entityRegistrationTimeout: 1000.calculateNextRunFromPreviousfalsetruefalsetruetruefalsetruefalse¹ The default mode loses that boot too: no seed row is written, and the InitialRun singleton is no longer running at shutdown.
Purging processed rows before a restart, on 4.0.5: with
truethe first tick ran twice (entity ids…T05:54:32.000Zandinitial); withfalsenone did. With the default timeout and a 70 s deploy 2 without the cron, deploy 3 ran 0 times fortrueand 4 times forfalse.Test: fails for
true, passes forfalse(packages/platform/node/test/cluster/ClusterCron.test.ts)