Any possibility to run an async function inside `#[pre_upgrade] fn pre_upgrade() {}` function?

I tried the code below. but none of it works.

#[pre_upgrade]
#[trace]
fn pre_upgrade() {

//method1:
  use tokio::runtime::Runtime;
  let rt = Runtime::new().unwrap();
  rt.block_on(async {
    save_payload_to_dropbox();
  });
//wasm doesnt support this code above yet.

//or method2:
  save_payload_to_dropbox_blocking(); 
//use   
//`use futures::executor::block_on;
// match block_on(http_request(request, cycles))`  
//inside save_payload_to_dropbox_blocking()

//returns:
//Failed during wasm installation call: The replica returned a replica error:
//reject code CanisterError, 
//reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai 
//violated contract: "ic0_call_new" cannot be executed in pre upgrade mode, 
//error code None
}
1 Like

No, await is forbidden in init, pre_upgrade, and post_upgrade

What is the technical limitation that does not allow for an async function?

1 Like

It would break a bunch of guarantees you have right now. For example now you 100% can be sure that init is run to completion before any other function ever runs.

But you can emulate an async-allowed function in these function (except pre_upgrade). If you set a timer to run in 0 seconds it’ll run very soon and in there you’re allowed to do async calls

1 Like

by the way. suggestions for viewers of this post:
ic doc dont recommend do any err prone code in pre_upgrade .
cause if it fail during canister upgrade process. Things may get bad .
I am one of the silly guy write buggy code on production canister.
Still strive to fix it yet.