diff --git a/src/wallet_child.rs b/src/wallet_child.rs index e4d7b13..d61dab8 100644 --- a/src/wallet_child.rs +++ b/src/wallet_child.rs @@ -527,6 +527,13 @@ impl WalletChild { Err(e) => { // A failure after the SOCKS child came up must not leave the // listener (or monerod) orphaned behind the error. + // + // That includes the in-process chain daemon, deliberately: this + // call hands back the error instead of the client, so nothing + // would own a daemon left running — and the next start could not + // bind the port it was holding. The chain *datadir* is what + // outlives a failed start and what a retry reuses, never a + // daemon that no longer has an owner. let _ = self.stop(); Err(e) } @@ -2151,8 +2158,14 @@ mod tests { .with_wallet_rpc_bin(fake_bin(&dir, "monero-wallet-rpc", &rpc_log)); // The wallet RPC child is fake, so `start` reaches only the daemon: the - // whole point of the in-process source is that the daemon *is* us. - let _ = child.start(&store); + // whole point of the in-process source is that the daemon *is* us. The + // hold is what a `sh` fake cannot do itself — binding the port the gate + // probes — so this test exercises the successful start it is named for + // (see [`test_support::PublishedPortHold`]). + let _hold = PublishedPortHold::watching(std::slice::from_ref(&rpc_log)); + child + .start(&store) + .expect("the daemon and the fake wallet RPC both come up"); let addr = format!("127.0.0.1:{DEFAULT_MONEROD_RPC_PORT}"); assert!( waits_for_tcp(&addr, Duration::from_secs(20)), @@ -2174,6 +2187,56 @@ mod tests { assert!(closed, "the daemon's RPC listener outlived stop()"); } + /// The other half of the same contract, and the reason `start_with_creds` + /// stops what it started on *any* failure after the children came up: an + /// in-process daemon survives a wallet-RPC failure only inside a `WalletChild` + /// somebody still owns, and this call is about to hand the error back instead + /// of the client — so a daemon left behind would have no owner, no handle and + /// no way to stop it, while the next `start` could not bind its port. The + /// chain datadir is what outlives a failed start; the process does not. + /// `node::tests::a_missing_binary_fails_the_start_and_leaves_no_child` + /// asserts the same contract one level up. + /// + /// Nothing holds the fake wallet RPC's port here, so its readiness gate times + /// out *after* the in-process daemon is already up — the failure path this + /// pins. The error naming `monero-wallet-rpc` (not the chain daemon) is what + /// proves the daemon had come up first. + #[cfg(feature = "cuprate-daemon")] + #[test] + fn a_start_that_fails_after_the_daemon_came_up_leaves_nothing_running() { + let dir = tmp_dir("cuprate-late-failure"); + let store = open_store(&dir); + let rpc_log = dir.join("wallet-rpc.argv"); + let child = WalletChild::new(in_process_source(&dir), dir.join("wallet"), None) + .unwrap() + .with_wallet_rpc_bin(fake_bin(&dir, "monero-wallet-rpc", &rpc_log)); + + let err = child + .start(&store) + .expect_err("a wallet RPC that never binds its port fails the start") + .to_string(); + assert!(err.contains("monero-wallet-rpc"), "{err}"); + + assert!( + !child.running(), + "a failed start must not leave a daemon running: {err}" + ); + assert!( + dir.join("chain").exists(), + "the chain datadir is the part that outlives a failed start" + ); + let addr = format!("127.0.0.1:{DEFAULT_MONEROD_RPC_PORT}"); + let mut closed = false; + for _ in 0..60 { + if std::net::TcpStream::connect(&addr).is_err() { + closed = true; + break; + } + std::thread::sleep(Duration::from_millis(250)); + } + assert!(closed, "the daemon's RPC listener outlived a failed start"); + } + fn probe_status(state: WalletChildState, height: u64, target_height: u64) -> WalletChildStatus { WalletChildStatus { state,