#5798·tinygo

wasm: Go timers stall on Cloudflare Workers (runtime.sleepTicks passes fractional milliseconds to setTimeout)

Author: joeblew999Created Oct 1, 2026Updated Oct 1, 2026

On Cloudflare Workers (production, not local workerd), Go timers in a TinyGo -target wasm build stall about half the time: time.Sleep, time.NewTimer and context deadlines don't fire until some unrelated I/O event arrives.

TinyGo 0.42.0, -target wasm, asyncify scheduler, run through syumai/workers-go v0.36.0 (which ships targets/wasm_exec.js).

Cause

runtime.sleepTicks in targets/wasm_exec.js passes a fractional number of milliseconds to setTimeout:

javascript
"runtime.sleepTicks": (timeout) => {
    setTimeout(() => { ... this._inst.exports.go_scheduler(); ... }, Number(timeout) / 1e6);
},

On Cloudflare the clock only moves on I/O (a Spectre mitigation), and after a setTimeout(d) it has moved by exactly d rounded down to a whole millisecond. Measured in plain JavaScript on a deployed Worker, after one fetch:

setTimeout(d) performance.now() advanced Date.now() advanced
1999.7 1999 1999
0.4 0 0
0 0 0
1 1 1
1.5 1 1
300 300 300

So when the scheduler has less than a millisecond left until its next timer, it calls sleepTicks with a sub-millisecond value, the callback runs with the clock unchanged, ticks() is still before the timer's deadline, and it re-arms the same sub-millisecond timeout, indefinitely. The Go timer never fires until another event moves the clock.

A sub-millisecond remainder is common, not a corner case: ticks() is (timeOrigin + performance.now()) * 1e6, about 1.8e18, where float64 has a granularity of 256 ns, so a deadline computed as now + 2s is usually a few hundred nanoseconds off a whole millisecond.

What it looks like

A handler that, after some I/O, waits on context.WithTimeout(ctx, 2*time.Second) in a select: 6 of 10 requests were still open after 8 s. The same binary under local workerd always ends at 2 s, because there the clock is real. Timers created at the very start of a request (before any I/O) fired on time in my probes, which is what made it look intermittent.

Fix

Round the delay up to a whole millisecond:

javascript
}, Math.ceil(Number(timeout) / 1e6));

With that applied from outside (wrapping importObject.gojs["runtime.sleepTicks"]), 16 of 16 requests ended on time, and a 100 s soak test with streams ending every 15 s, plus a 20-minute idle run, passed on Cloudflare. The wrapper I use: https://github.com/joeblew999/orpc-api/blob/main/api-go/worker/tinygo-clock.mjs

Rounding up can only make a sleep at most 1 ms longer, so it should be harmless in browsers and Node. Related, already closed: #5621 (the setTimeout chain in the same function).