Hi, I have something really weird going on with ic-stable-structures. It started happening today when I refactored some code and ended up with two memories on different crates.
I have the “user” canister, and a shared crate called state. The state crate contains :
four RefCells each containing a different memory Id. It’s much easier for these to be in a separate crate so I can use helper methods that are not in the actor.
This is how I use mimic to set up the user canister
The MEMORY_MANAGER is on the state crate. Then what this does is initiate a UserIndex under memory id 20, and a Sharder under memory_id 21.
This is how the Sharder is initialised :
Ok so this is the PART THAT CRASHES …
2025-02-12 15:53:17.817183509 UTC: [Canister fhj5a-wmaaa-aaaaa-qaa2q-cai] INFO: trying to insert key 'fokw4-aeaaa-aaaaa-qaa3a-cai' shard 'CanisterShard { pid: Principal { len: 10, bytes: [128, 0, 0, 0, 0, 16, 0, 54, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }, users: 0 }'
2025-02-12 15:53:17.817183509 UTC: [Canister fhj5a-wmaaa-aaaaa-qaa2q-cai] Panicked at 'Attempting to allocate an already allocated chunk.', /home/adam/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ic-stable-structures-0.6.7/src/btreemap/allocator.rs:166:9
2025-02-12 15:53:17.817183509 UTC: [Canister b77ix-eeaaa-aaaaa-qaada-cai] Panicked at 'called `Result::unwrap()` on an `Err` value: ResponseError(IcError(CallRejected("IC0503: Error from Canister fhj5a-wmaaa-aaaaa-qaa2q-cai: Canister called `ic0.trap` with message: Panicked at 'Attempting to allocate an already allocated chunk.', /home/adam/.cargo/registry/src/index.crates.io-6f17d22bba15001f/ic-stable-structures-0.6.7/src/btreemap/allocator.rs:166:9\nCanister Backtrace:\nic_cdk::api::trap\nic_cdk::printer::set_panic_hook::{{closure}}\nstd::panicking::rust_panic_with_hook\nstd::panicking::begin_panic_handler::{{closure}}\nstd::sys::backtrace::__rust_end_short_backtrace\nrust_begin_unwind\ncore::panicking::panic_fmt\nic_stable_structures::btreemap::allocator::Allocator<M>::allocate\nic_stable_structures::btreemap::BTreeMap<K,V,M>::allocate_node\nic_stable_structures::btreemap::BTreeMap<K,V,M>::insert\napi::state::shared::sharder::Sharder::register_shard\napi::state::shared::sharder::Sharder::register_shard_api\nstd::thread::local::LocalKey<core::cell::RefCell<T>>::with_borrow_mut\ncanister_user::register::{{closure}}\ncanister_user::__canister_method_init_async::{{closure}}\nic_cdk::futures::waker::wake\nic_cdk::api::call::callback\n.\nConsider gracefully handling failures from this canister or altering the canister to handle exceptions.
Very weird. Ok so what I do now is put in a clear_new() statement at the start of the function.
It works… but now one of the endpoints that should return SubnetIndex returns Sharder instead.
2025-02-12 16:40:44.354824855 UTC: [Canister eefug-cuaaa-aaaaa-qaa4a-cai] Panicked at 'called `Result::unwrap()` on an `Err` value: Deserialize("failed to deserialize: Semantic(None, \"invalid type: map, expected enum\") (Map([(Text(\"pid\"), Bytes([128, 0, 0, 0, 0, 16, 0, 57, 1, 1])), (Text(\"users\"), Integer(Integer(0)))]))")', src/api/src/lib.rs:93:1
I am completely out of ideas. The only thing I can think of is that now the same memory manager is used over two crates it’s corrupting it somehow.
Thanks,
Adam