Skip to content

Commit

Permalink
Mutualize MAX_LOOP_ITERATIONS in config
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <[email protected]>
  • Loading branch information
Wonshtrum committed Nov 23, 2023
1 parent 730f0c3 commit 4a444b1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
2 changes: 2 additions & 0 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ pub const DEFAULT_MAX_COMMAND_BUFFER_SIZE: usize = 2_000_000;
/// wether to avoid register cluster metrics in the local drain
pub const DEFAULT_DISABLE_CLUSTER_METRICS: bool = false;

pub const MAX_LOOP_ITERATIONS: usize = 100000;

/// Number of TLS 1.3 tickets to send to a client when establishing a connection.
/// The tickets allow the client to resume a session. This protects the client
/// agains session tracking. Increases the number of getrandom syscalls,
Expand Down
9 changes: 4 additions & 5 deletions lib/src/protocol/kawa_h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mio::{net::TcpStream, Interest, Token};
use rusty_ulid::Ulid;
use time::{Duration, Instant};

use sozu_command::proto::command::{Event, EventKind, ListenerType};
use sozu_command::{proto::command::{Event, EventKind, ListenerType}, config::MAX_LOOP_ITERATIONS};

use crate::{
backends::{Backend, BackendError},
Expand Down Expand Up @@ -1473,7 +1473,6 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
metrics: &mut SessionMetrics,
) -> SessionResult {
let mut counter = 0;
let max_loop_iterations = 100000;

if self.backend_connection_status.is_connecting()
&& !self.backend_readiness.event.is_empty()
Expand Down Expand Up @@ -1519,7 +1518,7 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
return SessionResult::Close;
}

while counter < max_loop_iterations {
while counter < MAX_LOOP_ITERATIONS {
let frontend_interest = self.frontend_readiness.filter_interest();
let backend_interest = self.backend_readiness.filter_interest();

Expand Down Expand Up @@ -1618,10 +1617,10 @@ impl<Front: SocketHandler, L: ListenerHandler + L7ListenerHandler> Http<Front, L
counter += 1;
}

if counter == max_loop_iterations {
if counter >= MAX_LOOP_ITERATIONS {
error!(
"PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection",
self.frontend_token, max_loop_iterations
self.frontend_token, MAX_LOOP_ITERATIONS
);
incr!("http.infinite_loop.error");

Expand Down
8 changes: 4 additions & 4 deletions lib/src/protocol/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{cell::RefCell, net::SocketAddr, rc::Rc};

use mio::{net::TcpStream, Token};
use rusty_ulid::Ulid;
use sozu_command::config::MAX_LOOP_ITERATIONS;

use crate::{
backends::Backend,
Expand Down Expand Up @@ -624,14 +625,13 @@ impl<Front: SocketHandler, L: ListenerHandler> SessionState for Pipe<Front, L> {
metrics: &mut SessionMetrics,
) -> SessionResult {
let mut counter = 0;
let max_loop_iterations = 100000;

if self.frontend_readiness.event.is_hup() {
return SessionResult::Close;
}

let token = self.frontend_token;
while counter < max_loop_iterations {
while counter < MAX_LOOP_ITERATIONS {
let frontend_interest = self.frontend_readiness.filter_interest();
let backend_interest = self.backend_readiness.filter_interest();

Expand Down Expand Up @@ -709,10 +709,10 @@ impl<Front: SocketHandler, L: ListenerHandler> SessionState for Pipe<Front, L> {
counter += 1;
}

if counter == max_loop_iterations {
if counter >= MAX_LOOP_ITERATIONS {
error!(
"PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection",
self.frontend_token, max_loop_iterations
self.frontend_token, MAX_LOOP_ITERATIONS
);
incr!("http.infinite_loop.error");

Expand Down
8 changes: 4 additions & 4 deletions lib/src/protocol/proxy_protocol/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{cell::RefCell, rc::Rc};
use mio::{net::TcpStream, *};
use nom::{Err, HexDisplay};
use rusty_ulid::Ulid;
use sozu_command::config::MAX_LOOP_ITERATIONS;

use crate::{
logs::LogContext,
Expand Down Expand Up @@ -211,13 +212,12 @@ impl<Front: SocketHandler> SessionState for ExpectProxyProtocol<Front> {
metrics: &mut SessionMetrics,
) -> SessionResult {
let mut counter = 0;
let max_loop_iterations = 100000;

if self.frontend_readiness.event.is_hup() {
return SessionResult::Close;
}

while counter < max_loop_iterations {
while counter < MAX_LOOP_ITERATIONS {
let frontend_interest = self.frontend_readiness.filter_interest();

trace!(
Expand Down Expand Up @@ -251,10 +251,10 @@ impl<Front: SocketHandler> SessionState for ExpectProxyProtocol<Front> {
counter += 1;
}

if counter == max_loop_iterations {
if counter >= MAX_LOOP_ITERATIONS {
error!(
"PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection",
self.frontend_token, max_loop_iterations
self.frontend_token, MAX_LOOP_ITERATIONS
);
incr!("http.infinite_loop.error");

Expand Down
8 changes: 4 additions & 4 deletions lib/src/protocol/rustls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{cell::RefCell, io::ErrorKind, rc::Rc};
use mio::{net::TcpStream, Token};
use rustls::ServerConnection;
use rusty_ulid::Ulid;
use sozu_command::config::MAX_LOOP_ITERATIONS;

use crate::{
logs::LogContext, protocol::SessionState, timer::TimeoutContainer, Readiness, Ready,
Expand Down Expand Up @@ -186,13 +187,12 @@ impl SessionState for TlsHandshake {
_metrics: &mut SessionMetrics,
) -> SessionResult {
let mut counter = 0;
let max_loop_iterations = 100000;

if self.frontend_readiness.event.is_hup() {
return SessionResult::Close;
}

while counter < max_loop_iterations {
while counter < MAX_LOOP_ITERATIONS {
let frontend_interest = self.frontend_readiness.filter_interest();

trace!(
Expand Down Expand Up @@ -233,10 +233,10 @@ impl SessionState for TlsHandshake {
counter += 1;
}

if counter == max_loop_iterations {
if counter >= MAX_LOOP_ITERATIONS {
error!(
"PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection",
self.frontend_token, max_loop_iterations
self.frontend_token, MAX_LOOP_ITERATIONS
);
incr!("http.infinite_loop.error");

Expand Down
13 changes: 6 additions & 7 deletions lib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use mio::net::{TcpListener, TcpStream};
use rustls::{ProtocolVersion, ServerConnection};
use socket2::{Domain, Protocol, Socket, Type};
use sozu_command::config::MAX_LOOP_ITERATIONS;

#[derive(thiserror::Error, Debug)]
pub enum ServerBindError {
Expand Down Expand Up @@ -44,8 +45,6 @@ pub enum TransportProtocol {
Tls1_3,
}

const MAX_LOOP_ITERATION: usize = 100000;

pub trait SocketHandler {
fn socket_read(&mut self, buf: &mut [u8]) -> (usize, SocketResult);
fn socket_write(&mut self, buf: &[u8]) -> (usize, SocketResult);
Expand All @@ -65,7 +64,7 @@ impl SocketHandler for TcpStream {
let mut counter = 0;
loop {
counter += 1;
if counter > MAX_LOOP_ITERATION {
if counter > MAX_LOOP_ITERATIONS {
error!("MAX_LOOP_ITERATION reached in TcpStream::socket_read");
incr!("socket.read.infinite_loop.error");
}
Expand Down Expand Up @@ -94,7 +93,7 @@ impl SocketHandler for TcpStream {
let mut counter = 0;
loop {
counter += 1;
if counter > MAX_LOOP_ITERATION {
if counter > MAX_LOOP_ITERATIONS {
error!("MAX_LOOP_ITERATION reached in TcpStream::socket_write");
incr!("socket.write.infinite_loop.error");
}
Expand Down Expand Up @@ -182,7 +181,7 @@ impl SocketHandler for FrontRustls {
let mut counter = 0;
loop {
counter += 1;
if counter > MAX_LOOP_ITERATION {
if counter > MAX_LOOP_ITERATIONS {
error!("MAX_LOOP_ITERATION reached in FrontRustls::socket_read");
incr!("socket.read.infinite_loop.error");
}
Expand Down Expand Up @@ -275,7 +274,7 @@ impl SocketHandler for FrontRustls {
let mut counter = 0;
loop {
counter += 1;
if counter > MAX_LOOP_ITERATION {
if counter > MAX_LOOP_ITERATIONS {
error!("MAX_LOOP_ITERATION reached in FrontRustls::socket_write");
incr!("socket.write.infinite_loop.error");
}
Expand Down Expand Up @@ -391,7 +390,7 @@ impl SocketHandler for FrontRustls {
let mut counter = 0;
loop {
counter += 1;
if counter > MAX_LOOP_ITERATION {
if counter > MAX_LOOP_ITERATIONS {
error!("MAX_LOOP_ITERATION reached in FrontRustls::socket_write_vectored");
incr!("socket.write.infinite_loop.error");
}
Expand Down
12 changes: 7 additions & 5 deletions lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rusty_ulid::Ulid;
use slab::Slab;
use time::{Duration, Instant};

use sozu_command::{proto::command::request::RequestType, ObjectKind};
use sozu_command::{config::MAX_LOOP_ITERATIONS, proto::command::request::RequestType, ObjectKind};

use crate::{
backends::{Backend, BackendMap},
Expand Down Expand Up @@ -572,7 +572,6 @@ impl TcpSession {

fn ready_inner(&mut self, session: Rc<RefCell<dyn ProxySession>>) -> StateResult {
let mut counter = 0;
let max_loop_iterations = 100000;

let back_connected = self.back_connected();
if back_connected.is_connecting() {
Expand Down Expand Up @@ -634,7 +633,7 @@ impl TcpSession {
}

let token = self.frontend_token;
while counter < max_loop_iterations {
while counter < MAX_LOOP_ITERATIONS {
let front_interest = self.front_readiness().interest & self.front_readiness().event;
let back_interest = self
.back_readiness()
Expand Down Expand Up @@ -754,8 +753,11 @@ impl TcpSession {
counter += 1;
}

if counter == max_loop_iterations {
error!("PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection", self.frontend_token, max_loop_iterations);
if counter >= MAX_LOOP_ITERATIONS {
error!(
"PROXY\thandling session {:?} went through {} iterations, there's a probable infinite loop bug, closing the connection",
self.frontend_token, MAX_LOOP_ITERATIONS
);
incr!("tcp.infinite_loop.error");

let front_interest = self.front_readiness().interest & self.front_readiness().event;
Expand Down

0 comments on commit 4a444b1

Please sign in to comment.