KEEP: Upgrading the Canister, Keeping the Heap
Chapters
- Read the running canister
- The old executable gets ready
- Check the saved image
- Query the same data again
Transcript
Read the running canister
This canister is already running. Its code reports business revision one, and its Store uses memory schema one. The root owns a vector of integers. Its numbered child owns a separate buffer of bytes. We can check where both buffers live before asking the Internet Computer to replace the Wasm. The request selects KEEP, which preserves the canister's Wasm main memory for the upgrade.
The old executable gets ready
Application work drains before the upgrade begins. The old code checks that callbacks and executor tasks have drained and that no Store frame is open. Shutdown then drops App and a separately held reference to the leaf's vector. The leaf keeps its own reference. After SDK cleanup and the final readiness checks, the old code records a clean marker. The incoming Wasm checks this marker before it initializes its statics.
Check the saved image
The startup guard checks the saved platform layout and consumes the clean marker. It resets the SDK's control area and initializes the new program's statics. The retained allocations stay in their pages. The incoming code compares the saved fingerprint with its compiled Catalog. The Catalog covers the data's layout and how the code interprets it. This fixture's business revision can change outside that contract. The fingerprints match. Before the first new allocation, the provider recovers any leftover Runtime pages for reuse. It reads their ownership records. The Nyxus pages are still owned, so both retained buffers stay at their original addresses.
Query the same data again
The restore hook opens the retained root. It takes a fresh reference to the leaf's vector and saves a new App bound to the actor. Then it writes the V1 fingerprint back to the saved Catalog. Ask for state again. The response now says business revision two. Schema one and the saved values match the first response. Both buffers are still at the same addresses. The test then tries a build with an extra field in the V1 root. The Catalog comparison fails before the candidate opens a retained pointer. The IC rolls back the attempt, including the old code's teardown. Business revision two is still running on schema one. To change the schema on purpose, the next build will name the V1 contract it can convert.