Skip to content

Commit 13b0594

Browse files
authoredOct 25, 2024··
fix(http2): improve graceful shutdown during handshake (#3729)
Before, if a graceful shutdown was triggered while the handshake was in-progress, the connection would just be dropped instantly. However, if some requests were in-flight as part of the handshake, they'd get dropped along with it. Now, if handshake is in-progress, we record that a close is desired, and as soon as the handshake has completed, the real graceful shutdown process starts, which should send the proper signals back about what happened with the in-flight requests.
1 parent 906f8db commit 13b0594

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed
 

‎src/proto/h2/server.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pin_project! {
8989
service: S,
9090
state: State<T, B>,
9191
date_header: bool,
92+
close_pending: bool
9293
}
9394
}
9495

@@ -101,7 +102,6 @@ where
101102
hs: Handshake<Compat<T>, SendBuf<B::Data>>,
102103
},
103104
Serving(Serving<T, B>),
104-
Closed,
105105
}
106106

107107
struct Serving<T, B>
@@ -172,26 +172,24 @@ where
172172
},
173173
service,
174174
date_header: config.date_header,
175+
close_pending: false,
175176
}
176177
}
177178

178179
pub(crate) fn graceful_shutdown(&mut self) {
179180
trace!("graceful_shutdown");
180181
match self.state {
181182
State::Handshaking { .. } => {
182-
// fall-through, to replace state with Closed
183+
self.close_pending = true;
184+
return;
183185
}
184186
State::Serving(ref mut srv) => {
185187
if srv.closing.is_none() {
186188
srv.conn.graceful_shutdown();
187189
}
188190
return;
189191
}
190-
State::Closed => {
191-
return;
192-
}
193192
}
194-
self.state = State::Closed;
195193
}
196194
}
197195

@@ -228,12 +226,11 @@ where
228226
})
229227
}
230228
State::Serving(ref mut srv) => {
231-
ready!(srv.poll_server(cx, &mut me.service, &mut me.exec))?;
232-
return Poll::Ready(Ok(Dispatched::Shutdown));
233-
}
234-
State::Closed => {
235229
// graceful_shutdown was called before handshaking finished,
236-
// nothing to do here...
230+
if true == me.close_pending && srv.closing.is_none() {
231+
srv.conn.graceful_shutdown();
232+
}
233+
ready!(srv.poll_server(cx, &mut me.service, &mut me.exec))?;
237234
return Poll::Ready(Ok(Dispatched::Shutdown));
238235
}
239236
};

0 commit comments

Comments
 (0)
Please sign in to comment.