Fixed potential resource leak

main
DaXcess 2023-09-26 13:56:55 +02:00
parent e4752444cf
commit d93758e250
No known key found for this signature in database
GPG Key ID: CF78CC72F0FD5EAD
1 changed files with 10 additions and 8 deletions

View File

@ -24,7 +24,7 @@ use songbird::{
create_player, create_player,
input::{Codec, Container, Input, Reader}, input::{Codec, Container, Input, Reader},
tracks::TrackHandle, tracks::TrackHandle,
Call, Event, EventContext, EventHandler, Call, Event, EventContext, EventHandler, Songbird,
}; };
use std::{ use std::{
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
@ -46,6 +46,7 @@ struct InnerSpoticordSession {
session_manager: SessionManager, session_manager: SessionManager,
songbird: Arc<Songbird>,
call: Arc<Mutex<Call>>, call: Arc<Mutex<Call>>,
track: Option<TrackHandle>, track: Option<TrackHandle>,
player: Option<Player>, player: Option<Player>,
@ -89,6 +90,7 @@ impl SpoticordSession {
text_channel_id, text_channel_id,
http: ctx.http.clone(), http: ctx.http.clone(),
session_manager: session_manager.clone(), session_manager: session_manager.clone(),
songbird: songbird.clone(),
call: call.clone(), call: call.clone(),
track: None, track: None,
player: None, player: None,
@ -97,7 +99,11 @@ impl SpoticordSession {
}; };
let mut instance = Self(Arc::new(RwLock::new(inner))); let mut instance = Self(Arc::new(RwLock::new(inner)));
instance.create_player(ctx).await?; if let Err(why) = instance.create_player(ctx).await {
songbird.remove(guild_id).await.ok();
return Err(why);
}
let mut call = call.lock().await; let mut call = call.lock().await;
@ -499,17 +505,13 @@ impl InnerSpoticordSession {
.remove_session(self.guild_id, self.owner) .remove_session(self.guild_id, self.owner)
.await; .await;
let mut call = self.call.lock().await;
if let Some(track) = self.track.take() { if let Some(track) = self.track.take() {
if let Err(why) = track.stop() { if let Err(why) = track.stop() {
error!("Failed to stop track: {:?}", why); error!("Failed to stop track: {:?}", why);
} }
} };
call.remove_all_global_events(); if let Err(why) = self.songbird.remove(self.guild_id).await {
if let Err(why) = call.leave().await {
error!("Failed to leave voice channel: {:?}", why); error!("Failed to leave voice channel: {:?}", why);
} }
} }