do not panic on connection reset

This commit is contained in:
Reinier Balt 2018-04-21 17:46:29 +02:00
parent 03de235d2f
commit 2aea0e8fe6
3 changed files with 32 additions and 2 deletions

View file

@ -301,6 +301,10 @@ impl Future for SpircTask {
loop { loop {
let mut progress = false; let mut progress = false;
if self.session.is_invalid() {
return Ok(Async::Ready(()));
}
if !self.shutdown { if !self.shutdown {
match self.subscription.poll().unwrap() { match self.subscription.poll().unwrap() {
Async::Ready(Some(frame)) => { Async::Ready(Some(frame)) => {

View file

@ -20,6 +20,7 @@ use mercury::MercuryManager;
struct SessionData { struct SessionData {
country: String, country: String,
canonical_username: String, canonical_username: String,
invalid: bool,
} }
struct SessionInternal { struct SessionInternal {
@ -77,7 +78,9 @@ impl Session {
reusable_credentials.username.clone(), reusable_credentials.username.clone(),
); );
handle.spawn(task.map_err(|e| panic!(e))); handle.spawn(task.map_err(|e| {
error!("{:?}", e);
}));
session session
}); });
@ -104,6 +107,7 @@ impl Session {
data: RwLock::new(SessionData { data: RwLock::new(SessionData {
country: String::new(), country: String::new(),
canonical_username: username, canonical_username: username,
invalid: false,
}), }),
tx_connection: sender_tx, tx_connection: sender_tx,
@ -212,6 +216,15 @@ impl Session {
pub fn session_id(&self) -> usize { pub fn session_id(&self) -> usize {
self.0.session_id self.0.session_id
} }
pub fn shutdown(&self) {
debug!("Invalidating session[{}]", self.0.session_id);
self.0.data.write().unwrap().invalid = true;
}
pub fn is_invalid(&self) -> bool {
self.0.data.read().unwrap().invalid
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -240,6 +253,7 @@ where
impl<S> Future for DispatchTask<S> impl<S> Future for DispatchTask<S>
where where
S: Stream<Item = (u8, Bytes)>, S: Stream<Item = (u8, Bytes)>,
<S as Stream>::Error: ::std::fmt::Debug,
{ {
type Item = (); type Item = ();
type Error = S::Error; type Error = S::Error;
@ -251,7 +265,15 @@ where
}; };
loop { loop {
let (cmd, data) = try_ready!(self.0.poll()).expect("connection closed"); let (cmd, data) = match self.0.poll() {
Ok(Async::Ready(t)) => t,
Ok(Async::NotReady) => return Ok(Async::NotReady),
Err(e) => {
session.shutdown();
return Err(From::from(e));
}
}.expect("connection closed");
session.dispatch(cmd, data); session.dispatch(cmd, data);
} }
} }

View file

@ -341,6 +341,10 @@ impl PlayerInternal {
self.handle_packet(packet, current_normalisation_factor); self.handle_packet(packet, current_normalisation_factor);
} }
} }
if self.session.is_invalid() {
return;
}
} }
} }