Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions watchman/rust/watchman_client/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust_library(
name = "watchman_client",
srcs = glob(["src/**/*.rs"]),
preferred_linkage = "static",
test_deps = ["fbsource//third-party/rust:tempfile"],
deps = [
"fbsource//third-party/rust:anyhow",
"fbsource//third-party/rust:bytes",
Expand Down
1 change: 1 addition & 0 deletions watchman/rust/watchman_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ winapi = { version = "0.3", features = ["fileapi", "handleapi", "winbase", "winu

[dev-dependencies]
clap = { version = "4.5.7", features = ["derive"] }
tempfile = "3"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(fbcode_build)"] }
59 changes: 59 additions & 0 deletions watchman/rust/watchman_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ impl Connector {

let mut cmd = Command::new(watchman_path);
cmd.args(["--output-encoding", "bser-v2", "get-sockname"]);
// A caller can time out before the server answers socket discovery.
cmd.kill_on_drop(true);

#[cfg(windows)]
cmd.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);
Expand Down Expand Up @@ -1228,6 +1230,63 @@ mod tests {
assert_eq!(builder.unix_domain, Some(PathBuf::from("/some/path")));
}

#[cfg(unix)]
#[tokio::test]
async fn cancelling_socket_discovery_kills_the_helper() {
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::process::Stdio;
use std::time::Duration;

let temp = tempfile::tempdir().unwrap();
let helper = temp.path().join("watchman");
// A helper that only exits on its own when the test directory is removed.
fs::write(
&helper,
concat!(
"#!/bin/sh\n",
"printf '%s\\n' \"$$\" > \"$0.pid\"\n",
"while [ -f \"$0\" ]; do sleep 1; done\n",
),
)
.unwrap();
fs::set_permissions(&helper, fs::Permissions::from_mode(0o700)).unwrap();

let connector = Connector::default().watchman_cli_path(&helper);
let mut discovery = Box::pin(connector.connect());
let pid_file = helper.with_extension("pid");
let started = tokio::time::timeout(Duration::from_secs(5), async {
loop {
if let Some(pid) = fs::read_to_string(&pid_file)
.ok()
.and_then(|pid| pid.trim().parse::<u32>().ok())
{
return pid.to_string();
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
});
let pid = tokio::select! {
result = &mut discovery => panic!("socket discovery exited: {:?}", result.err()),
result = started => result.expect("socket discovery did not start the helper"),
};
drop(discovery);

tokio::time::timeout(Duration::from_secs(5), async {
while std::process::Command::new("kill")
.args(["-0", &pid])
.stderr(Stdio::null())
.status()
.unwrap()
.success()
{
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("the socket-discovery helper outlived cancellation");
}

#[tokio::test]
async fn test_decoder() {
async fn read_bser(buf: &[u8], chunk_size: usize) -> Vec<TestStruct> {
Expand Down