# Wrong magic number post\_upgrade Rust

**URL:** <https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686>\
**Category:** Developers\
**Created:** [July 3, 2021, 4:58pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686 "2021-07-03T16:58:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 3, 2021, 4:58pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/1 "2021-07-03T16:58:28Z")

</div>

This is the first time I have attempted to do an upgrade and preserve state in Rust. I can’t even get a simple vector to persist. Here’s some simplified code:

```auto
#[pre_upgrade]
async fn pre_upgrade() {
    let numbers: Vec<i32> = vec![1, 2, 3];
    ic_cdk::storage::stable_save((numbers,)).expect("this should work");
}

#[post_upgrade]
async fn post_upgrade() {
    let (numbers,): (Vec<i32>,) = ic_cdk::storage::stable_restore().expect("this should work");
}

```

When I deploy I keep getting the following errors. I’ve tried a combination of different values and I always get the same error:

`Panicked at 'this should work: "Custom(\"wrong magic number [0, 0, 0, 0]\")"', canisters/graphql/src/graphql.rs:38:80`

---

<div class="post-metadata">

**Author:** ![roman-kashitsyn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/roman-kashitsyn/32/3802_2.png) [@roman-kashitsyn](https://forum.dfinity.org/u/roman-kashitsyn)\
**Post date:** [July 3, 2021, 7:56pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/2 "2021-07-03T19:56:40Z")

</div>

Does it work if you make the functions non-async?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 3, 2021, 8:57pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/3 "2021-07-03T20:57:49Z")

</div>

I just tried and that has no effect. I’ve also noticed that my `pre_upgrade` function does not seem to be called (it is not logging), so perhaps `post_upgrade` is looking for a value that doesn’t exist. I’ll keep digging.

---

<div class="post-metadata">

**Author:** ![roman-kashitsyn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/roman-kashitsyn/32/3802_2.png) [@roman-kashitsyn](https://forum.dfinity.org/u/roman-kashitsyn)\
**Post date:** [July 3, 2021, 8:59pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/4 "2021-07-03T20:59:13Z")

</div>

Which dfx command do you use to start the upgrade?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 3, 2021, 9:09pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/5 "2021-07-03T21:09:51Z")

</div>

I’m getting strange intermittent behavior, not sure what’s going on. I’ve got it working now, but not sure what made it work. I’m using `dfx deploy canister-name`.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 3, 2021, 9:11pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/6 "2021-07-03T21:11:27Z")

</div>

Actually, it might be working as intended, I might just be confused by the fact that the pre\_upgrade and post\_upgrade won’t run as written until the deployment after they are part of the binary. Am I thinking of this correctly?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 3, 2021, 9:20pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/7 "2021-07-03T21:20:39Z")

</div>

I have no more issues with the example, even with async functions. I was probably confused by not deploying enough times to see the effects.

---

<div class="post-metadata">

**Author:** ![roman-kashitsyn](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/roman-kashitsyn/32/3802_2.png) [@roman-kashitsyn](https://forum.dfinity.org/u/roman-kashitsyn)\
**Post date:** [July 3, 2021, 9:26pm UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/8 "2021-07-03T21:26:00Z")

</div>

Ah, right, when you upgraded from the old version V1 that didn’t have upgrade hooks to the new version V2 that had them, (non-existing) pre\_upgrade was called on V1 and post\_upgrade was called on V2, which caused post\_upgrade to fail because it wasn’t ready to handle empty stable memory.

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [July 4, 2021, 8:37am UTC](https://forum.dfinity.org/t/wrong-magic-number-post-upgrade-rust/5686/9 "2021-07-04T08:37:14Z")

</div>

Yeah it can be fun getting your head around this when making changes can’t it!

For others reading this thread—there’s a bit of time-travel thinking involved in this: changes to post\_upgrade will execute when you deploy them, changes to pre\_upgrade will execute on the deploy /after/ that one.
