Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

respect native unix socket path limits (#3538)

kt7e754697f181
diff --git a/.changes/fix-native-unix-socket-paths.md b/.changes/fix-native-unix-socket-paths.md
new file mode 100644
index 0000000000..43fd9f1268
--- /dev/null
+++ b/.changes/fix-native-unix-socket-paths.md
@@ -0,0 +1 @@
+- On macOS, socket paths are now checked against the native 103-byte limit, and a long `TMPDIR` (for example under Nix) no longer breaks the daemon: its sockets move to `/tmp/prime-agent-<uid>` when the default location would be too long.
diff --git a/crates/pa-daemon/src/platform/paths.rs b/crates/pa-daemon/src/platform/paths.rs
index 1a6da6ff60..2b220a3fb4 100644
--- a/crates/pa-daemon/src/platform/paths.rs
+++ b/crates/pa-daemon/src/platform/paths.rs
@@ -11,7 +11,24 @@ use crate::paths::hash_key;
pub fn socket_dir() -> PathBuf {
let uid = pa_core::platform::process::current_user_id();
let tmp = std::env::var_os("TMPDIR").map_or_else(|| PathBuf::from("/tmp"), PathBuf::from);
- tmp.join(format!("prime-agent-{uid}"))
+ socket_dir_for(&tmp, uid)
+}
+
+/// Reserve the longest generated endpoint before selecting a directory. Linux
+/// can re-anchor deep paths through /proc; other Unix platforms need a short path.
+#[cfg(unix)]
+fn socket_dir_for(tmp: &Path, uid: u32) -> PathBuf {
+ let name = format!("prime-agent-{uid}");
+ let dir = tmp.join(&name);
+ #[cfg(not(target_os = "linux"))]
+ if std::os::unix::net::SocketAddr::from_pathname(
+ dir.join("worker-000000000000-000000000000.sock"),
+ )
+ .is_err()
+ {
+ return Path::new("/tmp").join(name);
+ }
+ dir
}
/// The socket-dir half of a discovery state root on Windows: TS computes
@@ -80,7 +97,34 @@ mod tests {
let tmp = std::env::var_os("TMPDIR").map_or_else(|| PathBuf::from("/tmp"), PathBuf::from);
assert_eq!(
socket_dir(),
- tmp.join(format!("prime-agent-{}", uid.trim()))
+ socket_dir_for(&tmp, uid.trim().parse().unwrap())
+ );
+ }
+
+ #[cfg(all(unix, not(target_os = "linux")))]
+ #[test]
+ fn generated_endpoints_fit_even_when_tmpdir_is_deep() {
+ use std::os::unix::net::SocketAddr;
+
+ for length in [20, 65, 120] {
+ let tmp = PathBuf::from(format!("/{}", "d".repeat(length - 1)));
+ let dir = socket_dir_for(&tmp, 501);
+ let worker = dir.join("worker-000000000000-000000000000.sock");
+ assert!(SocketAddr::from_pathname(&worker).is_ok());
+ if length >= 65 {
+ assert_eq!(dir, Path::new("/tmp/prime-agent-501"));
+ } else {
+ assert_eq!(dir, tmp.join("prime-agent-501"));
+ }
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn short_tmpdir_keeps_the_original_socket_directory() {
+ assert_eq!(
+ socket_dir_for(Path::new("/tmp"), 501),
+ Path::new("/tmp/prime-agent-501")
);
}
diff --git a/crates/pa-types/src/platform/transport.rs b/crates/pa-types/src/platform/transport.rs
index 9335fdb031..ad490ac74f 100644
--- a/crates/pa-types/src/platform/transport.rs
+++ b/crates/pa-types/src/platform/transport.rs
@@ -53,9 +53,13 @@ impl TransportListener for tokio::net::UnixListener {
}
}
-/// `AF_UNIX` `sun_path` capacity: 108 bytes including the terminating NUL.
+/// Native `AF_UNIX` path capacity, excluding the terminating NUL.
#[cfg(unix)]
-const MAX_SUN_PATH: usize = 107;
+const MAX_SUN_PATH: usize = {
+ // SAFETY: sockaddr_un contains only integer fields and arrays of integers.
+ let address: libc::sockaddr_un = unsafe { std::mem::zeroed() };
+ address.sun_path.len() - 1
+};
/// A kernel-valid `AF_UNIX` address for `bind`/`connect`. Paths within the limit pass through; a
/// longer path is re-anchored through an `O_PATH` descriptor on its parent
@@ -364,6 +368,7 @@ pub fn connect_blocking(path: &Path) -> std::io::Result<Box<dyn BlockingTranspor
#[cfg(all(test, unix))]
mod tests {
use super::*;
+ #[cfg(target_os = "linux")]
use std::os::unix::fs::FileTypeExt;
/// A directory whose full path length is exactly `target` bytes; falls
@@ -391,6 +396,7 @@ mod tests {
dir
}
+ #[cfg(target_os = "linux")]
#[tokio::test]
async fn over_limit_paths_bind_connect_and_land_in_place() {
use tokio::io::AsyncReadExt;
@@ -444,6 +450,22 @@ mod tests {
let _ = std::fs::remove_file(&socket);
}
+ #[cfg(not(target_os = "linux"))]
+ #[test]
+ fn paths_one_byte_over_the_native_limit_are_rejected_with_context() {
+ let path = std::path::PathBuf::from(format!("/{}", "x".repeat(MAX_SUN_PATH)));
+ let error = UnixSocketAddress::new(&path)
+ .err()
+ .expect("over native limit");
+ assert_eq!(
+ error.to_string(),
+ format!(
+ "AF_UNIX socket path exceeds the {MAX_SUN_PATH}-byte limit: {}",
+ path.display()
+ )
+ );
+ }
+
#[tokio::test]
async fn over_limit_paths_without_a_short_name_error_clearly() {
let dir = dir_of_exact_len("toolong", 120);