# Embedding wasm - dfx 0.17.0 crashing where previous version works

**URL:** <https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916>\
**Category:** Developers\
**Created:** [February 26, 2024, 10:52pm UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916 "2024-02-26T22:52:48Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [April 9, 2024, 1:07am UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/21 "2024-04-09T01:07:21Z")

</div>

Just hit this issue with `dfx 0.16.1`. Previous deploys (using this same version of dfx) have succeeded.

Here were my steps (I’m using dfxvm):

1. I briefly upgraded from dfx 0.16.1 to 0.18.0 in order to deploy a canister on the same subnet as another canister.
2. I downgraded back to dfx 0.16.1, and attempted to deploy a new wasm for an existing canister. I received this error:

> Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, reject message Canister \<canister\_id\> trapped explicitly: canister\_pre\_upgrade attempted with outstanding message callbacks (try stopping the canister before upgrade), error code None

At first I freaked out thinking my canister had trapped in the pre upgrade hook, but I don’t think this is the case based on the message. I also am using Motoko `stable` for all of my types, so serialization is being done in the background by the language. Finally, none of these `stable` types were changed in the upgrade…so no data was lost (I verified this)

1. After ensuring nothing had been lost, I tried upgrading my canister again and received a different error this time (looks similar to others in this thread)

> Failed during wasm installation call: Candid returned an error: input: 4449444c036d016c01cedfa0a804026d7b010003\_204b7cb7e28621a6f1110913a33e11455d407f584d3cfe91edf6c12c75b6bd00a2204f7deaf907cea359eda7caa340b204e1558cf451da3fe7582a4cb010a57290d020f52735197797ef2a9e1d2b5d7ed8c13bcd1f41fc65a4731742c12b87f2346bc3  
> table: type table0 = vec table1  
> type table1 = record { 1\_158\_164\_430 : table2 }  
> type table2 = vec nat8  
> wire\_type: record { 1\_158\_164\_430 : table2 }, expect\_type: vec nat8

Again, both of these deployment attempts come when using dfx 0.16.1 after downgrading from dfx 0.18.0 back to 0.16.1 using dfxvm.

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [April 9, 2024, 7:46am UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/22 "2024-04-09T07:46:24Z")

</div>

> [@icme](#):
>
> Failed during wasm installation call: The replica returned a replica error: reject code CanisterError, reject message Canister \<canister\_id\> trapped explicitly: canister\_pre\_upgrade attempted with outstanding message callbacks (try stopping the canister before upgrade), error code None

This error means that your canister is still `await`ing another call and can’t be upgraded yet. This should not have something to do with the dfx version

> [@icme](#):
>
> Failed during wasm installation call: Candid returned an error: input: 4449444c036d016c01cedfa0a804026d7b010003\_204b7cb7e28621a6f1110913a33e11455d407f584d3cfe91edf6c12c75b6bd00a2204f7deaf907cea359eda7caa340b204e1558cf451da3fe7582a4cb010a57290d020f52735197797ef2a9e1d2b5d7ed8c13bcd1f41fc65a4731742c12b87f2346bc3  
> table: type table0 = vec table1  
> type table1 = record { 1\_158\_164\_430 : table2 }  
> type table2 = vec nat8  
> wire\_type: record { 1\_158\_164\_430 : table2 }, expect\_type: vec nat8

`1_158_164_430` is `didc encode hash`, so I think this is a problem with the install call and it seems to be trying to use the chunked installer. But if you downgrade to 0.16.1 this shouldn’t happen. What does `dfx --version` report? Did the downgrade work?

---

<div class="post-metadata">

**Author:** ![lwshang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lwshang/32/3394_2.png) [@lwshang](https://forum.dfinity.org/u/lwshang)\
**Post date:** [April 10, 2024, 12:08am UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/23 "2024-04-10T00:08:21Z")

</div>

As Jordan and Severin pointed out, the candid decoding error `record { 1_158_164_430 : table2 }` should be about the `type chunk_hash = record { hash: blob;}` in the management canister API.

## The Conjecture

> Adam: `install_chunked_code` has gone through some rocky API evolution and the error is likely related to this.

Let me elaborate a bit about the circumstance.

At the time of dfx v0.16.0 release, IC replica implemented the new `install_chunked_code` API which didn’t conform the specification. And dfx/agent-rs at that time added the support for such “wrong” replica implementation.

To not break users who have been using the feature of large wasm module with dfx/agent-rs, we decided to keeping the support of both the “wrong” and “correct” `install_chunked_code` API on the replica side.

The replica fix has been [deployed](https://dashboard.internetcomputer.org/proposal/128876) to the mainnet on 04/05/2024. That was later than the release of `dfx` v0.19.0. So the `dfx` bundled `replica` has never had the fix (v0.16.0 ~ v0.19.0).

So it is possible that some client side (agent/CDK) `install_chunked_code` calls conformed the specification but failed against the mainnet (before 04/05/2024) or local replica (dfx v0.16.0 ~ v0.19.0).

## Action

We, the SDK team, has already:

- Updated the `dfx` bundled replica, which includes the fix as deployed on the mainnet.
- Updated `dfx` and `agent-rs` to conform the specification.
- Released Rust CDK (v0.13.2), which adds support for chunk uploads conforming to the specification (no `ic-cdk` version has the incorrect support).

If building `dfx` from source code is feasible, you can try it now and see if the errors disappear.

You may also choose to wait for the upcoming `dfx` release next week.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [April 10, 2024, 4:26am UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/24 "2024-04-10T04:26:38Z")

</div>

> [@lwshang](#):
>
> To not break users who have been using the feature of large wasm module with dfx/agent-rs, we decided to keeping the support of both the “wrong” and “correct” `install_chunked_code` API on the replica side.

From our side, we were never using the chunked code uploads (our wasm has always been under 2MB). Maybe that changed during the last commit, but we never intentionally used any chunking functionality 🤷‍♂️

> [@lwshang](#):
>
> You may also choose to wait for the upcoming `dfx` release next week.

Is there any way to downgrade dfx to get around this issue? I’d prefer not to need to upgrade dfx to the latest version unless necessary.

---

<div class="post-metadata">

**Author:** ![lwshang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lwshang/32/3394_2.png) [@lwshang](https://forum.dfinity.org/u/lwshang)\
**Post date:** [April 10, 2024, 9:15pm UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/25 "2024-04-10T21:15:35Z")

</div>

We added the support for installing large wasm (over 2MiB) in `dfx` v0.16.0. Since then, `dfx` automatically using the chunked upload when the wasm module is greater than 1.85 MiB. So it might be the case that your wasm is larger than 1.85MiB that trigger the chunked upload.

To completely avoid the chunked upload, you may have to downgrade to a version before `dfx` v0.16.0, e.g. v0.15.0.

P.S. I strongly suggest to use the latest `dfx` as much as possible. If you need a new feature from a later release, it might be more difficult to upgrade from a very old version.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [April 10, 2024, 9:41pm UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/26 "2024-04-10T21:41:20Z")

</div>

> [@lwshang](#):
>
> To not break users who have been using the feature of large wasm module with dfx/agent-rs, we decided to keeping the support of both the “wrong” and “correct” `install_chunked_code` API on the replica side.

> [@lwshang](#):
>
> To completely avoid the chunked upload, you may have to downgrade to a version before `dfx` v0.16.0, e.g. v0.15.0.

😞 I think the way this was fixed on the backend to protect large wasm module support then sort of invalidates using dfx 0.16.0-0.19.0 for us even though we weren’t using the chunked wasm endpoint, as we can’t deploy any updates to our canister when using these versions.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [April 10, 2024, 11:08pm UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/27 "2024-04-10T23:08:51Z")

</div>

It turned out that over the past few months our wasm has grown over 2MB and we didn’t notice that it was using the chunked upload endpoint.

Huge thanks to @lwshang that immediately reached out to me and hopped on a call to unblock us by suggesting we use the dfx wasm gzip option for lowering the size of the wasm.

That’s rockstar 🎸 developer customer service!

Although we’re not sure what the exact source of the deployment bug is, with the gzipped wasm we just successfully deployed the canister upgrade and are unblocked 🎉

---

<div class="post-metadata">

**Author:** ![lwshang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lwshang/32/3394_2.png) [@lwshang](https://forum.dfinity.org/u/lwshang)\
**Post date:** [April 11, 2024, 2:42am UTC](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916/28 "2024-04-11T02:42:52Z")

</div>

I finally managed to reproduce the error locally. So I believe I found the real cause of the issue.

## Bug

In `ic-utils` crate, there was a bug in [`InstallBuilder::call_and_wait_with_progress()`](https://github.com/dfinity/agent-rs/blob/7840c34933fc509f443c3045c1c634201ad87e07/ic-utils/src/interfaces/management_canister/builders.rs#L724). It couldn’t handle the case of non-empty chunk store.

`dfx` v0.16.0 to v0.19.0 depended on this buggy `ic-utils` when install wasm larger than 1.85 MiB .

So for the canister installation failures discussed in this forum thread, it’s likely that the chunk store was not empty before the installation.

## Fix

The merged agent-rs [PR](https://github.com/dfinity/agent-rs/pull/545) and sdk [PR](https://github.com/dfinity/sdk/pull/3704) fixed the issue. I verified that the `dfx` built from sdk master branch can handle such cases properly.

> #### EDITED:
> 
> I opened a [PR](https://github.com/dfinity/sdk/pull/3706) which adds a test to make sure that we fixed the real issue.  
> I reproduced **the error** in CI by downgrading agent-rs to a previous buggy version.

The upcoming `dfx` release next week will certainly have the fix.

Before that, there is an **easy remedy** to unblock your current work.

* * *

## Check

You can check if your canister’s chunk store is empty by this command (replace the canister id with yours):

```sh
dfx canister call --ic aaaaa-aa stored_chunks '(record { canister_id = principal "bkyz2-fmaaa-aaaaa-qaaaq-cai" })'

```

The result will be like:

```auto
(
  vec {
    record {
      1_158_164_430 = blob "\96\a2\96\d2\24\f2\85\c6\7b\ee\93\c3\0f\8a\30\91\57\f0\da\a3\5d\c5\b8\7e\41\0b\78\63\0a\09\cf\c7";
    };
  },
)

```

A non-empty `vec` means that the chunk store is non-empty.

## Remedy

If the check above turns out to be positive, it is easy to fix it.

You only need to execute this command (replace the canister id with yours):

```sh
dfx canister call --ic aaaaa-aa clear_chunk_store '(record { canister_id = principal "bkyz2-fmaaa-aaaaa-qaaaq-cai" })'

```

Then you should be able to install your canister.

* * *

Thanks for all your patience!

[Previous page](https://forum.dfinity.org/t/embedding-wasm-dfx-0-17-0-crashing-where-previous-version-works/27916.md?page=1)
