Skip to content

Commit 9ced901

Browse files
committed
compiletest: Fix flaky Android gdb test runs
Local testing showed that I was able to reproduce an error where debuginfo tests on Android would fail with "connection reset by peer". Further investigation turned out that the gdb tests are android with bit of process management: * First an `adb forward` command is run to ensure that the host's port 5039 is the same as the emulator's. * Next an `adb shell` command is run to execute the `gdbserver` executable inside the emulator. This gdb server will attach to port 5039 and listen for remote gdb debugging sessions. * Finally, we run `gdb` on the host (not in the emulator) and then connect to this gdb server to send it commands. The problem was happening when the host's gdb was failing to connect to the remote gdbserver running inside the emulator. The previous test for this was that after `adb shell` executed we'd sleep for a second and then attempt to make a TCP connection to port 5039. If successful we'd run gdb and on failure we'd sleep again. It turns out, however, that as soon as we've executed `adb forward` all TCP connections to 5039 will succeed. This means that we would only ever sleep for at most one second, and if this wasn't enough time we'd just fail later because we would assume that gdbserver had started but it may not have done so yet. This commit fixes these issues by removing the TCP connection to test if gdbserver is ready to go. Instead we read the stdout of the process and wait for it to print that it's listening at which point we start running gdb. I've found that locally at least I was unable to reproduce the failure after these changes. Closes #38710
1 parent 8f62c29 commit 9ced901

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

src/tools/compiletest/src/procsrv.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::env;
1212
use std::ffi::OsString;
1313
use std::io::prelude::*;
14+
use std::io;
1415
use std::path::PathBuf;
1516
use std::process::{Child, Command, ExitStatus, Output, Stdio};
1617

@@ -52,7 +53,7 @@ pub fn run(lib_path: &str,
5253
args: &[String],
5354
env: Vec<(String, String)>,
5455
input: Option<String>)
55-
-> Option<Result> {
56+
-> io::Result<Result> {
5657

5758
let mut cmd = Command::new(prog);
5859
cmd.args(args)
@@ -64,21 +65,17 @@ pub fn run(lib_path: &str,
6465
cmd.env(&key, &val);
6566
}
6667

67-
match cmd.spawn() {
68-
Ok(mut process) => {
69-
if let Some(input) = input {
70-
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
71-
}
72-
let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
73-
74-
Some(Result {
75-
status: status,
76-
out: String::from_utf8(stdout).unwrap(),
77-
err: String::from_utf8(stderr).unwrap(),
78-
})
79-
}
80-
Err(..) => None,
68+
let mut process = cmd.spawn()?;
69+
if let Some(input) = input {
70+
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
8171
}
72+
let Output { status, stdout, stderr } = process.wait_with_output().unwrap();
73+
74+
Ok(Result {
75+
status: status,
76+
out: String::from_utf8(stdout).unwrap(),
77+
err: String::from_utf8(stderr).unwrap(),
78+
})
8279
}
8380

8481
pub fn run_background(lib_path: &str,
@@ -87,26 +84,21 @@ pub fn run_background(lib_path: &str,
8784
args: &[String],
8885
env: Vec<(String, String)>,
8986
input: Option<String>)
90-
-> Option<Child> {
87+
-> io::Result<Child> {
9188

9289
let mut cmd = Command::new(prog);
9390
cmd.args(args)
94-
.stdin(Stdio::piped())
95-
.stdout(Stdio::piped())
96-
.stderr(Stdio::piped());
91+
.stdin(Stdio::piped())
92+
.stdout(Stdio::piped());
9793
add_target_env(&mut cmd, lib_path, aux_path);
9894
for (key, val) in env {
9995
cmd.env(&key, &val);
10096
}
10197

102-
match cmd.spawn() {
103-
Ok(mut process) => {
104-
if let Some(input) = input {
105-
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
106-
}
107-
108-
Some(process)
109-
}
110-
Err(..) => None,
98+
let mut process = cmd.spawn()?;
99+
if let Some(input) = input {
100+
process.stdin.as_mut().unwrap().write_all(input.as_bytes()).unwrap();
111101
}
102+
103+
Ok(process)
112104
}

src/tools/compiletest/src/runtest.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ use test::TestPaths;
2121
use uidiff;
2222
use util::logv;
2323

24-
use std::env;
2524
use std::collections::HashSet;
25+
use std::env;
2626
use std::fmt;
2727
use std::fs::{self, File};
28-
use std::io::{self, BufReader};
2928
use std::io::prelude::*;
30-
use std::net::TcpStream;
29+
use std::io::{self, BufReader};
3130
use std::path::{Path, PathBuf};
3231
use std::process::{Command, Output, ExitStatus};
3332
use std::str;
@@ -506,8 +505,8 @@ actual:\n\
506505
exe_file.to_str().unwrap().to_owned(),
507506
self.config.adb_test_dir.clone()
508507
],
509-
vec![("".to_owned(), "".to_owned())],
510-
Some("".to_owned()))
508+
Vec::new(),
509+
None)
511510
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
512511

513512
procsrv::run("",
@@ -518,8 +517,8 @@ actual:\n\
518517
"tcp:5039".to_owned(),
519518
"tcp:5039".to_owned()
520519
],
521-
vec![("".to_owned(), "".to_owned())],
522-
Some("".to_owned()))
520+
Vec::new(),
521+
None)
523522
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
524523

525524
let adb_arg = format!("export LD_LIBRARY_PATH={}; \
@@ -539,17 +538,23 @@ actual:\n\
539538
"shell".to_owned(),
540539
adb_arg.clone()
541540
],
542-
vec![("".to_owned(),
543-
"".to_owned())],
544-
Some("".to_owned()))
541+
Vec::new(),
542+
None)
545543
.expect(&format!("failed to exec `{:?}`", self.config.adb_path));
544+
545+
// Wait for the gdbserver to print out "Listening on port ..."
546+
// at which point we know that it's started and then we can
547+
// execute the debugger below.
548+
let mut stdout = BufReader::new(process.stdout.take().unwrap());
549+
let mut line = String::new();
546550
loop {
547-
//waiting 1 second for gdbserver start
548-
::std::thread::sleep(::std::time::Duration::new(1,0));
549-
if TcpStream::connect("127.0.0.1:5039").is_ok() {
551+
line.truncate(0);
552+
stdout.read_line(&mut line).unwrap();
553+
if line.starts_with("Listening on port 5039") {
550554
break
551555
}
552556
}
557+
drop(stdout);
553558

554559
let debugger_script = self.make_out_name("debugger.script");
555560
// FIXME (#9639): This needs to handle non-utf8 paths
@@ -569,7 +574,7 @@ actual:\n\
569574
&gdb_path,
570575
None,
571576
&debugger_opts,
572-
vec![("".to_owned(), "".to_owned())],
577+
Vec::new(),
573578
None)
574579
.expect(&format!("failed to exec `{:?}`", gdb_path));
575580
let cmdline = {

0 commit comments

Comments
 (0)