Weird error: I don't understand why execution is interrupted

I have:

        Debug.print("A6");
        let inst = await pm.installPackageWithPreinstalledModules({
            packageName = "icpack";
            version = "0.0.1"; // TODO: should be `"stable"`
            preinstalledModules = [("frontend", frontend)];
            repo;
            caller;
            callback = ?bootstrapBackendCallback;
        });
        Debug.print("A7");

and

    public shared({caller}) func installPackageWithPreinstalledModules({
        packageName: Common.PackageName;
        version: Common.Version;
        preinstalledModules: [(Text, Principal)];
        repo: Common.RepositoryPartitionRO;
        caller: Principal;
        callback: ?(shared ({
            installationId: Common.InstallationId;
            can: Principal;
            caller: Principal;
        }) -> async ());
    })
        : async {installationId: Common.InstallationId}
    {
        Debug.print("B1");
        onlyOwner(caller);
        Debug.print("B2");

        await* _installPackage({caller; packageName; version; preinstalledModules = ?preinstalledModules; repo; callback});
    };

How so that A6 is printed and B1 isn’t?!

...

2024-10-06 17:54:12.074027733 UTC: [Canister by6od-j4aaa-aaaaa-qaadq-cai] A6

2024-10-06 17:54:13.406173747 UTC: [Canister 6zqe7-uuaaa-aaaaa-qacia-cai] Indirect caller: IC0503: Error from Canister 66rcl-zmaaa-aaaaa-qaciq-cai: Canister called ic0.trap with message: IDL error: unexpected IDL type when parsing Principal.

Consider gracefully handling failures from this canister or altering the canister to handle exceptions. See documentation: http://internetcomputer.org/docs/current/references/execution-errors#trapped-explicitly

There isn’t anything between A6 and B1 that would prevent B1 to be printed. That a one-way method fails with an error (Indirect caller error is printed by Debug.print in an one-way tunction) during execution of the ongoing code should not prevent B1 to be printed… I misunderstand something.

Your candid types are off so the function is never being called…not at. Place to inspect, but check that your types exactly line up.

Something is wrong with a principal. It expects one but finds something else. opt principal maybe?

I told that I also have a trouble with a one-way function. (I do understand that I pass a wrong type to it.) But why A6 is printed and B1 isn’t. Why? It is in another fiber of execution and should not be affected by the calling (or not) the one-way function. What is wrong HERE?

@skilesare The following runs just fine (despite the one-way function fails), printing all the 1000 numbers:

import Debug "mo:base/Debug";

actor {
  public shared func greet() : async () {
    oneWay();
    var i = 0;
    while (i < 1000) {
      Debug.print(debug_show(i));
      i += 1;
    };
  };

  public shared func oneWay(): () {
    let _ = 1 / 0;
  }
};

But my function in a way that I can’t understand fails to print B1. Why?! Is refusal to print B1 somehow related with calling a one-way function above in my code (not shown)?

InstallPackageWithPreinstall is never getting executed because the canister can’t interpret your parameters

The trap is from
Message parsing code not from inside your function.

‘’’
Canister called ic0.trapwith message: IDL error: unexpected IDL type when parsing Principal.
‘’’

See Motoko: Interesting one shot behavior - #2 by claudio for some one shot explanations.

How so, “canister can’t interpret your parameters”? I call a Motoko function directly, not through some fancy e.g. management canister method. It looks like that the parameters cannot be mis-interpreted (but they are, how so?)

And yes, “Message parsing code not from inside your function” because it starts with Indirect caller:, this way I log exceptions in a one-way function (so, yes, not from inside my function).

What I don’t understand: If message passing to my function is correct, then why isn’t B1 printed?!! The message passing bug is in another one-way function (not from inside my functions). Then how come that control does not reach Debug.print("B1")?!

I’ve realized what happened:

The IC.install_code is run before my code in an one-way function. And it is either not installed altogether or installed to late (after await pm.installPackageWithPreinstalledModules(...). So, I am calling a canister with no code in it.

That makes sense. The error comes from terraform-provider-ic/internal/provider/arg.go at aec0f7d61e5e3e0c2920db6f5394ef1a23a3d0cb · dfinity/terraform-provider-ic · GitHub which is in the terraform provider.