Skip to content

Commit

Permalink
Driver: Fix multiple disconnect events on leave (#233)
Browse files Browse the repository at this point in the history
Fixes behaviour where a Driver which was asked to leave an active call would receive the disconnect event several times: once when we started the disconnect, and once again when Discord killed the WS client.
  • Loading branch information
FelixMcFelix authored Mar 19, 2024
1 parent 58f5c89 commit a5d9b58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/driver/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async fn runner(mut config: Config, rx: Receiver<CoreMessage>, tx: Sender<CoreMe
drop(interconnect.events.send(EventMessage::FireCoreEvent(
CoreContext::DriverDisconnect(InternalDisconnect {
kind: DisconnectKind::Runtime,
reason: None,
reason: Some(DisconnectReason::Requested),
info: conn.info.clone(),
}),
)));
Expand All @@ -120,21 +120,26 @@ async fn runner(mut config: Config, rx: Receiver<CoreMessage>, tx: Sender<CoreMe
// (i.e., prevent users from mistakenly trying to reconnect for an *old* dead conn).
// if it *is* a match, the conn needs to die!
// (as the WS channel has truly given up the ghost).
if ws_idx == attempt_idx {
connection = None;
let conn = if ws_idx == attempt_idx {
drop(interconnect.mixer.send(MixerMessage::DropConn));
drop(interconnect.mixer.send(MixerMessage::RebuildEncoder));
connection.take()
} else {
reason = None;
}
None
};

drop(interconnect.events.send(EventMessage::FireCoreEvent(
CoreContext::DriverDisconnect(InternalDisconnect {
kind: DisconnectKind::Runtime,
reason,
info: ws_info,
}),
)));
// Conn may have been unset earlier (i.e., in a deliberate disconnect).
// If so, do not repropagate/repeat the disconnect event.
if conn.is_some() {
drop(interconnect.events.send(EventMessage::FireCoreEvent(
CoreContext::DriverDisconnect(InternalDisconnect {
kind: DisconnectKind::Runtime,
reason,
info: ws_info,
}),
)));
}
},
CoreMessage::SetTrack(s) => {
drop(interconnect.mixer.send(MixerMessage::SetTrack(s)));
Expand Down
4 changes: 4 additions & 0 deletions src/events/context/data/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub enum DisconnectReason {
ProtocolViolation,
/// A voice connection was not established in the specified time.
TimedOut,
/// The call was manually disconnected by a user command, e.g. [`Driver::leave`].
///
/// [`Driver::leave`]: crate::driver::Driver::leave
Requested,
/// The Websocket connection was closed by Discord.
///
/// This typically indicates that the voice session has expired,
Expand Down

0 comments on commit a5d9b58

Please sign in to comment.