Skip to content

Cloudflare workflow: a DurableClock.sleep of about 60 s or more costs one extra alarm per wake (the resume guard outlives the attempt) #10895

Todo

Description

@RhysSullivan

This issue was drafted by an AI agent and may not have been reviewed by a human.

Package: @effect/platform-cloudflare 4.1.4 (CloudflareWorkflowEngine, internal/workflowRuntime)

What happens

A workflow execution that runs a step and then DurableClock.sleeps for 60 s or more gets one extra alarm per wake. The extra alarm finds no clock due, re-arms, and the real wake follows. With a 60 s sleep, the extra alarm comes early by exactly the time the step took, because the alarm in place is the resume guard armed when the wake started, not the sleep's due time.

Mechanism

The two lines:

// src/CloudflareWorkflowEngine.ts:196, at the sleep
const wakeUp = clock.currentTimeMillisUnsafe() + Math.ceil(Duration.toMillis(opts.clock.duration))

// src/internal/workflowRuntime.ts:140, at the wake's start (resumeGuardMillis = 60_000, line 38)
const armResumeGuard = Effect.suspend(() => armAlarm(options.alarm, options.now() + resumeGuardMillis))

In two sentences: when a clock alarm wakes the execution, runAlarm calls resume (line 324, armResumeGuard) and then sets the alarm outright to options.now() + resumeGuardMillis while the replay is pending (lines 404-414), both read before the replayed step runs; the sleep after the step then computes its due time from a later clock, and scheduleClock (lines 381-385) cannot replace the guard because armAlarm only moves an alarm earlier. Suspension (saveSuspension, line 242) leaves the guard armed, so with a 60 s sleep the guard fires early by the step's duration, runAlarm finds no clock due (line 389) and sets the alarm to the clock, which is the real wake one alarm later.

On Cloudflare Date.now() advances only at I/O, so a step that writes for 1.8 s moves the clock 1.8 s between the guard's read and the sleep's: with a 60 s sleep the early amount equals the step's write time to the millisecond. With a sleep longer than 60 s the guard fires earlier still; with a sleep shorter than 60 s minus the step, the sleep's clock is earlier than the guard and wins, and there is no extra alarm.

Reproduction

A Durable Object workflow, nothing else:

const Loop = Workflow.make("Loop", { payload: { id: Schema.String }, success: Schema.Void, idempotencyKey: ({ id }) => id })
const LoopLive = Loop.toLayer(({ id }) =>
  Effect.gen(function* () {
    for (let i = 0; i < 20; i++) {
      // A step whose storage writes take a second or two (any I/O-heavy step will do).
      yield* Activity.make({ name: `write-${i}`, success: Schema.Void, execute: writeTenRows })
      yield* DurableClock.sleep({ name: `nap-${i}`, duration: "60 seconds" })
    }
  })
)

Run it on Cloudflare (CloudflareCluster / CloudflareWorkflowEngine). Count the execution object's alarm invocations, and compare each alarm's time with the previous step's end + 60 s. A control whose step writes nothing shows no extra alarms.

Measured (one run, 20 wakes each):

workflow alarms for 20 wakes re-armed (extra)
step writes 10 rows (1.3 to 2.2 s per step) 37 17
step writes nothing 20 0

For the extra alarms, how early each came, against the previous step's own write time:

early (ms) the step's write time (ms)
1,769 1,769
2,134 2,134
1,419 1,419
2,003 2,003

15 of the 17 matched to the millisecond. The other 3 of the 20 wakes, whose guard came early by 1,164 to 1,211 ms, cost no extra alarm, most likely because the object's own work before runAlarm read the clock (start-up, about 1.2 s there) took longer than that, so the clock had already passed the due time when it was read (not measured directly). Every wake at least 1,232 ms early re-armed.

A variation that would confirm it, not run: a 90 s sleep should give an extra alarm about 30 s plus the step's time early, and a 30 s sleep none.

Cost

One extra alarm invocation per affected wake (here 17 of 20). That is a Durable Object alarm, a cold start where the object was evicted, and whatever the host does per alarm, for every sleep of 60 s minus the step's time or longer.

Smallest fix I can see

When an attempt suspends with no replay pending, the guard has served its purpose, so set the alarm outright to the earliest clock (as runAlarm's tail already does) instead of leaving the guard in place. In persist, after WorkflowStorage.saveSuspension(sql, text, replay) (line 242), when replay === undefined: options.alarm.setAlarm(earliestClockWakeUp(sql)), or delete the alarm when there is no clock. A few lines; the guard still covers the attempt while it runs.

Alternatively, scheduleClock could replace an alarm that is only the current attempt's guard. That needs to know which alarm is the guard, so the first fix is simpler.

Activity

  1. self-assigned this
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions