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 excessive stack sizes #950

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

* Fix excessive stack sizes (#950)
* Do not enable **json** by default (breaking, but it was a mistake) (#948)

# 3.0.0

* Replace RequestBuilder Deref with explicit wrappers (#944)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ features = ["rustls", "platform-verifier", "native-tls", "socks-proxy", "cookies

[dependencies]
base64 = "0.22.1"
ureq-proto = "0.2.4"
ureq-proto = "0.2.5"
# ureq-proto = { path = "../ureq-proto" }
log = "0.4.22"
once_cell = "1.19.0"
Expand Down
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,9 @@ pub(crate) mod test {
use std::io;

use assert_no_alloc::AllocDisabler;
use config::Config;
use config::{Config, ConfigBuilder};
use once_cell::sync::Lazy;
use typestate::AgentScope;

use super::*;

Expand Down Expand Up @@ -988,6 +989,30 @@ pub(crate) mod test {
res.body_mut().read_to_string().unwrap();
}

#[test]
fn ensure_reasonable_stack_sizes() {
macro_rules! ensure {
($type:ty, $size:tt) => {
let sz = std::mem::size_of::<$type>();
// println!("{}: {}", stringify!($type), sz);
assert!(
sz <= $size,
"Stack size of {} is too big {} > {}",
stringify!($type),
sz,
$size
);
};
}

ensure!(RequestBuilder<WithoutBody>, 400); // 288
ensure!(Agent, 100); // 32
ensure!(Config, 400); // 320
ensure!(ConfigBuilder<AgentScope>, 400); // 320
ensure!(Response<Body>, 800); // 760
ensure!(Body, 700); // 648
}

// This doesn't need to run, just compile.
fn _ensure_send_sync() {
fn is_send(_t: impl Send) {}
Expand Down
6 changes: 3 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub(crate) struct BodyHandler {
connection: Option<Connection>,
timings: CallTimings,
remote_closed: bool,
redirect: Option<Flow<Redirect>>,
redirect: Option<Box<Flow<Redirect>>>,
}

impl BodyHandler {
Expand Down Expand Up @@ -651,7 +651,7 @@ impl BodyHandler {
let must_close_connection = match flow.proceed().unwrap() {
RecvBodyResult::Redirect(flow) => {
let c = flow.must_close_connection();
self.redirect = Some(flow);
self.redirect = Some(Box::new(flow));
c
}
RecvBodyResult::Cleanup(v) => v.must_close_connection(),
Expand All @@ -674,7 +674,7 @@ impl BodyHandler {

// Unwrap is OK, because we are only consuming the redirect body if
// such a body was signalled by the remote.
let redirect = self.redirect.take();
let redirect = self.redirect.take().map(|b| *b);
Ok(redirect.expect("remote to have signaled redirect"))
}
}
Expand Down
25 changes: 5 additions & 20 deletions src/timings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;
use std::sync::Arc;
use ureq_proto::ArrayVec;

use crate::config::Timeouts;
use crate::transport::time::{Duration, Instant};
Expand Down Expand Up @@ -82,37 +81,23 @@ impl Timeout {
}
}

#[derive(Debug)]
#[derive(Default, Debug)]
pub(crate) struct CallTimings {
timeouts: Timeouts,
timeouts: Box<Timeouts>,
current_time: CurrentTime,
times: ArrayVec<(Timeout, Instant), 8>,
}

impl Default for CallTimings {
fn default() -> Self {
Self {
timeouts: Default::default(),
current_time: Default::default(),
times: empty_times(),
}
}
}

fn empty_times() -> ArrayVec<(Timeout, Instant), 8> {
ArrayVec::from_fn(|_| (Timeout::Global, Instant::AlreadyHappened))
times: Vec<(Timeout, Instant)>,
}

impl CallTimings {
pub(crate) fn new(timeouts: Timeouts, current_time: CurrentTime) -> Self {
let mut times = empty_times();
let mut times = Vec::with_capacity(8);

let now = current_time.now();
times.push((Timeout::Global, now));
times.push((Timeout::PerCall, now));

CallTimings {
timeouts,
timeouts: Box::new(timeouts),
current_time,
times,
}
Expand Down
Loading