Skip to content

Commit 3456432

Browse files
committed
Auto merge of rust-lang#3799 - RalfJung:josh-wait, r=RalfJung
josh: wait until the socket is ready
2 parents bb04eab + 1156d58 commit 3456432

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/tools/miri/miri-script/src/commands.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::env;
22
use std::ffi::{OsStr, OsString};
33
use std::io::Write;
4+
use std::net;
45
use std::ops::Not;
56
use std::ops::Range;
67
use std::path::PathBuf;
78
use std::process;
8-
use std::thread;
9-
use std::time;
9+
use std::time::Duration;
1010

1111
use anyhow::{anyhow, bail, Context, Result};
1212
use path_macro::path;
@@ -19,7 +19,7 @@ use crate::Command;
1919
/// Used for rustc syncs.
2020
const JOSH_FILTER: &str =
2121
":rev(75dd959a3a40eb5b4574f8d2e23aa6efbeb33573:prefix=src/tools/miri):/src/tools/miri";
22-
const JOSH_PORT: &str = "42042";
22+
const JOSH_PORT: u16 = 42042;
2323

2424
impl MiriEnv {
2525
/// Returns the location of the sysroot.
@@ -105,13 +105,11 @@ impl Command {
105105
let mut cmd = process::Command::new("josh-proxy");
106106
cmd.arg("--local").arg(local_dir);
107107
cmd.arg("--remote").arg("https://github.com");
108-
cmd.arg("--port").arg(JOSH_PORT);
108+
cmd.arg("--port").arg(JOSH_PORT.to_string());
109109
cmd.arg("--no-background");
110110
cmd.stdout(process::Stdio::null());
111111
cmd.stderr(process::Stdio::null());
112112
let josh = cmd.spawn().context("failed to start josh-proxy, make sure it is installed")?;
113-
// Give it some time so hopefully the port is open. (100ms was not enough.)
114-
thread::sleep(time::Duration::from_millis(200));
115113

116114
// Create a wrapper that stops it on drop.
117115
struct Josh(process::Child);
@@ -125,7 +123,7 @@ impl Command {
125123
.output()
126124
.expect("failed to SIGINT josh-proxy");
127125
// Sadly there is no "wait with timeout"... so we just give it some time to finish.
128-
thread::sleep(time::Duration::from_millis(100));
126+
std::thread::sleep(Duration::from_millis(100));
129127
// Now hopefully it is gone.
130128
if self.0.try_wait().expect("failed to wait for josh-proxy").is_some() {
131129
return;
@@ -139,6 +137,14 @@ impl Command {
139137
}
140138
}
141139

140+
// Wait until the port is open.
141+
let josh_ready = net::TcpStream::connect_timeout(
142+
&net::SocketAddr::from(([127, 0, 0, 1], JOSH_PORT)),
143+
Duration::from_secs(1),
144+
)
145+
.context("failed to connect to josh-proxy")?;
146+
drop(josh_ready);
147+
142148
Ok(Josh(josh))
143149
}
144150

@@ -236,6 +242,8 @@ impl Command {
236242
}
237243
// Make sure josh is running.
238244
let josh = Self::start_josh()?;
245+
let josh_url =
246+
format!("http://localhost:{JOSH_PORT}/rust-lang/rust.git@{commit}{JOSH_FILTER}.git");
239247

240248
// Update rust-version file. As a separate commit, since making it part of
241249
// the merge has confused the heck out of josh in the past.
@@ -250,7 +258,7 @@ impl Command {
250258
.context("FAILED to commit rust-version file, something went wrong")?;
251259

252260
// Fetch given rustc commit.
253-
cmd!(sh, "git fetch http://localhost:{JOSH_PORT}/rust-lang/rust.git@{commit}{JOSH_FILTER}.git")
261+
cmd!(sh, "git fetch {josh_url}")
254262
.run()
255263
.inspect_err(|_| {
256264
// Try to un-do the previous `git commit`, to leave the repo in the state we found it.
@@ -294,6 +302,8 @@ impl Command {
294302
}
295303
// Make sure josh is running.
296304
let josh = Self::start_josh()?;
305+
let josh_url =
306+
format!("http://localhost:{JOSH_PORT}/{github_user}/rust.git{JOSH_FILTER}.git");
297307

298308
// Find a repo we can do our preparation in.
299309
if let Ok(rustc_git) = env::var("RUSTC_GIT") {
@@ -338,20 +348,11 @@ impl Command {
338348
// Do the actual push.
339349
sh.change_dir(miri_dir()?);
340350
println!("Pushing miri changes...");
341-
cmd!(
342-
sh,
343-
"git push http://localhost:{JOSH_PORT}/{github_user}/rust.git{JOSH_FILTER}.git HEAD:{branch}"
344-
)
345-
.run()?;
351+
cmd!(sh, "git push {josh_url} HEAD:{branch}").run()?;
346352
println!();
347353

348354
// Do a round-trip check to make sure the push worked as expected.
349-
cmd!(
350-
sh,
351-
"git fetch http://localhost:{JOSH_PORT}/{github_user}/rust.git{JOSH_FILTER}.git {branch}"
352-
)
353-
.ignore_stderr()
354-
.read()?;
355+
cmd!(sh, "git fetch {josh_url} {branch}").ignore_stderr().read()?;
355356
let head = cmd!(sh, "git rev-parse HEAD").read()?;
356357
let fetch_head = cmd!(sh, "git rev-parse FETCH_HEAD").read()?;
357358
if head != fetch_head {

0 commit comments

Comments
 (0)