Skip to content

Prevent Memory Buildup by Clearing Joinsets #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion simln-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ async fn produce_simulation_results(
) -> Result<(), SimulationError> {
let mut set = tokio::task::JoinSet::new();

let result = loop {
let result = 'outer: loop {
tokio::select! {
biased;
_ = listener.clone() => {
Expand Down Expand Up @@ -1388,6 +1388,16 @@ async fn produce_simulation_results(
}
}
}

// Do a best-effort attempt to clear out any completed tasks in the joinset before proceeding to process
// more results. This prevents the joinset from building up over time and never emptying out any completed
// tasks.
while let Some(res) = set.try_join_next() {
if let Err(e) = res {
log::error!("Error collecting joinset: {e}");
break 'outer Err(SimulationError::TaskError);
}
}
};

log::debug!("Simulation results producer exiting.");
Expand Down
8 changes: 8 additions & 0 deletions simln-lib/src/sim_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,14 @@ impl SimNetwork for SimGraph {
},
};

// This fixes it, but is a super weird way to do this!
while let Some(res) = self.tasks.try_join_next() {
if let Err(e) = res {
log::error!("Error completing task {e}");
self.shutdown_trigger.trigger();
}
}

self.tasks.spawn(propagate_payment(
self.channels.clone(),
source,
Expand Down