wallet: share the loopback accept wait the SOCKS path already uses
This commit is contained in:
parent
9322d885f8
commit
052879fa39
1 changed files with 74 additions and 7 deletions
|
|
@ -67,6 +67,20 @@ pub const SOCKS_PROBE_TIMEOUT: Duration = Duration::from_millis(500);
|
|||
/// Gap between readiness-probe attempts.
|
||||
pub const SOCKS_PROBE_INTERVAL: Duration = Duration::from_millis(100);
|
||||
|
||||
/// How long `start_children` waits for the app-owned chain daemon to accept a
|
||||
/// TCP connection after `Node::launch` (or the `monerod` spawn) returned.
|
||||
///
|
||||
/// This is a `connect()`-accepted budget, not a Tor-bootstrap or chain-sync
|
||||
/// budget: the spike saw Cuprate's unrestricted listener accept within ~0.2s of
|
||||
/// `launch` returning. A longer wait only makes Start look dead again.
|
||||
pub const DAEMON_LISTENER_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// How long `start_children` waits for its own `monero-wallet-rpc` to accept a
|
||||
/// TCP connection before handing the client back. Same reasoning as
|
||||
/// [`DAEMON_LISTENER_TIMEOUT`]: the child binds its port a moment after
|
||||
/// `spawn` returns.
|
||||
pub const WALLET_RPC_LISTENER_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// The scheme Monero's `--proxy` needs for SOCKS5. A bare `host:port` means
|
||||
/// SOCKS4a (docs/proxies.md), which is *not* what this app asks for.
|
||||
pub const SOCKS5_SCHEME: &str = "socks5://";
|
||||
|
|
@ -299,6 +313,22 @@ impl WalletChild {
|
|||
format!("{SOCKS5_SCHEME}127.0.0.1:{port}")
|
||||
}
|
||||
|
||||
/// Poll `127.0.0.1:port` until it accepts, or `budget` elapses.
|
||||
///
|
||||
/// Proves the listener accepted a TCP connection. Does not prove HTTP, a
|
||||
/// wallet file, or a synced chain. `what` is the noun in the error
|
||||
/// (`chain daemon RPC`, `monero-wallet-rpc`).
|
||||
pub fn loopback_listener_ready(port: u16, budget: Duration, what: &str) -> Result<()> {
|
||||
match Self::poll_loopback(port, budget) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(last) => Err(ChildError(format!(
|
||||
"{what} on 127.0.0.1:{port} did not accept a connection within {}s ({}) — not continuing startup against a closed port",
|
||||
budget.as_secs_f32(),
|
||||
last
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for the SOCKS listener to accept a connection, with an explicit
|
||||
/// budget so a dead proxy fails visibly instead of hanging the wallet
|
||||
/// screen.
|
||||
|
|
@ -307,22 +337,32 @@ impl WalletChild {
|
|||
/// it does not prove the Tor circuit works. It also does not resolve the
|
||||
/// `.onion` name locally — the name travels inside the SOCKS request.
|
||||
pub fn socks_listener_ready(port: u16, budget: Duration) -> Result<()> {
|
||||
match Self::poll_loopback(port, budget) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(last) => Err(ChildError(format!(
|
||||
"SOCKS proxy on 127.0.0.1:{port} did not accept a connection within {}s \
|
||||
({last}) — not spawning a wallet child against a dead proxy",
|
||||
budget.as_secs_f32()
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// One accept loop, shared by every loopback readiness gate. `Err` carries
|
||||
/// the last connect error as text, not a formatted message: the caller owns
|
||||
/// the wording (the SOCKS gate and the daemon/wallet-RPC gates say
|
||||
/// different things).
|
||||
fn poll_loopback(port: u16, budget: Duration) -> std::result::Result<(), String> {
|
||||
let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port));
|
||||
let deadline = Instant::now() + budget;
|
||||
let mut last = None;
|
||||
while Instant::now() <= deadline {
|
||||
match TcpStream::connect_timeout(&addr, SOCKS_PROBE_TIMEOUT) {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(e) => last = Some(e),
|
||||
Err(e) => last = Some(e.to_string()),
|
||||
}
|
||||
std::thread::sleep(SOCKS_PROBE_INTERVAL);
|
||||
}
|
||||
Err(ChildError(format!(
|
||||
"SOCKS proxy on 127.0.0.1:{port} did not accept a connection within {}s \
|
||||
({}) — not spawning a wallet child against a dead proxy",
|
||||
budget.as_secs_f32(),
|
||||
last.map(|e| e.to_string()).unwrap_or_default()
|
||||
)))
|
||||
Err(last.unwrap_or_else(|| "no attempt".into()))
|
||||
}
|
||||
|
||||
/// The honest one-liner a UI can show for the configured chain source. A
|
||||
|
|
@ -1522,6 +1562,33 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
/// The shared accept wait: it passes for a live listener and fails, inside
|
||||
/// its budget, for a closed port — with a message that names the thing that
|
||||
/// did not accept, so the wallet screen can say which port is closed.
|
||||
#[test]
|
||||
fn loopback_listener_ready_accepts_a_live_listener_and_times_out_without_one() {
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
assert!(
|
||||
WalletChild::loopback_listener_ready(
|
||||
port,
|
||||
Duration::from_millis(200),
|
||||
"chain daemon RPC"
|
||||
)
|
||||
.is_ok(),
|
||||
"a live listener is ready"
|
||||
);
|
||||
drop(listener);
|
||||
let err =
|
||||
WalletChild::loopback_listener_ready(9, Duration::from_millis(200), "chain daemon RPC")
|
||||
.expect_err("a closed port must fail the start, not hang");
|
||||
let msg = err.to_string();
|
||||
assert!(msg.contains("chain daemon RPC"), "{msg}");
|
||||
assert!(msg.contains("127.0.0.1:9"), "{msg}");
|
||||
assert!(msg.contains("not continuing startup"), "{msg}");
|
||||
assert!(!msg.contains("dead proxy"), "{msg}");
|
||||
}
|
||||
|
||||
/// The spawn path: a `.onion` remote starts `arti proxy -p <port>` first,
|
||||
/// the wallet child is pointed at it, and stopping the wallet child takes
|
||||
/// the SOCKS listener with it.
|
||||
|
|
|
|||
Loading…
Reference in a new issue