Skip to content

Commit

Permalink
Merge pull request #1160 from sozu-proxy/devel/edemolis/fix/tls-close
Browse files Browse the repository at this point in the history
Fix TLS close initiated by Sozu
  • Loading branch information
FlorentinDUBOIS authored Dec 5, 2024
2 parents 02b49e9 + 2b19805 commit a81c50e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ impl ProxySession for HttpSession {
}

self.state.cancel_timeouts();
// defer backend closing to the state
self.state.close(self.proxy.clone(), &mut self.metrics);

let front_socket = self.state.front_socket();
if let Err(e) = front_socket.shutdown(Shutdown::Both) {
Expand All @@ -328,8 +330,6 @@ impl ProxySession for HttpSession {
}
proxy.remove_session(self.frontend_token);

// defer backend closing to the state
self.state.close(self.proxy.clone(), &mut self.metrics);
self.has_been_closed = true;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ impl ProxySession for HttpsSession {
}

self.state.cancel_timeouts();
// defer backend closing to the state
// in case of https it should also send a close notify on the client before the socket is closed below
self.state.close(self.proxy.clone(), &mut self.metrics);

let front_socket = self.state.front_socket();
if let Err(e) = front_socket.shutdown(Shutdown::Both) {
Expand All @@ -458,8 +461,6 @@ impl ProxySession for HttpsSession {
}
proxy.remove_session(self.frontend_token);

// defer backend closing to the state
self.state.close(self.proxy.clone(), &mut self.metrics);
self.has_been_closed = true;
}

Expand Down
18 changes: 8 additions & 10 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
let bufs = response_stream.as_io_slice();
if bufs.is_empty() && !self.frontend_socket.socket_wants_write() {
self.frontend_readiness.interest.remove(Ready::WRITABLE);
return StateResult::Continue;
// do not shortcut, response might have been terminated without anything more to send
}

let (size, socket_state) = self.frontend_socket.socket_write_vectored(&bufs);
Expand Down Expand Up @@ -532,6 +532,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
if response_stream.is_terminated() && response_stream.is_completed() {
if self.context.closing {
debug!("{} closing proxy, no keep alive", log_context!(self));
self.log_request_success(metrics);
return StateResult::CloseSession;
}

Expand Down Expand Up @@ -1623,15 +1624,10 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L

response_stream.parsing_phase = kawa::ParsingPhase::Terminated;

// check if there is anything left to write
if response_stream.is_completed() {
// we have to close the session now, because writable would short-cut
self.log_request_success(metrics);
StateResult::CloseSession
} else {
// writable() will be called again and finish the session properly
StateResult::CloseBackend
}
// writable() will be called again and finish the session properly
// for this reason, writable must not short cut
self.frontend_readiness.interest.insert(Ready::WRITABLE);
StateResult::Continue
}
// probably backend hup between keep alive request, change backend
(true, true) => {
Expand Down Expand Up @@ -1912,6 +1908,8 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> SessionState

fn close(&mut self, proxy: Rc<RefCell<dyn L7Proxy>>, metrics: &mut SessionMetrics) {
self.close_backend(proxy, metrics);
self.frontend_socket.socket_close();
let _ = self.frontend_socket.socket_write_vectored(&[]);

//if the state was initial, the connection was already reset
if !self.request_stream.is_initial() {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub trait SocketHandler {
fn socket_wants_write(&self) -> bool {
false
}
fn socket_close(&mut self) {}
fn socket_ref(&self) -> &TcpStream;
fn socket_mut(&mut self) -> &mut TcpStream;
fn protocol(&self) -> TransportProtocol;
Expand Down Expand Up @@ -430,6 +431,10 @@ impl SocketHandler for FrontRustls {
}
}

fn socket_close(&mut self) {
self.session.send_close_notify();
}

fn socket_wants_write(&self) -> bool {
self.session.wants_write()
}
Expand Down

0 comments on commit a81c50e

Please sign in to comment.