motoko icon indicating copy to clipboard operation
motoko copied to clipboard

experiment: weird stable upgrade failure

Open crusso opened this issue 4 months ago • 1 comments

I don't understand why upgrading from version0 to version1 fails, while version2 is ok. I'd expect the upgrade to version1 to be ok too:

(The only difference is that verson2 retains a use to persistent function f, but both define f.

version0.mo

import Prim "mo:prim";

persistent actor {
    persistent func f() : {a : Int; b : Int} {
        {a=0; b=0};
    };

    stable let g = f;
    assert g().a == 0;

    Prim.debugPrint "version0";
}

version1.mo

import Prim "mo:prim";

// why does upgrading tois fail? Both f and g are declared with the same type (but th
ere is no-reference to f)

persistent actor {
    persistent func f() : {a: Int; b: Int} {
        {a = 1; b = 1};
    };


    stable let g : persistent () -> {a:Int; b: Int} = Prim.trap "unreachable";
    assert g().a == 1;
    // stable let h : Any = f; // why does upgrade fail without this reference to f?
    Prim.debugPrint "version1";

}

version2.mo

import Prim "mo:prim";

persistent actor {
    persistent func f() : {a: Int; b: Int} {
        {a = 1; b = 1};
    };


    stable let g : persistent () -> {a:Int; b: Int} = Prim.trap "unreachable";
    assert g().a == 1;
    stable let h : Any = f; // why does upgrade fail without this reference to f?
    Prim.debugPrint "version2";

}

Output:

crusso@crusso-Virtual-Machine:~/motoko/test/run-drun$ more ok/stable-weirdness.drun.ok 
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101
debug.print: version0
ingress Completed: Reply: 0x4449444c0000
ingress Err: IC0503: Error from Canister rwlgt-iiaaa-aaaaa-aaaaa-cai: Canister called
 `ic0.trap` with message: 'RTS error: Incompatible upgrade: Stable function f is miss
ing in the new program version'.
Consider gracefully handling failures from this canister or altering the canister to 
handle exceptions. See documentation: https://internetcomputer.org/docs/current/refer
ences/execution-errors#trapped-explicitly
debug.print: version2
ingress Completed: Reply: 0x4449444c0000

crusso avatar Sep 15 '25 12:09 crusso

Comparing from c88e5ef019076f590b48653ec6bd281bc46fcad7 to da5e15155c5a11d11f6babb6c051ad96ea27c076: The produced WebAssembly code seems to be completely unchanged. In terms of gas, no changes are observed in 5 tests. In terms of size, no changes are observed in 5 tests.

github-actions[bot] avatar Sep 15 '25 12:09 github-actions[bot]