Skip to content

Commit 398aaa7

Browse files
committed
fix: Unregister instances when they are dropped
1 parent ffdc4ac commit 398aaa7

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/global.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ impl Global {
7979
self.0.write().await.register_instance(handle);
8080
}
8181

82+
pub async fn unregister_instance(&self, id: i32) {
83+
self.0.write().await.unregister_instance(id);
84+
}
85+
8286
pub async fn get_instance(&self, id: i32) -> Option<InstanceHandle> {
8387
self.0.read().await.instances.get(&id).cloned()
8488
}
@@ -156,6 +160,14 @@ impl GlobalData {
156160
}
157161

158162
fn register_instance(&mut self, handle: InstanceHandle) {
159-
self.instances.insert(handle.id(), handle);
163+
let id = handle.id();
164+
self.instances.insert(id, handle);
165+
info!("registered instance {}", id);
166+
}
167+
168+
fn unregister_instance(&mut self, id: i32) {
169+
if let Some(_) = self.instances.remove(&id) {
170+
info!("unregistered instance {}", id);
171+
}
160172
}
161173
}

src/main.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,23 @@ async fn run(opts: Opts) -> color_eyre::eyre::Result<()> {
3232
let global = hyperion::global::GlobalData::new(&config).wrap();
3333

3434
// Initialize and spawn the devices
35-
for (_, inst) in &config.instances {
35+
for (&id, inst) in &config.instances {
3636
// Create the instance
3737
let (inst, handle) = hyperion::instance::Instance::new(global.clone(), inst.clone()).await;
3838
// Register the instance globally using its handle
3939
global.register_instance(handle).await;
4040
// Run the instance futures
41-
tokio::spawn(async move {
42-
let result = inst.run().await;
41+
tokio::spawn({
42+
let global = global.clone();
4343

44-
if let Err(error) = result {
45-
error!("Instance error: {:?}", error);
44+
async move {
45+
let result = inst.run().await;
46+
47+
if let Err(error) = result {
48+
error!("Instance error: {:?}", error);
49+
}
50+
51+
global.unregister_instance(id).await;
4652
}
4753
});
4854
}

0 commit comments

Comments
 (0)