-
Notifications
You must be signed in to change notification settings - Fork 177
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
Fix memory leaks #708
Closed
DenisBiryukov91
wants to merge
7
commits into
eclipse-zenoh:main
from
DenisBiryukov91:fix/memory-leaks-finalizers
Closed
Fix memory leaks #708
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2cf21f0
- added finalizers to remove cyclic references in Runtime and Session
DenisBiryukov91 ae0811e
fixes after rebase
DenisBiryukov91 a84e419
- break cyclic reference in Mux->Face->State->Mux
DenisBiryukov91 0b40edb
- zenoh-task crate to ci
DenisBiryukov91 921365a
- fix Resource::clean() clear nonwildprefix if any, to allow breaking…
DenisBiryukov91 a4111cc
- remove no longer needed manual resource cleaning code for hats
DenisBiryukov91 1575544
- Network now owns a weak ptr to Runtime; this allows to remove termi…
DenisBiryukov91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# Copyright (c) 2023 ZettaScale Technology | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Eclipse Public License 2.0 which is available at | ||
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
# which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
# | ||
# Contributors: | ||
# ZettaScale Zenoh Team, <[email protected]> | ||
# | ||
[package] | ||
rust-version = { workspace = true } | ||
name = "zenoh-task" | ||
version = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
authors = [ | ||
"Denis <[email protected]>" | ||
] | ||
edition = { workspace = true } | ||
license = { workspace = true } | ||
categories = { workspace = true } | ||
description = "Internal crate for zenoh." | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
async-std = { workspace = true, features = ["default", "unstable"] } | ||
tokio = { workspace = true, features = ["default", "sync"] } | ||
futures = { workspace = true } | ||
uuid = { workspace = true } | ||
|
||
[dev-dependencies] | ||
async-std = { workspace = true, features = ["default", "unstable", "attributes"] } |
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,8 @@ | ||
# ⚠️ WARNING ⚠️ | ||
|
||
This crate is intended for Zenoh's internal use. | ||
|
||
- [Click here for Zenoh's main repository](https://github.com/eclipse-zenoh/zenoh) | ||
- [Click here for Zenoh's documentation](https://zenoh.io) | ||
|
||
|
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,88 @@ | ||
// | ||
// Copyright (c) 2023 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
//! ⚠️ WARNING ⚠️ | ||
//! | ||
//! This module is intended for Zenoh's internal use. | ||
//! | ||
//! [Click here for Zenoh's documentation](../zenoh/index.html) | ||
|
||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::future::Future; | ||
use std::ops::DerefMut; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
use tokio::task::{self, JoinHandle}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Clone)] | ||
pub struct TaskController { | ||
running_task_id_to_handle: Arc<Mutex<HashMap<Uuid, Option<JoinHandle<()>>>>>, | ||
} | ||
|
||
impl TaskController { | ||
pub fn new() -> TaskController { | ||
TaskController { | ||
running_task_id_to_handle: Arc::new(Mutex::new( | ||
HashMap::<Uuid, Option<JoinHandle<()>>>::new(), | ||
)), | ||
} | ||
} | ||
|
||
/// Spawns a task (similarly to task::spawn) that can be later terminated by call to terminate_all() | ||
/// Task output is ignored | ||
pub fn spawn<F, T>(&self, future: F) | ||
where | ||
F: Future<Output = T> + Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
let mut tasks = self.running_task_id_to_handle.lock().unwrap(); | ||
let id = TaskController::get_next_task_id(tasks.deref_mut()); | ||
let tasks_mutex = self.running_task_id_to_handle.clone(); | ||
let jh = task::spawn(futures::FutureExt::map(future, move |_| { | ||
tasks_mutex.lock().unwrap().remove(&id); | ||
() | ||
})); | ||
tasks.insert(id, Some(jh)); | ||
} | ||
|
||
fn get_next_task_id(hm: &mut HashMap<Uuid, Option<JoinHandle<()>>>) -> Uuid { | ||
loop { | ||
let uuid = Uuid::new_v4(); | ||
match hm.entry(uuid.clone()) { | ||
Entry::Occupied(_) => { | ||
continue; | ||
} | ||
Entry::Vacant(v) => { | ||
v.insert(None); | ||
return uuid; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Terminates all prevously spawned tasks | ||
pub fn terminate_all(&self) { | ||
let tasks: Vec<(Uuid, Option<JoinHandle<()>>)> = self | ||
.running_task_id_to_handle | ||
.lock() | ||
.unwrap() | ||
.drain() | ||
.collect(); | ||
for (_id, jh) in tasks { | ||
let _ = jh.unwrap().abort(); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An attempt of finer grain executor and task management is being proposed in #566 .
Moreover, every time a new crate is added it should be also added in the right order for publication in the CI.