-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from gfusee/fix/memory_leak
Partially fixed memory leaks
- Loading branch information
Showing
13 changed files
with
88 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod static_request_arc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters