fix(app): moving to another voice channel no longer ends the new session
- Same-server moves point the existing songbird connection at the new channel and replace its handlers; removing and rejoining at once let the late gateway "left" update cancel the new join. - A driver disconnect without a reason (our own leave or move) is ignored, and a disconnect only ends the session its handler belongs to, so an old connection's late event cannot end a newer one. - Live test: move to a second voice channel and back; the session survives both, only the two moves end one, and the speaker is still heard afterwards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -323,7 +323,19 @@ impl DiscordCore {
|
||||
if self.voice_channel_id() == Some(channel_id) {
|
||||
return Ok(());
|
||||
}
|
||||
self.leave_locked("moved").await;
|
||||
let current = self.inner.session.lock().unwrap().take();
|
||||
if let Some(old) = current {
|
||||
if old.guild_id == gc.guild_id.get() {
|
||||
// Same server: move the existing connection. Leaving first and joining at once races
|
||||
// the gateway (the late "left" update cancels the new join).
|
||||
end_session(&self.inner, old, "moved", false).await;
|
||||
if let Some(call) = self.inner.songbird.get(gc.guild_id) {
|
||||
call.lock().await.remove_all_global_events();
|
||||
}
|
||||
} else {
|
||||
end_session(&self.inner, old, "moved", true).await;
|
||||
}
|
||||
}
|
||||
let call = self.inner.songbird.join(gc.guild_id, gc.id).await.map_err(|e| {
|
||||
CoreError(format!("could not connect to voice channel {channel_id}: {e}"))
|
||||
});
|
||||
@@ -337,7 +349,7 @@ impl DiscordCore {
|
||||
let rx = Arc::new(Mutex::new(RxState::default()));
|
||||
{
|
||||
let mut c = call.lock().await;
|
||||
let h = Receiver { core: Arc::downgrade(&self.inner), rx: rx.clone(), guild_id: gc.guild_id.get() };
|
||||
let h = Receiver { core: Arc::downgrade(&self.inner), rx: rx.clone() };
|
||||
c.add_global_event(CoreEvent_::SpeakingStateUpdate.into(), h.clone());
|
||||
c.add_global_event(CoreEvent_::RtpPacket.into(), h.clone());
|
||||
c.add_global_event(CoreEvent_::ClientDisconnect.into(), h.clone());
|
||||
@@ -365,7 +377,7 @@ impl DiscordCore {
|
||||
|
||||
async fn leave_locked(&self, reason: &str) -> bool {
|
||||
let Some(s) = self.inner.session.lock().unwrap().take() else { return false };
|
||||
end_session(&self.inner, s, reason).await;
|
||||
end_session(&self.inner, s, reason, true).await;
|
||||
true
|
||||
}
|
||||
|
||||
@@ -450,7 +462,9 @@ fn with_playback<T>(inner: &Inner, f: impl FnOnce(&mut Playback<WorkerClient, Pl
|
||||
out
|
||||
}
|
||||
|
||||
async fn end_session(inner: &Arc<Inner>, s: Session, reason: &str) {
|
||||
/// End a session's state; with `disconnect`, also leave the voice channel (a same-server move keeps
|
||||
/// the connection and points it at the new channel instead).
|
||||
async fn end_session(inner: &Arc<Inner>, s: Session, reason: &str, disconnect: bool) {
|
||||
s.alive.store(false, Ordering::Relaxed);
|
||||
with_playback(inner, |p| p.stop());
|
||||
let _ = inner.player.send(PlayerCmd::SetCall(None));
|
||||
@@ -463,7 +477,9 @@ async fn end_session(inner: &Arc<Inner>, s: Session, reason: &str) {
|
||||
inner.worker.speaker_leave(&user.to_string());
|
||||
}
|
||||
inner.gate.lock().unwrap().clear();
|
||||
let _ = inner.songbird.remove(GuildId::new(s.guild_id)).await;
|
||||
if disconnect {
|
||||
let _ = inner.songbird.remove(GuildId::new(s.guild_id)).await;
|
||||
}
|
||||
debug_log!("voice", "left voice {} reason={reason} packets lost={lost} late={late} bad={bad}", s.channel_id);
|
||||
let _ = inner.events.send(CoreEvent::Left { guild_id: s.guild_id, channel_id: s.channel_id, reason: reason.into() });
|
||||
}
|
||||
@@ -597,8 +613,8 @@ async fn player_task(core: std::sync::Weak<Inner>, mut rx: mpsc::UnboundedReceiv
|
||||
#[derive(Clone)]
|
||||
struct Receiver {
|
||||
core: std::sync::Weak<Inner>,
|
||||
/// This session's receive state; also tells this session apart from a later one.
|
||||
rx: Arc<Mutex<RxState>>,
|
||||
guild_id: u64,
|
||||
}
|
||||
|
||||
impl Receiver {
|
||||
@@ -651,18 +667,21 @@ impl VoiceEventHandler for Receiver {
|
||||
}
|
||||
EventContext::RtpPacket(p) => self.packet(&inner, p),
|
||||
EventContext::ClientDisconnect(d) => self.left(&inner, d.user_id.0),
|
||||
EventContext::DriverDisconnect(_) => {
|
||||
// No reason: we asked for it (leave or move), and that path already ended the session.
|
||||
EventContext::DriverDisconnect(d) if d.reason.is_some() => {
|
||||
// The driver gave up reconnecting (kicked, channel deleted, long network loss).
|
||||
let core = DiscordCore { inner: inner.clone() };
|
||||
let guild = self.guild_id;
|
||||
let rx = self.rx.clone();
|
||||
tokio::spawn(async move {
|
||||
let _g = core.inner.voice_lock.lock().await;
|
||||
let s = {
|
||||
let mut s = core.inner.session.lock().unwrap();
|
||||
if s.as_ref().is_some_and(|s| s.guild_id == guild) { s.take() } else { None }
|
||||
// Only the session this handler belongs to: a late event from an old
|
||||
// connection must not end a newer one.
|
||||
if s.as_ref().is_some_and(|s| Arc::ptr_eq(&s.rx, &rx)) { s.take() } else { None }
|
||||
};
|
||||
if let Some(s) = s {
|
||||
end_session(&core.inner, s, "disconnected").await;
|
||||
end_session(&core.inner, s, "disconnected", true).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -742,7 +761,7 @@ impl EventHandler for Handler {
|
||||
}
|
||||
let was_here = old.and_then(|o| o.channel_id).map(|c| c.get()) == Some(session_channel);
|
||||
if was_here && new_channel != Some(session_channel) {
|
||||
Receiver { core: Arc::downgrade(inner), rx, guild_id: 0 }.left(inner, user);
|
||||
Receiver { core: Arc::downgrade(inner), rx }.left(inner, user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user