Skip to content

Commit 606fa2b

Browse files
mattsseyash-atreya
andauthored
fix: drop geth's stderr handle (#2104)
* fix: drop geth's stderr handle * fix: consume geth stderr in separate thread * nit * fix test * nit --------- Co-authored-by: Yash Atreya <[email protected]>
1 parent 4ca3b9c commit 606fa2b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

crates/node-bindings/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ thiserror.workspace = true
3737
tracing.workspace = true
3838
url.workspace = true
3939

40+
4041
[dev-dependencies]
4142
ci_info.workspace = true
4243
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

crates/node-bindings/src/nodes/geth.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ pub struct Geth {
202202
data_dir: Option<PathBuf>,
203203
chain_id: Option<u64>,
204204
insecure_unlock: bool,
205+
keep_err: bool,
205206
genesis: Option<Genesis>,
206207
mode: NodeMode,
207208
clique_private_key: Option<SigningKey>,
@@ -369,6 +370,14 @@ impl Geth {
369370
self
370371
}
371372

373+
/// Keep the handle to geth's stderr in order to read from it.
374+
///
375+
/// Caution: if the stderr handle isn't used, this can end up blocking.
376+
pub const fn keep_stderr(mut self) -> Self {
377+
self.keep_err = true;
378+
self
379+
}
380+
372381
/// Adds an argument to pass to `geth`.
373382
///
374383
/// Pass any arg that is not supported by the builder.
@@ -635,7 +644,20 @@ impl Geth {
635644
}
636645
}
637646

638-
child.stderr = Some(reader.into_inner());
647+
if self.keep_err {
648+
// re-attach the stderr handle if requested
649+
child.stderr = Some(reader.into_inner());
650+
} else {
651+
// We need to consume the stderr otherwise geth is non-responsive and RPC server results
652+
// in connection refused.
653+
// See: <https://github.com/alloy-rs/alloy/issues/2091#issuecomment-2676134147>
654+
std::thread::spawn(move || {
655+
let mut buf = String::new();
656+
loop {
657+
let _ = reader.read_line(&mut buf);
658+
}
659+
});
660+
}
639661

640662
Ok(GethInstance {
641663
pid: child,

crates/provider/src/ext/admin.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ mod test {
107107
async_ci_only(|| async move {
108108
run_with_tempdir("geth-test-1", |temp_dir_1| async move {
109109
run_with_tempdir("geth-test-2", |temp_dir_2| async move {
110-
let geth1 = Geth::new().disable_discovery().data_dir(&temp_dir_1).spawn();
111-
let mut geth2 =
112-
Geth::new().disable_discovery().port(0u16).data_dir(&temp_dir_2).spawn();
110+
let geth1 =
111+
Geth::new().disable_discovery().keep_stderr().data_dir(&temp_dir_1).spawn();
112+
let mut geth2 = Geth::new()
113+
.disable_discovery()
114+
.keep_stderr()
115+
.port(0u16)
116+
.data_dir(&temp_dir_2)
117+
.spawn();
113118

114119
let provider1 = ProviderBuilder::new().on_http(geth1.endpoint_url());
115120
let provider2 = ProviderBuilder::new().on_http(geth2.endpoint_url());

0 commit comments

Comments
 (0)