From 65e0c7f4b1b484517364d081ceca2b588d077e4c Mon Sep 17 00:00:00 2001 From: Fusee Date: Fri, 3 May 2024 17:30:19 +0200 Subject: [PATCH] Partially fixed memory leaks --- Cargo.lock | 28 +++++++-------- abi-build/src/generator/impl_contract.rs | 2 ++ core/Cargo.toml | 10 +++--- core/src/lib.rs | 5 +++ core/src/utils/mod.rs | 1 + core/src/utils/static_request_arc.rs | 46 ++++++++++++++++++++++++ data/Cargo.toml | 6 ++-- executor/Cargo.toml | 6 ++-- mocking/Cargo.toml | 4 +-- tester/contract/Cargo.toml | 4 +-- tester/contract/meta/Cargo.toml | 2 +- tester/contract/wasm/Cargo.toml | 2 +- token/Cargo.toml | 6 ++-- 13 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 core/src/utils/mod.rs create mode 100644 core/src/utils/static_request_arc.rs diff --git a/Cargo.lock b/Cargo.lock index ab141d1..59cb5ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1033,9 +1033,9 @@ checksum = "b59072fa0624b55ae5ae3fa6bfa91515bbeb4ac440214bc4a509e2c8806d6e9f" [[package]] name = "multiversx-sc" -version = "0.47.5" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f2a40b8535885488f1449ba6f03e05e6013c2ff73c89c54f94dcdc0af7c8084" +checksum = "23f5fa49bfb4505465784dc6b8b1d5e52ffcdafedfc2c6f5e9ba54b4bcf5cee8" dependencies = [ "bitflags 2.4.2", "hex-literal", @@ -1046,9 +1046,9 @@ dependencies = [ [[package]] name = "multiversx-sc-codec" -version = "0.18.6" +version = "0.18.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1da6db65170105c9495848c5e4ba388abb1f9201ff2ca362056c9328f36b7760" +checksum = "2f7711088da6b140257f35a25d90dc4f78bc4e2be324d1096f7f5842dd397cda" dependencies = [ "arrayvec", "multiversx-sc-codec-derive", @@ -1057,9 +1057,9 @@ dependencies = [ [[package]] name = "multiversx-sc-codec-derive" -version = "0.18.6" +version = "0.18.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "631c4d4b37fc94659c8d6cf559c21b68c68899095201de2e1b779fccad7b0b03" +checksum = "5dc92c79e975de4b4f956d54d2c31aaca5e18310e1c36d37593fe856a07b8130" dependencies = [ "hex", "proc-macro2", @@ -1069,9 +1069,9 @@ dependencies = [ [[package]] name = "multiversx-sc-derive" -version = "0.47.5" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73567b0b79730b4f6000c2fac4a21ea7ba8c8b12aa32901dd8ff10ccb90cfd79" +checksum = "7f114e0cd72950ae1f0dc1bca2da7d2a02f6ce51bcbee7721980340436bfe63a" dependencies = [ "hex", "proc-macro2", @@ -1082,9 +1082,9 @@ dependencies = [ [[package]] name = "multiversx-sc-meta" -version = "0.47.5" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3522f14ef47b1516a6635e86e0c19acffd8d328eb194a90c29b05d09a22c5296" +checksum = "4ce1f4557894ea3b556d3376d4a12c0494b8085a69c7f02def42bed0702ade58" dependencies = [ "clap", "colored", @@ -1109,9 +1109,9 @@ dependencies = [ [[package]] name = "multiversx-sc-scenario" -version = "0.47.5" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd121a50905b4da3f85750ab3c4f283738245ad22e619b50da2588d1528e4240" +checksum = "fa109d85d90599c232c2c4ac19a0f6c12a558ad72aa04f4b40ff9e23365b1897" dependencies = [ "base64", "bech32", @@ -1137,9 +1137,9 @@ dependencies = [ [[package]] name = "multiversx-sc-snippets" -version = "0.47.5" +version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6bbfa67c15e7fe3e136eed39fa2cc64fadac7885c09a80481578c6dfbfac355" +checksum = "c6373fe0040e72d633e0ccc6d2e231d2f2ce9f4fdaaf7ad0b937d8c6bda4a53a" dependencies = [ "base64", "env_logger", diff --git a/abi-build/src/generator/impl_contract.rs b/abi-build/src/generator/impl_contract.rs index 7cf565d..18a3041 100644 --- a/abi-build/src/generator/impl_contract.rs +++ b/abi-build/src/generator/impl_contract.rs @@ -375,6 +375,8 @@ fn impl_abi_endpoint_call_query(contract_info_name: &str, abi_endpoint: &AbiEndp let (endpoint_args_let_statements, endpoint_args_inputs) = impl_endpoint_args_for_call(&abi_endpoint.inputs, abi_types)?; let common_token = quote! { + let _novax_request_arc = crate::utils::static_request_arc::get_static_request_arc_clone(); + let _novax_contract_address_value: AddressValue = (&Address::from(&self.contract_address)).into(); let mut _novax_contract = #contract_info_ident::new(&_novax_contract_address_value); // unnecessary clone when calling diff --git a/core/Cargo.toml b/core/Cargo.toml index f9eac36..e68153d 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -22,12 +22,12 @@ async-trait = "0.1.72" serde = "1.0.177" serde_json = "1.0.104" tokio = "1.29.1" -multiversx-sc = "=0.47.5" -multiversx-sc-snippets = "=0.47.5" +multiversx-sc = "=0.48.1" +multiversx-sc-snippets = "=0.48.1" multiversx-sdk = "=0.3.2" -multiversx-sc-scenario = "=0.47.5" -multiversx-sc-codec = "=0.18.6" -multiversx-sc-codec-derive = "=0.18.6" +multiversx-sc-scenario = "=0.48.1" +multiversx-sc-codec = "=0.18.7" +multiversx-sc-codec-derive = "=0.18.7" reqwest = "0.11.18" hex = "0.4.3" diff --git a/core/src/lib.rs b/core/src/lib.rs index 3329f82..59662d2 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -60,6 +60,11 @@ pub mod code; /// The `account` module provides structures and functionalities for handling and obtaining account information. pub mod account; +/// The `utils` module provides some helpers to make the framework working. +pub mod utils; + + + // Include the generated client code from the output directory. include!(concat!(env!("OUT_DIR"), "/generated_lib.rs")); diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs new file mode 100644 index 0000000..1b49f97 --- /dev/null +++ b/core/src/utils/mod.rs @@ -0,0 +1 @@ +pub(crate) mod static_request_arc; \ No newline at end of file diff --git a/core/src/utils/static_request_arc.rs b/core/src/utils/static_request_arc.rs new file mode 100644 index 0000000..b423910 --- /dev/null +++ b/core/src/utils/static_request_arc.rs @@ -0,0 +1,46 @@ +use std::sync::{Arc, Mutex}; +use multiversx_sc_scenario::api::StaticApi; + +thread_local! { + /// Useful to know when we can call `StaticApi::reset()` safely. + /// Each request will clone this value, increasing the strong references count by one. + /// + /// After each request, the cloned value will be released, implying the reference count to decrease by one. + /// + /// With this mechanism, we can ensure that a call to `StaticApi::reset()` is safe is the strong references count is 1 (the one in this file) + /// Otherwise, calling result would lead to bad behaviors for ongoing requests that have ManagedTypes instantiated. + static STATIC_REQUEST_ARC: StaticRequestArcWrapper = StaticRequestArcWrapper::default(); +} + +#[derive(Clone, Default)] +pub struct StaticRequestArcWrapper { + /// Locked only while the API is being reset. + /// So no new request is launched while the reset. + locker: Arc>, + /// If the count of arc is 1 there is no ongoing request. + /// It means that the API can be safely reset. + arc: Arc<()> +} + +impl Drop for StaticRequestArcWrapper { + fn drop(&mut self) { + // The arc is only dropped after the drop function. + // Therefore, we have to anticipate. + let arc_strong_count_after_drop = Arc::strong_count(&self.arc) - 1; + if arc_strong_count_after_drop == 1 { + let guard = self.locker.lock().unwrap(); // Ensure no new request is initiated + StaticApi::reset(); + drop(guard); + } + } +} + +pub fn get_static_request_arc_clone() -> StaticRequestArcWrapper { + STATIC_REQUEST_ARC.with(|e| { + // First we don't want to perform any operation if the API is being reset + let guard = e.locker.lock().unwrap(); + drop(guard); + + e.clone() + }) +} diff --git a/data/Cargo.toml b/data/Cargo.toml index 9242554..06abf17 100644 --- a/data/Cargo.toml +++ b/data/Cargo.toml @@ -10,9 +10,9 @@ repository = "https://github.com/gfusee/novax" [dependencies] num-bigint = "0.4.3" -multiversx-sc = "=0.47.5" -multiversx-sc-scenario = "=0.47.5" -multiversx-sc-codec = "=0.18.6" +multiversx-sc = "=0.48.1" +multiversx-sc-scenario = "=0.48.1" +multiversx-sc-codec = "=0.18.7" multiversx-sdk = "=0.3.2" serde = "1.0.183" base64 = "0.21.3" diff --git a/executor/Cargo.toml b/executor/Cargo.toml index 30fbf6c..6d82a9f 100644 --- a/executor/Cargo.toml +++ b/executor/Cargo.toml @@ -18,10 +18,10 @@ hex = "0.4.3" base64 = "0.21.3" novax-data = { path = "../data", version = "0.1.5" } novax-request = { path = "../request", version = "0.1.5" } -multiversx-sc = "=0.47.5" -multiversx-sc-scenario = "=0.47.5" +multiversx-sc = "=0.48.1" +multiversx-sc-scenario = "=0.48.1" multiversx-sdk = "=0.3.2" -multiversx-sc-snippets = "=0.47.5" +multiversx-sc-snippets = "=0.48.1" [dev-dependencies] serde_json = "1.0.105" diff --git a/mocking/Cargo.toml b/mocking/Cargo.toml index 70e327f..f31afec 100644 --- a/mocking/Cargo.toml +++ b/mocking/Cargo.toml @@ -15,8 +15,8 @@ tokio = "1.30.0" novax = { path = "../core", version = "0.1.5" } novax-token = { path = "../token", version = "0.1.5" } novax-executor = { path = "../executor", version = "0.1.5" } -multiversx-sc = "=0.47.5" -multiversx-sc-snippets = "=0.47.5" +multiversx-sc = "=0.48.1" +multiversx-sc-snippets = "=0.48.1" multiversx-sdk = "=0.3.2" reqwest = "0.11.18" serde = "1.0.180" diff --git a/tester/contract/Cargo.toml b/tester/contract/Cargo.toml index 647e0ca..715d5be 100644 --- a/tester/contract/Cargo.toml +++ b/tester/contract/Cargo.toml @@ -10,10 +10,10 @@ license = "GPL-3.0-only" path = "src/lib.rs" [dependencies] -multiversx-sc = "=0.47.5" +multiversx-sc = "=0.48.1" [dev-dependencies] -multiversx-sc-scenario = "=0.47.5" +multiversx-sc-scenario = "=0.48.1" [package.metadata.release] release = false diff --git a/tester/contract/meta/Cargo.toml b/tester/contract/meta/Cargo.toml index 95d1b9c..fc26f1c 100644 --- a/tester/contract/meta/Cargo.toml +++ b/tester/contract/meta/Cargo.toml @@ -8,7 +8,7 @@ authors = [ "you",] [dev-dependencies] [dependencies] -multiversx-sc-meta = "=0.47.5" +multiversx-sc-meta = "=0.48.1" tester-contract = { path = ".." } [package.metadata.release] diff --git a/tester/contract/wasm/Cargo.toml b/tester/contract/wasm/Cargo.toml index b69e038..c3dae32 100644 --- a/tester/contract/wasm/Cargo.toml +++ b/tester/contract/wasm/Cargo.toml @@ -22,4 +22,4 @@ panic = "abort" [dependencies] tester-contract = { path = ".." } -multiversx-sc-wasm-adapter = "=0.47.5" \ No newline at end of file +multiversx-sc-wasm-adapter = "=0.48.1" \ No newline at end of file diff --git a/token/Cargo.toml b/token/Cargo.toml index 03ac492..016c850 100644 --- a/token/Cargo.toml +++ b/token/Cargo.toml @@ -17,9 +17,9 @@ novax-data = { path = "../data", version = "0.1.5" } novax-request = { path = "../request", version = "0.1.5" } num-bigint = "0.4.4" async-trait = "0.1.73" -multiversx-sc = "=0.47.5" -multiversx-sc-scenario = "=0.47.5" -multiversx-sc-codec = "=0.18.6" +multiversx-sc = "=0.48.1" +multiversx-sc-scenario = "=0.48.1" +multiversx-sc-codec = "=0.18.7" base64 = "0.21.5" [dev-dependencies]