CDK timer recovery after a lost wake
The SDK heap retained its timers after the physical global wake was lost. Registering later work could leave the earliest deadline unchanged, so the cache skipped the system call that would have armed it. Registration and retry now reassert the earliest live deadline, even when it matches the cache. Cancellation still avoids a system call when the head is unchanged. Timer state · SDK timer API
Free distinguished a freeze before the timer task enters Wasm from a trap
after the IC consumes its wake. The replica prepays the timer task before
consuming the deadline. A trap after that consumption can roll back the
canister's heap while leaving the global timer inactive. Restoring the
balance can wake a retained deadline. A consumed wake still needs a later
message to rearm it. This distinction is visible in the IC Git objects at
49a94778d9aff90139cadc4f9c14b98b2aae42a4, in
rs/execution_environment/src/execution/call_or_task.rs. That source review
is separate from the measured PocketIC executable.
Forum discussion ·
IC timer contract
Motoko issued a dispatcher self-call and discarded its future. A rejection before execution could leave the global wake unset. Its merged fix added a fallback wake and recovered one-shot callbacks after transient rejection. The CDK already observes reject callbacks and keeps the original timer queued after an immediate send failure. The tests exercise both boundaries on the same installed CDK artifact before and after restoring cycles. Motoko issue and tests · Motoko fix
PocketIC cases
The runner uses an application subnet and the pinned PocketIC 16.1.0
executable, SHA-256
6a9a75ad9b4551d2b0f4a0d954ffb8bd5bbd0ebc44ac00290679b06ad0b2f8e6.
The unchanged SDK passed five cases and failed the stale-wake case. The
candidate passed all six, along with the native timer and example suites.
Timer recovery runner
| Case | Assertion |
|---|---|
| Already frozen at the deadline | Fixed nonzero threshold, actual balance below the reserve, frozen query rejection, then top-up and resumed cadence with the same module hash |
| Low balance before dispatch | Trace shows the global hook without a timer executor or callback, then top-up completes the original one-shot once |
| Low balance after dispatch | Trace orders the global hook, burn update, reject callback and recovered executor. One-shot runs once and both periodic modes advance one tick per interval |
| Stop and start | Original pending timers resume across an ordinary stop and start |
| Consumed wake with retained queue | Fault injection removes the physical wake. A later registration recovers the original due timer, then the new one fires at its own deadline |
| Serial callback trap | A real peer call suspends the timer. The callback traps and the same registration completes on subsequent ticks |
The lost-wake fixture clears the global timer directly to reproduce the retained-heap and inactive-wake state. The trap test runs in an application's timer callback. It exercises the separate executor and cleanup path.
Matched benchmarks
Measured 2026-11-25 from clean unified commit
f6c863661ada43a6766448fc84fc6923944dd98b and SDK tree
f47f148539776a12355a513acb7667f3450edeb6. The frozen benchmark candidate
applied the working-tree SDK Rust patch with SHA-256
bacbc507d0ad1bb30e9d7e38bac53f62cadbe305017a5c2b5d561eef133b9100.
The benchmark harness and its inputs were identical. Both sides used
nightly-2026-10-15, canonical wasm64 builds and the PocketIC pin above.
The crypto pair used Clang 21.1.8. The first baseline crypto build needed
its missing pinned Clang resource headers restored before measurement.
Benchmark method
All four screens passed at zero tolerance for increases in their measured comparison metrics. Heavy16 completed 18,000 root workflows, 1,200 child workflows and 594,000 steps, with zero traps and 1,024 verified blocks. Workflow p95 was 11,400 ms, simulated drain time was 29,400 ms, fleet Wasm memory was 504,365,056 bytes and there were 2,434 physical calls on both sides. Replicated instructions fell from 91,452,062,766 to 91,452,051,308. The workload uses one-shot timers. The PocketIC fixture qualifies the periodic recovery behavior separately.
| Screen | Matched rows | Instruction change | Installed Wasm bytes |
|---|---|---|---|
| RRT Heavy16 | 1 | −0.00001253% | 2,385,445 → 2,385,453 |
| Collections | 59 | 0% | 424,872 → 424,872 |
| Compression | 44 | 0% | 328,355 → 328,363 |
| Crypto | 8 | 0% | 3,795,817 → 3,795,785 |
Artifact identities
Wasm and compiler source closure SHA-256 values from the canonical build reports follow. The two test artifacts used the same fixture source with their respective SDKs.
| Scope | Revision | Wasm | Source closure |
|---|---|---|---|
| RRT Heavy16 | Baseline | 4f60196aa470d42bd9d04fd9db3f93567bb5814786c360879442f8379ad7aacb |
f3b7d7a35a708651ebf40e489e470865348a281cce08352108d4fabac17290b6 |
| RRT Heavy16 | Candidate | da9b76bfcd3882abc64da851d216b676d59e9e8cf90c648ead752ea671688302 |
80c7807d28ed2bd06593ca6efccae3795d65f28d64c0f1a92370e04729880bed |
| Collections | Baseline | 0a8b4fcf388465c66fcc4a70ec8c6a7755485c1c623353e602ebc752f01438d5 |
3bab83d0bfa0206f5f32842c202a91b5a00f8a5f2d16adc483b210f0675ed861 |
| Collections | Candidate | ab869507a1912e829473060f53efeecec9766e7cbc1975c6d498a6b6521b7aca |
8a06c479d945b2ef40b20345db6c50e304f8242e33dec74795912f98b27e20ab |
| Compression | Baseline | 4e7a43a05361994b8a29dc405e678933c92dffb0201825471ddfec4858664dc7 |
248ba0f2cba5db2e578658691d0aef4df0e47a8553828f6d8519e868e24a01f0 |
| Compression | Candidate | e1dae9cb619efb449c05f2b381482f854bff2fa84f408e6efd093aa856ef9789 |
f7fe0867f64e6af2e4989f0163ce8c7f933e4cc86924fc99ed8b554790568db9 |
| Crypto | Baseline | fda07625e915792d05379599c84e60c73e9dada65ce1b6a948cdc533b2f211ca |
305d0aab397bb61a39e042c3fff336397c9d479f9b6ee5aaae7d1750871effec |
| Crypto | Candidate | 596ea12b8044a95e4b04ea37ce69a1ce83be05679429d39a011bc1156c3fe793 |
b88fd81a5a7f46432663c2d6357b681098c3af7739795111a38d7977dde7b8e1 |
| Timer fixture | Baseline | 0be1e7be6a35479481acc227794472c894b305fa108443c4731ab6175bb596bf |
a5635a62b1da6414d6636ed950929a663c81b426010c49049a96342df3b1f847 |
| Timer fixture | Candidate | c35e36716539a68bf62220cdd079d5349533fbe078fe26fdde09bfcf422aff01 |
8a5185b7520a435f8b582af8d4f25672610af4bbcfe541928715e501f5dbb596 |
Application futures in the global timer hook
Timer bodies already ran in their own self-call. The global hook used a tracked update wrapper that also drained ready migratory futures after rearming. A trap in that poll rolled back dispatch and rearming, leaving the SDK queue with no physical wake. The global hook now tracks its outgoing callback handles and returns without polling tasks. The private self-call and ordinary callback polling policies are unchanged. This adds no heartbeat or calls. Global hook · Executor contexts
In the added PocketIC case a weak task's destructor wakes a migratory
future after the last runnable application update. The replica's allocation
charge freezes the canister with the original deadline still armed.
After funding, the old hook polls the future in a system task, where
ic0.msg_arg_data_size traps. With isolated dispatch, the same future
reads the private timer self-call's payload in an update and the original
one-shot completes exactly once. The host trace verifies the outcome of
the global timer execution. The control includes the earlier deadline
rearm fix so this test isolates the change to polling.
Regression runner
Replica source at 49a94778d9aff90139cadc4f9c14b98b2aae42a4 was read from
Git objects. rs/embedders/src/wasmtime_embedder/system_api.rs rejects
msg_arg_data_size in a system task and allows it in an update.
rs/execution_environment/src/execution/call_or_task.rs consumes the
physical wake before entering Wasm, after successful cycle prepayment.
An SDK trap or resource exhaustion in the hook itself can still consume
the last wake. A later registration restores the retained queue.
The final runs passed seven PocketIC cases, plus the native timer, native executor and example lifecycle checks. The rearm-only control passed the six earlier cases and reported a real global hook error in the seventh.
Matched benchmarks with isolated dispatch
The exact clean baseline was again f6c863661ada43a6766448fc84fc6923944dd98b
with SDK tree f47f148539776a12355a513acb7667f3450edeb6. The combined
SDK Rust patch is 688789b47179cb2d7b80c34a6bb508c4af148b5532e11a04b79975884df8a710
under SHA-256. This pair used the same pinned PocketIC 16.1.0, compiler
and workload definitions. All four screens passed with zero increases
in their comparison metrics.
Heavy16 executed 18,000 root workflows, 1,200 child workflows and 594,000 steps with zero traps and 1,024 verified blocks. Both runs used 2,434 physical calls, with p95 11,400 ms, drain time 29,400 ms and Wasm memory 504,365,056 bytes. Replicated instructions fell from 91,452,062,766 to 91,451,948,894. Heap page and nonreplicated query instructions matched exactly.
| Screen | Matched rows | Instruction change | Installed Wasm bytes |
|---|---|---|---|
| RRT Heavy16 | 1 | −0.00012452% | 2,385,465 → 2,385,423 |
| Collections | 59 | 0% | 424,872 → 424,874 |
| Compression | 44 | 0% | 328,355 → 328,357 |
| Crypto | 8 | 0% | 3,795,801 → 3,795,779 |
| Scope | Revision | Wasm SHA-256 | Source closure SHA-256 |
|---|---|---|---|
| RRT Heavy16 | Baseline | 6e3911437753c926c95dc78e6e8162ccbccb8b527810fc3e2d9b9496d7e1d7cd |
17e31ec1fcc794f27e7dac264ef8d03f5f7c7c49e3639c7aefb98eabeea06a30 |
| RRT Heavy16 | Candidate | 8e4667e21bca427bb2e6ec0b8d9adc8d7a7b4e8ce8d6f5fc688b79b0b1aa37ac |
ca84e64576fc04ff610840eea0b6407ad8a9b745ca136d01e4b178045ec123cc |
| Collections | Baseline | f2beab49c26f55099b2f72ad42c6e712b2d038ec40917798e90e2c860177a5f7 |
c4324637f45dd884e49ab4156acdf2f96f240b311bc5b65dace5fcc814f367fa |
| Collections | Candidate | 3e5bf8c0321528c88377d89f91d26256336a19bd8fef9b4a78954d84f8c987e4 |
8a526a19b7ff731a9d78cdd0989f60577fe8d9c2ed31c0b8d0a49ae793d43879 |
| Compression | Baseline | 8133f6ea0b70a201fe0afb809a24b25f446ac324b4c5ba872b82abc9623f572c |
77b4523a89a9a515faa490bf5fa02c2ba0429a9d31a7aeaf6fe832774abec6d3 |
| Compression | Candidate | 570feb02e0f51b8080c7b99aa5ed82a0447c399a94655a9fdf63566515ead5ef |
b66fd7a4d71a848486a4e72f8b2db6cbec268890fe1b1cc4c7699f092876cbf2 |
| Crypto | Baseline | 4dc4131dede95875a25f66b2858bc7549dd7e7009bdc56f1fc238437a5c6585f |
b376cf140fd85dd6b2e710eb2bb8c285e0d0db9a9636ccbc33564cb893c3bff8 |
| Crypto | Candidate | 816030f215e1c43a25205cf606a8be45d8ad1dd2a3fdf3b3899f4e79559d6ca3 |
b8789e7200744034f35e1e11f34e2a183c8b50a2aaa5c87c034d39c09c09e1e1 |
| Timer fixture | Rearm-only control | c8e0032fbf99670b5a5615a31db424d33493a57fa749bec78a574c683a98c514 |
0d794c16a5f99c23e0665aaa105f90d59a6402d6689d9ad26f376fce171f2410 |
| Timer fixture | Isolated dispatch | 7350d50d5b5a8cac3c633cb8fbc831fff6b0a9b2d57f2a845a305df38c74314a |
8d34998127d997ce9a82b36305694abdc11fa8a6b551c958a1909ef546369f5f |
Rejection, freeze and top-up
The test plan asked for one canister whose timer self-call is rejected for cycles above the freezing reserve, which then freezes from idle charges and is topped up. Both variants recover on the unchanged Wasm with no heartbeat or wake call. The one-shot runs once. Each interval timer runs its rejected occurrence once and then one tick per regular deadline. Regression runner
The first run used the SDK as it stood after isolated dispatch. The lone one-shot recovered. The run also showed a hook that could prepay its own execution without affording the self-call. It rearmed at the overdue deadline, so it ran in every round and wrote a log line each time. One run recorded 42 consecutive hook executions at about 6 million cycles each before the day's idle charge froze the canister.
The hook now keeps the due timers queued and sets the wake 60 seconds
ahead when the SDK's liquid-balance gate declines a dispatch. It logs the
first decline of an episode. A failed call_perform still retries in the
next round, because queue and callback limits clear within rounds. The SDK
also logs each retried rejection with its reject code and message. The
test ties IC0207 to the private timer request through that line.
Global hook ·
Timer state
| Case | Assertion |
|---|---|
| Lone one-shot, rejected then frozen | Trace orders hook, burn and reject with no executor entry. The refund lets the hook run once more and decline. The query still answers above the reserve. Idle charges then freeze the canister. After the deposit the one-shot runs once within the retry delay |
| Mixed timers, rejected then frozen | Three rejections. A second burn removes the refunds, so the hook cannot prepay again. After the deposit each timer runs once, then both intervals advance one tick at each of the next two deadlines |
| Canceled after rejection | The funded hook redispatches three retries. The cancellation runs before their executor entries. The canceled one-shot and interval never run, including the interval's queued successor. The serial sibling keeps its cadence |
| Low balance before dispatch | One hook execution in ten rounds, one more after the retry delay and one log line. The deposit alone does not run Wasm. The one-shot runs once at the next retry |
| Frozen before the deadline | No Wasm entry across the deadline while frozen |
| Consumed wake with retained queue | A deposit leaves the overdue count at zero until later work registers |
The replica rotates a canister's global timer with its input messages. A
round after the hook ran gives pending ingress the next turn. The mixed
case relies on that order to burn the refunds before the hook returns.
This was read in the IC source at
49a94778d9aff90139cadc4f9c14b98b2aae42a4, in
rs/execution_environment/src/scheduler.rs, and observed on the pinned
PocketIC executable. The same review confirmed that CanisterOutOfCycles
maps to reject code 2, that the reject callback runs on its prepaid cycles
even below the freezing threshold, and that a deposit runs no Wasm.
Limits
A timer that becomes due during the retry delay waits for it, as does a deposit into an unfrozen canister. When some due timers were dispatched and the rest declined, the rest wait for the retry even though the returning calls refund cycles sooner. The matched control on the earlier SDK was the exploratory run above, not a full run of this suite, because the new assertions read log lines that SDK does not write.
A trap in the hook is not an open defect. The hook runs no application code, bounds its own instructions and encodes only fixed-size SDK data, so it traps only when the heap cannot supply its small allocations. After such a trap a registration or a retried rejection rearms the consumed wake. A cancellation that leaves the head unchanged does not. This path was read from source, not tested.
Matched Heavy16 with the retry delay
The base was clean commit f6c863661ada43a6766448fc84fc6923944dd98b. The
candidate was that commit with the complete working-tree change, including
the retry delay and the rejection log. Both used the pinned PocketIC
16.1.0 and nightly-2026-10-15. The comparison passed at zero tolerance.
Both sides completed 18,000 root workflows, 1,200 child workflows and 594,000 steps with zero traps and 1,024 verified blocks. Both used 2,434 physical calls, with p95 11,400 ms, drain time 29,400 ms, heap capacity 504,365,056 bytes and 47,752,000,000 heap page instructions. Replicated instructions fell from 91,452,062,766 to 91,451,950,766, a change of −0.00012247%. That is 1,872 more than the isolated-dispatch candidate above. Installed Wasm grew from 2,385,453 to 2,385,767 bytes. The collections, compression and crypto screens were not rerun for this step.
| Scope | Wasm SHA-256 | Source closure SHA-256 |
|---|---|---|
| RRT Heavy16 candidate | 5f9d20d5ea571bc7c8a51e4453a9b9279c5e8393c3b5d7b73f8184eda5d8733d |
3859faac27f46656614c45aeece7d1c51fdcf9c85d93a420d0a45f6d23b0ef55 |
The same sources passed the native timer tests and all ten PocketIC cases
through tools/test.py run --crate cdk-rsx-timers.
Reactor wake after a discarded timer
Reactor pumps from one-shot timers and saves each handle. The timers crate discards a one-shot whose self-call ends with a reject code other than 2 or 6. A trapped pump rolls back the body's first step, so the saved handle stayed set and every later wake request returned early. The finding and plan
The callback probe gained a pump_fault switch. On the SDK before the fix,
one trapped pump left a saved frame unsent after the fault was cleared and a
simulated day had passed. A stop and start did not help, and a frame from
the peer was not processed.
Each controller timer now captures a guard. When the timers crate drops the timer unrun, the guard clears the stale handle and schedules a pump 60 seconds later. Reactor's own cancellations take the handle first.
| Case | Result with the fix |
|---|---|
| Pump traps once | One timer failed (code 5) log line. The frame stays queued and no further pump runs before the delay. |
| Pump keeps trapping | One more attempt after 60 seconds, not one per round. |
| Fault cleared | The next retry delivers the saved frame once, in order. A later send completes without waiting. |
The existing callback-safety and timer-cancellation runners passed on
the same probe build. The replica-side claims come from a source review at
49a94778d9aff90139cadc4f9c14b98b2aae42a4, whose checkout had local
changes, and were not measured on PocketIC beyond the trapped pump. A wake
in flight when the canister is stopped is rejected with the same code. That
path was not exercised.
Matched Heavy16 with the guard
The base was clean commit f6c863661ada43a6766448fc84fc6923944dd98b. The
candidate was the complete working tree. Both used the pinned PocketIC
16.1.0 and nightly-2026-10-15. The comparison passed its warning gates.
Both sides completed 18,000 root workflows, 1,200 child workflows and 594,000 steps with zero traps and 1,024 verified blocks. Both used 2,434 physical calls, with p95 11,400 ms, drain time 29,400 ms and heap capacity 504,365,056 bytes. The global timer ran 507 times and the timer executor 858 times on both sides.
| Metric | Base | Candidate | Change |
|---|---|---|---|
| Replicated instructions | 91,452,062,766 | 91,618,580,324 | +0.1821% |
| Heap page instructions | 47,752,000,000 | 47,975,840,000 | +0.4688% |
| Installed Wasm bytes | 2,385,429 | 2,386,775 | +1,346 |
The base Wasm size varied by up to 40 bytes between runs with its checkout path. The candidate size and hash were the same in two runs.
The increase is page-access charges. Instructions outside page accounting fell by about 57 million. One response callback dirtied about 1,400 more 64 KiB pages. A second variant kept the discard path in a separate cold function. It showed the same page increase and more instructions, +0.2439%, so the first variant was kept. The cause of the extra pages was not identified. The collections, compression and crypto screens were not rerun.
| Scope | Wasm SHA-256 | Source closure SHA-256 |
|---|---|---|
| RRT Heavy16 candidate | 69bad14915b82984f94070c4b553a744e048e61a2f9176d0d83b8ce9907c5db3 |
975cbdbfca7174259b3823d7d40f9d533fa0983256d0b5df413d7366fa58df72 |