From 8d80a4fd55d1a6a2626b0cdbb0af44568d33b91e Mon Sep 17 00:00:00 2001 From: agent Date: Tue, 22 Sep 2026 14:33:32 -0400 Subject: [PATCH] tests: write the fake child's argv log with one mv, not append-per-line The wallet-child tests learn a spawned stand-in's port by polling its argv log, and `fake_bin`/`fake_child_bin` truncated the log and then appended one line per argument -- so a poller could read a prefix. A truncated port is a *valid* u16 ("427" out of "42765"), so `wait_for_child_rpc_port` happily returned a port nothing was listening on and the restart's `open_wallet` died with `Connection refused` after its 10s budget; the same prefix read is what `loopback_and_clearnet_remotes_carry_no_proxy...` printed with its `--daemon-address` value missing. Both tests failed on the first CI run of this branch (ci / test) and are green in isolation, which is the signature of this race rather than of a code defect. The argv now lands in the log with a single `mv`, so a reader sees either no log or the complete one. Measured with a tight read loop (argv-race-probe.sh): the append version was caught mid-write 1/300 reads, the mv version 0/300. --- src/node.rs | 8 ++++++-- src/wallet_child.rs | 13 ++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/node.rs b/src/node.rs index 7d185d8..3e33a32 100644 --- a/src/node.rs +++ b/src/node.rs @@ -3074,12 +3074,16 @@ pub(crate) mod tests { /// A stand-in for `monero-wallet-rpc`: it records its argv, then does what /// the test needs. The wallet child spawns a real process, so this is the /// only honest way to drive the start path offline. + /// + /// The argv is `mv`d into place in one step rather than truncated and + /// appended: a poller must never read half of it. It did — the port was + /// parsed while it was still being written, so `42765` read as `427`, and + /// the mock was bound on a port nothing dialled. fn fake_child_bin(dir: &Path, name: &str, log: &Path, body: &str) -> PathBuf { std::fs::create_dir_all(dir).expect("fake bin dir"); let path = dir.join(name); let script = format!( - "#!/bin/sh\n: > '{}'\nprintf '%s\\n' \"$0\" >> '{}'\nfor a in \"$@\"; do printf '%s\\n' \"$a\" >> '{}'; done\n{body}\n", - log.display(), + "#!/bin/sh\ntmp='{}'.$$\n: > \"$tmp\"\nprintf '%s\\n' \"$0\" >> \"$tmp\"\nfor a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$tmp\"; done\nmv -f \"$tmp\" '{}'\n{body}\n", log.display(), log.display() ); diff --git a/src/wallet_child.rs b/src/wallet_child.rs index d07b3fb..13357de 100644 --- a/src/wallet_child.rs +++ b/src/wallet_child.rs @@ -1233,17 +1233,24 @@ mod tests { } /// A stand-in for monerod / monero-wallet-rpc: records its argv, then parks. + /// + /// The argv lands in `` with one `mv`, not a truncate-then-append: a + /// reader must never see half an argv. It did — the poll below could catch + /// `--rpc-bind-port` while the port was still being written and parse + /// `427` out of `42765`, a valid-but-wrong port, and the test then bound a + /// mock nobody dialled. fn fake_bin(dir: &Path, name: &str, log: &Path) -> PathBuf { let path = dir.join(name); let mut f = fs::File::create(&path).unwrap(); writeln!(f, "#!/bin/sh").unwrap(); - writeln!(f, ": > '{}'", log.display()).unwrap(); + writeln!(f, "tmp='{}'.$$", log.display()).unwrap(); + writeln!(f, ": > \"$tmp\"").unwrap(); writeln!( f, - "for a in \"$@\"; do printf '%s\\n' \"$a\" >> '{}'; done", - log.display() + "for a in \"$@\"; do printf '%s\\n' \"$a\" >> \"$tmp\"; done" ) .unwrap(); + writeln!(f, "mv -f \"$tmp\" '{}'", log.display()).unwrap(); writeln!(f, "sleep 30").unwrap(); drop(f); let mut perms = fs::metadata(&path).unwrap().permissions();