Skip to content

Commit

Permalink
Better handle stuck WebDriver processes (#4350)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Dec 15, 2024
1 parent 237babb commit 37f0aa1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* Adding `getter`, `setter`, and `constructor` methods to enums now results in a compiler error. This was previously erroneously allowed and resulted in invalid JS code gen.
[#4278](https://github.com/rustwasm/wasm-bindgen/pull/4278)

* Handle stuck and failed WebDriver processes when re-trying to start them.
[#4340](https://github.com/rustwasm/wasm-bindgen/pull/4340)

### Fixed

- Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly.
Expand Down
41 changes: 31 additions & 10 deletions crates/cli/src/bin/wasm-bindgen-test-runner/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value as Json};
use std::env;
use std::fs::File;
use std::io::{self, Read};
use std::io::{self, Cursor, ErrorKind, Read, Write};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use ureq::Agent;
Expand Down Expand Up @@ -615,12 +617,6 @@ impl Drop for Client {
}
}

fn read<R: Read>(r: &mut R) -> io::Result<Vec<u8>> {
let mut dst = Vec::new();
r.read_to_end(&mut dst)?;
Ok(dst)
}

fn tab(s: &str) -> String {
let mut result = String::new();
for line in s.lines() {
Expand All @@ -635,6 +631,7 @@ struct BackgroundChild<'a> {
child: Child,
stdout: Option<thread::JoinHandle<io::Result<Vec<u8>>>>,
stderr: Option<thread::JoinHandle<io::Result<Vec<u8>>>>,
any_stderr: Arc<AtomicBool>,
shell: &'a Shell,
print_stdio_on_drop: bool,
}
Expand All @@ -654,12 +651,36 @@ impl<'a> BackgroundChild<'a> {
.context(format!("failed to spawn {:?} binary", path))?;
let mut stdout = child.stdout.take().unwrap();
let mut stderr = child.stderr.take().unwrap();
let stdout = Some(thread::spawn(move || read(&mut stdout)));
let stderr = Some(thread::spawn(move || read(&mut stderr)));
let stdout = Some(thread::spawn(move || {
let mut dst = Vec::new();
stdout.read_to_end(&mut dst)?;
Ok(dst)
}));
let any_stderr = Arc::new(AtomicBool::new(false));
let any_stderr_clone = Arc::clone(&any_stderr);
let stderr = Some(thread::spawn(move || {
let mut dst = Cursor::new(Vec::new());
let mut buffer = [0];

match stderr.read_exact(&mut buffer) {
Ok(()) => {
dst.write_all(&buffer).unwrap();
any_stderr_clone.store(true, Ordering::Relaxed);
}
Err(error) if error.kind() == ErrorKind::UnexpectedEof => {
return Ok(dst.into_inner())
}
Err(error) => return Err(error),
}

io::copy(&mut stderr, &mut dst)?;
Ok(dst.into_inner())
}));
Ok(BackgroundChild {
child,
stdout,
stderr,
any_stderr,
shell,
print_stdio_on_drop: true,
})
Expand All @@ -668,7 +689,7 @@ impl<'a> BackgroundChild<'a> {
fn has_failed(&mut self) -> bool {
match self.child.try_wait() {
Ok(Some(status)) => !status.success(),
Ok(None) => false,
Ok(None) => self.any_stderr.load(Ordering::Relaxed),
Err(_) => true,
}
}
Expand Down

0 comments on commit 37f0aa1

Please sign in to comment.