Skip to content

Commit 70b17ec

Browse files
authored
Keep tokio console only in the playground (#3950)
# Description It turned out that `tokio_unstable` introduces a memory leak when creating spans. As @MartinquaXD noticed: > Most of the tracing related memory was allocated while holding a tokio mutex lock. When you check the tokio code you see that this only happens when we build it with `tokio_unstable` which is only used for `tokio-console` support. Disabling the `tokio_unstable` helped to mitigate the memory leak issue almost completely. # Changes - Remove `tokio_unstable` from the codebase. - Keep it only in the playground (for now). ## Further implementation Using the updated deployment CI job from #3954, the tokio-console can be gated behind a feature in case we need to run it in the future. Will implement it in a follow-up pr.
1 parent 627d099 commit 70b17ec

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

.cargo/config.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/observe/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ axum = { workspace = true, optional = true }
1010
atty = { workspace = true }
1111
async-trait = { workspace = true }
1212
chrono = { workspace = true, features = ["now"] }
13-
console-subscriber = { workspace = true }
13+
console-subscriber = { workspace = true, optional = true }
1414
futures = { workspace = true }
1515
opentelemetry = { workspace = true }
1616
opentelemetry-otlp = { workspace = true, features = ["grpc-tonic"] }
@@ -37,3 +37,4 @@ workspace = true
3737
default = []
3838
axum-tracing = ["axum"]
3939
jemalloc-profiling = ["dep:jemalloc_pprof"]
40+
tokio-console = ["dep:console-subscriber"]

crates/observe/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fn main() {
22
// Make build system aware of custom config flags to avoid clippy warnings
3+
// tokio_unstable is only used when explicitly compiled with --cfg
4+
// tokio_unstable (e.g., in the playground environment)
35
println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");
46
}

crates/observe/src/tracing.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,7 @@ pub fn initialize_reentrant(config: &Config) {
5959
fn set_tracing_subscriber(config: &Config) {
6060
let initial_filter = config.env_filter.to_string();
6161

62-
// The `tracing` APIs are heavily generic to enable zero overhead. Unfortunately
63-
// this leads to very annoying type constraints which can only be satisfied
64-
// by literally copy and pasting the code so the compiler doesn't try to
65-
// infer types that satisfy both the tokio-console and the regular case.
66-
// It's tempting to resolve this mess by first configuring the `fmt_layer` and
67-
// only then the `console_subscriber`. However, this setup was the only way
68-
// I found that:
69-
// 1. actually makes `tokio-console` work
70-
// 2. prints logs if `tokio-console` is disabled
71-
// 3. does NOT skip the next log following a `tracing::event!()`. These calls
72-
// happen for example under the hood in `sqlx`. I don't understand what's
73-
// actually causing that but at this point I'm just happy if all the features
74-
// work correctly.
62+
// The `tracing` APIs are heavily generic to enable zero overhead.
7563

7664
macro_rules! fmt_layer {
7765
($env_filter:expr_2021, $stderr_threshold:expr_2021, $use_json_format:expr_2021) => {{
@@ -109,11 +97,6 @@ fn set_tracing_subscriber(config: &Config) {
10997
}};
11098
}
11199

112-
let enable_tokio_console: bool = std::env::var("TOKIO_CONSOLE")
113-
.unwrap_or("false".to_string())
114-
.parse()
115-
.unwrap();
116-
117100
let (env_filter, reload_handle) =
118101
tracing_subscriber::reload::Layer::new(EnvFilter::new(&initial_filter));
119102

@@ -161,13 +144,33 @@ fn set_tracing_subscriber(config: &Config) {
161144
))
162145
.with(tracing_layer);
163146

164-
if cfg!(tokio_unstable) && enable_tokio_console {
165-
subscriber.with(console_subscriber::spawn()).init();
166-
tracing::info!("started program with support for tokio-console");
167-
} else {
147+
#[cfg(all(tokio_unstable, feature = "tokio-console"))]
148+
{
149+
let enable_tokio_console: bool = std::env::var("TOKIO_CONSOLE")
150+
.unwrap_or("false".to_string())
151+
.parse()
152+
.unwrap();
153+
154+
if enable_tokio_console {
155+
subscriber.with(console_subscriber::spawn()).init();
156+
tracing::info!("started program with support for tokio-console");
157+
} else {
158+
subscriber.init();
159+
tracing::info!(
160+
"started program without support for tokio-console (TOKIO_CONSOLE=false)"
161+
);
162+
}
163+
}
164+
165+
#[cfg(not(all(tokio_unstable, feature = "tokio-console")))]
166+
{
168167
subscriber.init();
169-
tracing::info!("started program without support for tokio-console");
168+
tracing::info!(
169+
"started program without support for tokio-console (not compiled with tokio_unstable \
170+
cfg and tokio-console feature)"
171+
);
170172
}
173+
171174
if cfg!(unix) {
172175
spawn_reload_handler(initial_filter, reload_handle);
173176
}

playground/Dockerfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CMD ["migrate"]
3131
FROM chef AS builder
3232
COPY --from=planner /src/recipe.json recipe.json
3333
COPY --from=chef /.cargo /.cargo
34-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo chef cook --release --recipe-path recipe.json
34+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo chef cook --release --features observe/tokio-console --recipe-path recipe.json
3535

3636
# Copy only the library crates for now
3737
COPY --from=chef /.cargo /.cargo
@@ -48,9 +48,9 @@ COPY ./crates/chain/ ./crates/chain
4848
COPY ./crates/ethrpc/ ./crates/ethrpc
4949
COPY ./crates/observe/ ./crates/observe
5050
COPY ./crates/order-validation/ ./crates/order-validation
51-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package shared
51+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package shared
5252
COPY ./crates/solver/ ./crates/solver
53-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package solver
53+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package solver
5454

5555
# Create an base image for all the binaries
5656
FROM docker.io/debian:bookworm-slim AS base
@@ -61,14 +61,14 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked apt-get update && \
6161
FROM builder AS alerter-build
6262
COPY --from=chef /.cargo /.cargo
6363
COPY ./crates/alerter/ ./crates/alerter
64-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package alerter
64+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package alerter
6565

6666
FROM base AS alerter
6767
COPY --from=alerter-build /src/target/release/alerter /usr/local/bin/alerter
6868
ENTRYPOINT [ "alerter" ]
6969

7070
FROM builder AS autopilot-build
71-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package autopilot
71+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package autopilot
7272

7373
FROM base AS autopilot
7474
COPY --from=chef /.cargo /.cargo
@@ -78,7 +78,7 @@ ENTRYPOINT [ "autopilot" ]
7878

7979
FROM builder AS driver-build
8080
COPY ./crates/driver/ ./crates/driver
81-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package driver
81+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package driver
8282

8383
FROM base AS driver
8484
COPY --from=driver-build /src/target/release/driver /usr/local/bin/driver
@@ -87,7 +87,7 @@ ENTRYPOINT [ "driver" ]
8787
FROM builder AS orderbook-build
8888
COPY --from=chef /.cargo /.cargo
8989
COPY ./crates/orderbook/ ./crates/orderbook
90-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package orderbook
90+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package orderbook
9191

9292
FROM base AS orderbook
9393
COPY --from=orderbook-build /src/target/release/orderbook /usr/local/bin/orderbook
@@ -96,7 +96,7 @@ ENTRYPOINT [ "orderbook" ]
9696
FROM builder AS refunder-build
9797
COPY --from=chef /.cargo /.cargo
9898
COPY ./crates/refunder/ ./crates/refunder
99-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package refunder
99+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package refunder
100100

101101
FROM base AS refunder
102102
COPY --from=refunder-build /src/target/release/refunder /usr/local/bin/refunder
@@ -105,7 +105,7 @@ ENTRYPOINT [ "refunder" ]
105105
FROM builder AS solvers-build
106106
COPY --from=chef /.cargo /.cargo
107107
COPY ./crates/solvers/ ./crates/solvers
108-
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --package solvers
108+
RUN CARGO_PROFILE_RELEASE_DEBUG=1 cargo build --release --features observe/tokio-console --package solvers
109109

110110
FROM base AS solvers
111111
COPY --from=solvers-build /src/target/release/solvers /usr/local/bin/solvers

0 commit comments

Comments
 (0)