Fix double disconnect/corrupt state on auto leave
parent
3aa708a070
commit
ce0d2fd0ce
|
@ -25,7 +25,7 @@ mod session;
|
||||||
mod stats;
|
mod stats;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
if std::env::var("RUST_LOG").is_err() {
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
|
|
@ -69,12 +69,11 @@ impl InnerSessionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a session
|
/// Remove a session
|
||||||
pub async fn remove_session(&mut self, guild_id: GuildId) {
|
pub async fn remove_session(&mut self, guild_id: GuildId, owner: Option<UserId>) {
|
||||||
if let Some(session) = self.sessions.get(&guild_id) {
|
// Remove the owner from the owner map (if it exists)
|
||||||
if let Some(owner) = session.owner().await {
|
if let Some(owner) = owner {
|
||||||
self.owner_map.remove(&owner);
|
self.owner_map.remove(&owner);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
self.sessions.remove(&guild_id);
|
self.sessions.remove(&guild_id);
|
||||||
}
|
}
|
||||||
|
@ -149,8 +148,8 @@ impl SessionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a session
|
/// Remove a session
|
||||||
pub async fn remove_session(&self, guild_id: GuildId) {
|
pub async fn remove_session(&self, guild_id: GuildId, owner: Option<UserId>) {
|
||||||
self.0.write().await.remove_session(guild_id).await;
|
self.0.write().await.remove_session(guild_id, owner).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove owner from owner map.
|
/// Remove owner from owner map.
|
||||||
|
|
|
@ -54,6 +54,10 @@ struct InnerSpoticordSession {
|
||||||
disconnect_handle: Option<tokio::task::JoinHandle<()>>,
|
disconnect_handle: Option<tokio::task::JoinHandle<()>>,
|
||||||
|
|
||||||
client: Option<Client>,
|
client: Option<Client>,
|
||||||
|
|
||||||
|
/// Whether the session has been disconnected
|
||||||
|
/// If this is true then this instance should no longer be used and dropped
|
||||||
|
disconnected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SpoticordSession {
|
impl SpoticordSession {
|
||||||
|
@ -90,6 +94,7 @@ impl SpoticordSession {
|
||||||
playback_info: None,
|
playback_info: None,
|
||||||
disconnect_handle: None,
|
disconnect_handle: None,
|
||||||
client: None,
|
client: None,
|
||||||
|
disconnected: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut instance = Self(Arc::new(RwLock::new(inner)));
|
let mut instance = Self(Arc::new(RwLock::new(inner)));
|
||||||
|
@ -278,6 +283,7 @@ impl SpoticordSession {
|
||||||
message.embed(|embed| {
|
message.embed(|embed| {
|
||||||
embed.title("Failed to connect to Spotify");
|
embed.title("Failed to connect to Spotify");
|
||||||
embed.description(why);
|
embed.description(why);
|
||||||
|
embed.footer(|footer| footer.text("Please try again"));
|
||||||
embed.color(Status::Error as u64);
|
embed.color(Status::Error as u64);
|
||||||
|
|
||||||
embed
|
embed
|
||||||
|
@ -290,9 +296,6 @@ impl SpoticordSession {
|
||||||
error!("Failed to send error message: {:?}", why);
|
error!("Failed to send error message: {:?}", why);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up session
|
|
||||||
instance.player_stopped().await;
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,6 +385,11 @@ impl SpoticordSession {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up session
|
||||||
|
if !inner.read().await.disconnected {
|
||||||
|
instance.player_stopped().await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -515,7 +523,7 @@ impl SpoticordSession {
|
||||||
// read lock to read the current owner.
|
// read lock to read the current owner.
|
||||||
// This would deadlock if we have an active write lock
|
// This would deadlock if we have an active write lock
|
||||||
{
|
{
|
||||||
let inner = self.0.read().await;
|
let mut inner = self.0.write().await;
|
||||||
inner.disconnect_no_abort().await;
|
inner.disconnect_no_abort().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -599,7 +607,7 @@ impl SpoticordSession {
|
||||||
|
|
||||||
pub async fn disconnect_with_message(&self, content: &str) {
|
pub async fn disconnect_with_message(&self, content: &str) {
|
||||||
{
|
{
|
||||||
let inner = self.0.read().await;
|
let mut inner = self.0.write().await;
|
||||||
|
|
||||||
// Firstly we disconnect
|
// Firstly we disconnect
|
||||||
inner.disconnect_no_abort().await;
|
inner.disconnect_no_abort().await;
|
||||||
|
@ -674,8 +682,12 @@ impl SpoticordSession {
|
||||||
|
|
||||||
impl InnerSpoticordSession {
|
impl InnerSpoticordSession {
|
||||||
/// Internal version of disconnect, which does not abort the disconnect timer
|
/// Internal version of disconnect, which does not abort the disconnect timer
|
||||||
async fn disconnect_no_abort(&self) {
|
async fn disconnect_no_abort(&mut self) {
|
||||||
self.session_manager.remove_session(self.guild_id).await;
|
self.disconnected = true;
|
||||||
|
self
|
||||||
|
.session_manager
|
||||||
|
.remove_session(self.guild_id, self.owner)
|
||||||
|
.await;
|
||||||
|
|
||||||
let mut call = self.call.lock().await;
|
let mut call = self.call.lock().await;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue