tokio-xmpp: Poll packets in a loop

This needs to be a loop in order to ignore packets we don’t care about,
or those we want to handle elsewhere.  Returning something isn’t correct
in those two cases because it would signal to tokio that the XMPPStream
is also done, while there could be additional packets waiting for us.

The proper solution is thus a loop which we exit once we have something
to return.

Fixes a deadlock when we ignore some packets.
This commit is contained in:
Emmanuel Gil Peyrot 2023-06-21 13:43:24 +02:00 committed by pep
commit 6ccc5ccace

View file

@ -243,42 +243,50 @@ impl Stream for Client {
}; };
// Poll stream // Poll stream
match Pin::new(&mut stream).poll_next(cx) { //
Poll::Ready(None) => { // This needs to be a loop in order to ignore packets we dont care about, or those
// EOF // we want to handle elsewhere. Returning something isnt correct in those two
self.state = ClientState::Disconnected; // cases because it would signal to tokio that the XMPPStream is also done, while
Poll::Ready(Some(Event::Disconnected(Error::Disconnected))) // there could be additional packets waiting for us.
} //
Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => { // The proper solution is thus a loop which we exit once we have something to
// Receive stanza // return.
self.state = ClientState::Connected(stream); loop {
Poll::Ready(Some(Event::Stanza(stanza))) match Pin::new(&mut stream).poll_next(cx) {
} Poll::Ready(None) => {
Poll::Ready(Some(Ok(Packet::Text(_)))) => { // EOF
// Ignore text between stanzas self.state = ClientState::Disconnected;
self.state = ClientState::Connected(stream); return Poll::Ready(Some(Event::Disconnected(Error::Disconnected)));
Poll::Pending }
} Poll::Ready(Some(Ok(Packet::Stanza(stanza)))) => {
Poll::Ready(Some(Ok(Packet::StreamStart(_)))) => { // Receive stanza
// <stream:stream> self.state = ClientState::Connected(stream);
self.state = ClientState::Disconnected; return Poll::Ready(Some(Event::Stanza(stanza)));
Poll::Ready(Some(Event::Disconnected( }
ProtocolError::InvalidStreamStart.into(), Poll::Ready(Some(Ok(Packet::Text(_)))) => {
))) // Ignore text between stanzas
} }
Poll::Ready(Some(Ok(Packet::StreamEnd))) => { Poll::Ready(Some(Ok(Packet::StreamStart(_)))) => {
// End of stream: </stream:stream> // <stream:stream>
self.state = ClientState::Disconnected; self.state = ClientState::Disconnected;
Poll::Ready(Some(Event::Disconnected(Error::Disconnected))) return Poll::Ready(Some(Event::Disconnected(
} ProtocolError::InvalidStreamStart.into(),
Poll::Pending => { )));
// Try again later }
self.state = ClientState::Connected(stream); Poll::Ready(Some(Ok(Packet::StreamEnd))) => {
Poll::Pending // End of stream: </stream:stream>
} self.state = ClientState::Disconnected;
Poll::Ready(Some(Err(e))) => { return Poll::Ready(Some(Event::Disconnected(Error::Disconnected)));
self.state = ClientState::Disconnected; }
Poll::Ready(Some(Event::Disconnected(e.into()))) Poll::Pending => {
// Try again later
self.state = ClientState::Connected(stream);
return Poll::Pending;
}
Poll::Ready(Some(Err(e))) => {
self.state = ClientState::Disconnected;
return Poll::Ready(Some(Event::Disconnected(e.into())));
}
} }
} }
} }