schedule() delayed rows accumulate across OOM crashes, alarm() processes all without dedup
Hi! I ran into a very expensive issue with schedule() and wanted to report it in case others hit the same thing. I might be wrong about some of the SDK internals. Happy to be corrected.
What happened
I'm building an MCP server using Agent with schedule() for periodic maintenance. One of my DOs had a bug where it called schedule(60, "maintenance") during init, which runs on every cold start. This created a new row in cf_agents_schedules on every alarm cycle.
On its own, that would have been a minor issue: one extra row per cycle. But two things amplified it dramatically:
1. Rows accumulate across OOM crashes. The DO also had a memory issue (SDK DDL + app DDL exceeded 128MB). Each crash cycle wrote the schedule row before dying. Because cf_agents_schedules is persisted in SQLite, the rows survived the crash. Over ~4 days of continuous crashes, hundreds of stale rows accumulated, all with time in the past.
2. alarm() batch-processes all stale rows without dedup. Whenever a cycle did survive (which happened intermittently throughout, and consistently after I deployed a fix), alarm() ran:
SELECT * FROM cf_agents_schedules WHERE time <= [now]
It processed every stale row individually, executing my maintenance callback once per row instead of once per unique callback name. So 100 stale rows = 100 maintenance() executions in a single alarm cycle, each doing SQL queries against my tables. Each execution also created a new schedule row (via my finally block), which got picked up by the next alarm cycle, keeping the row count constant.
The combination ran for 21 days across two accounts. My bug caused the accumulating rows. The batch processing without dedup amplified it 100-200x. Total: ~$1,650 in DO SQL row read charges (which is how I found out about the issue... Flagged it to support so hope to get the charges dropped), from a system with 1 user and 270 actual tool calls.
I captured this via the Workers Observability telemetry API. A single 346ms alarm invocation (request ID B7R8TIEFDYLG9CR5) contained 24 diagnosticsChannelEvents on the agents:schedule channel: 12x schedule:execute (processing 12 stale rows) + 12x schedule:create (each callback rescheduling for the next cycle).
What I think the issue is
It looks like scheduleEvery() got dedup in #1049. Calling it multiple times with the same callback returns the existing row instead of creating a duplicate. But schedule() (delayed type) doesn't have this protection:
- Each
schedule(N, "callback")creates a new row with a freshnanoid(9), even if rows for the same callback already exist alarm()processes ALL rows wheretime <= nowwithout deduplicating by callback name- There's no limit on how many rows get processed per alarm cycle
- Stale rows from crashed invocations are never cleaned up
I'm not sure which of these is the actual fix — maybe schedule() creating new rows is fine (it's a one-shot), and the real issue is the batch processing?
Possible related
- #1049:
scheduleEvery()dedup fix (theschedule()equivalent of this) - #1119:
scheduleEveryalarm re-set after restart (closed Mar 18, which is the exact date my OOM rate went from intermittent to 100%. Possibly coincidence, possibly the fix changed alarm timing for my already-broken DOs)
Environment
[email protected][email protected]- DO with SQLite storage, extending
Agent compatibility_date: 2026-02-19
What I'd find helpful
- Should
alarm()deduplicate pendingdelayedrows by callback name before processing? (100 stale "maintenance" rows → 1 execution, delete the other 99) - Should there be a cap on rows processed per alarm cycle to prevent cascades?
- Is there a recommended pattern for using
schedule()that avoids row accumulation if the DO crashes before the scheduled callback runs?
The diagnostics_channel addition (0.7.0?) is enabled troubleshooting it for me.
Ouch that sounds bad. This is a bit tricky to do right without changing semantics, but I made an attempted PR here https://github.com/cloudflare/agents/pull/1154 I think this would've helped you.
Thanks for following up so quickly. Gives a lot of trust.
Felt a bit scared by DO's power today.. Bit hard to get a graps/feel for all the instances that are running and what they're doing. Any way you could chime in on the support request regarding the invoice? Would really appreciate that.
Sure I can try (no promises). Could you email me your account id/worker name/durable object namespace to spai @ cloudflare dot com?
And the support ticket number if you have one
Thanks. Emailed. Totally understand that you can't make any promises. Appreciate the effort regardless the outcome.