Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(quic/connection): await connection.closed #30

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions transports/quic/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct Connection {
outgoing: Option<
BoxFuture<'static, Result<(quinn::SendStream, quinn::RecvStream), quinn::ConnectionError>>,
>,
/// Future to wait for the connection to be closed.
closing: Option<BoxFuture<'static, quinn::ConnectionError>>,
}

impl Connection {
Expand All @@ -57,6 +59,7 @@ impl Connection {
connection,
incoming: None,
outgoing: None,
closing: None,
}
}
}
Expand Down Expand Up @@ -108,8 +111,21 @@ impl StreamMuxer for Connection {
Poll::Pending
}

fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.connection.close(From::from(0u32), &[]);
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = self.get_mut();

let closing = this.closing.get_or_insert_with(|| {
this.connection.close(From::from(0u32), &[]);
let connection = this.connection.clone();
async move { connection.closed().await }.boxed()
});

match futures::ready!(closing.poll_unpin(cx)) {
// Expected error given that `connection.close` was called above.
quinn::ConnectionError::LocallyClosed => {}
error => return Poll::Ready(Err(Error::Connection(ConnectionError(error)))),
};

Poll::Ready(Ok(()))
}
}
Loading