Skip to content

Commit 62ceda7

Browse files
dswijseanmonstar
authored andcommitted
fix(conn/auto): hang on graceful shutdown when reading version
1 parent 8c7b13b commit 62ceda7

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/server/conn/auto.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ where
174174
buf: [MaybeUninit::uninit(); 24],
175175
filled: 0,
176176
version: Version::H2,
177+
cancelled: false,
177178
_pin: PhantomPinned,
178179
}
179180
}
@@ -185,12 +186,19 @@ pin_project! {
185186
// the amount of `buf` thats been filled
186187
filled: usize,
187188
version: Version,
189+
cancelled: bool,
188190
// Make this future `!Unpin` for compatibility with async trait methods.
189191
#[pin]
190192
_pin: PhantomPinned,
191193
}
192194
}
193195

196+
impl<I> ReadVersion<I> {
197+
pub fn cancel(self: Pin<&mut Self>) {
198+
*self.project().cancelled = true;
199+
}
200+
}
201+
194202
impl<I> Future for ReadVersion<I>
195203
where
196204
I: Read + Unpin,
@@ -199,6 +207,9 @@ where
199207

200208
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
201209
let this = self.project();
210+
if *this.cancelled {
211+
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Interrupted, "Cancelled")));
212+
}
202213

203214
let mut buf = ReadBuf::uninit(&mut *this.buf);
204215
// SAFETY: `this.filled` tracks how many bytes have been read (and thus initialized) and
@@ -296,7 +307,7 @@ where
296307
/// `Connection::poll` has resolved, this does nothing.
297308
pub fn graceful_shutdown(self: Pin<&mut Self>) {
298309
match self.project().state.project() {
299-
ConnStateProj::ReadVersion { .. } => {}
310+
ConnStateProj::ReadVersion { read_version, .. } => read_version.cancel(),
300311
#[cfg(feature = "http1")]
301312
ConnStateProj::H1 { conn } => conn.graceful_shutdown(),
302313
#[cfg(feature = "http2")]
@@ -420,7 +431,7 @@ where
420431
/// called after `UpgradeableConnection::poll` has resolved, this does nothing.
421432
pub fn graceful_shutdown(self: Pin<&mut Self>) {
422433
match self.project().state.project() {
423-
UpgradeableConnStateProj::ReadVersion { .. } => {}
434+
UpgradeableConnStateProj::ReadVersion { read_version, .. } => read_version.cancel(),
424435
#[cfg(feature = "http1")]
425436
UpgradeableConnStateProj::H1 { conn } => conn.graceful_shutdown(),
426437
#[cfg(feature = "http2")]

0 commit comments

Comments
 (0)