Skip to content

Commit

Permalink
Merge pull request #86 from gfusee/fix/memory_leak
Browse files Browse the repository at this point in the history
Partially fixed memory leaks
  • Loading branch information
gfusee authored May 3, 2024
2 parents f39b61b + 65e0c7f commit aabb446
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 34 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions abi-build/src/generator/impl_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
1 change: 1 addition & 0 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod static_request_arc;
46 changes: 46 additions & 0 deletions core/src/utils/static_request_arc.rs
Original file line number Diff line number Diff line change
@@ -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<Mutex<()>>,
/// 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()
})
}
6 changes: 3 additions & 3 deletions data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions mocking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tester/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tester/contract/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tester/contract/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ panic = "abort"

[dependencies]
tester-contract = { path = ".." }
multiversx-sc-wasm-adapter = "=0.47.5"
multiversx-sc-wasm-adapter = "=0.48.1"
6 changes: 3 additions & 3 deletions token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit aabb446

Please sign in to comment.