A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
wezterm-uds, mux-server: probe a Windows socket before binding over it
RoversXfd3c11027628
diff --git a/mux/src/session_server.rs b/mux/src/session_server.rsindex 34ead17f..20fc9f0a 100644--- a/mux/src/session_server.rs+++ b/mux/src/session_server.rs@@ -35,6 +35,10 @@ pub fn pid_holding(pid_file: &Path) -> Option<u32> {.filter(|pid| *pid > 1)}+/// No answer off unix: `daemonize` is `#![cfg(unix)]` entire, so no pid+/// file is written to hold a lock on. Everything downstream -- `is_running`,+/// `stop`, and the GUI's session host -- is short-circuited by this, which+/// is why `socket_answers` below stays a stub too.#[cfg(not(unix))]pub fn pid_holding(_pid_file: &Path) -> Option<u32> {None@@ -78,6 +82,13 @@ pub fn socket_answers(path: &Path) -> bool {)}+/// Deliberately not `wezterm_uds::someone_listens`, which the mux server+/// uses to decide whether it may bind over a socket. That question fails+/// safe towards "someone is there"; this one asks whether *our* server is+/// up and fails safe towards "it is not", as the unix version above does on+/// every error. Sharing one probe would invert one of them. Moot in+/// practice: `pid_holding` returns `None` here, so both callers stop before+/// they reach this.#[cfg(not(unix))]pub fn socket_answers(_path: &Path) -> bool {falsediff --git a/wezterm-mux-server-impl/src/local.rs b/wezterm-mux-server-impl/src/local.rsindex 4f23d0f5..022440db 100644--- a/wezterm-mux-server-impl/src/local.rs+++ b/wezterm-mux-server-impl/src/local.rs@@ -153,6 +153,11 @@ fn other_server_holding_pid_file(pid_file: &std::path::Path) -> Option<u32> {(unsafe { libc::kill(pid as libc::pid_t, 0) } == 0).then_some(pid)}+/// No answer off unix: the pid file is written by `daemonize`, which is+/// `#![cfg(unix)]` entire, so on Windows there is nothing holding a lock to+/// ask about. `someone_listens` carries the whole guard there, which leaves+/// a server stalled long enough to fill its accept backlog able to be bound+/// over -- the case this pid file exists to catch on unix.#[cfg(not(unix))]fn other_server_holding_pid_file(_pid_file: &std::path::Path) -> Option<u32> {None@@ -204,7 +209,15 @@ pub fn someone_listens(path: &std::path::Path) -> bool {)}-#[cfg(not(unix))]+/// Windows has real `AF_UNIX`, so the same question has a real answer; see+/// [`wezterm_uds::someone_listens`] for what its error codes can and cannot+/// distinguish.+#[cfg(windows)]+pub fn someone_listens(path: &std::path::Path) -> bool {+ wezterm_uds::someone_listens(path)+}++#[cfg(not(any(unix, windows)))]pub fn someone_listens(_path: &std::path::Path) -> bool {false}diff --git a/wezterm-uds/src/lib.rs b/wezterm-uds/src/lib.rsindex 01935bdd..dde579dc 100644--- a/wezterm-uds/src/lib.rs+++ b/wezterm-uds/src/lib.rs@@ -152,3 +152,55 @@ impl std::ops::DerefMut for UnixListener {&mut self.0}}++/// Whether a listener answers on the socket at `path`.+///+/// Windows only. The unix side asks this question in two places with two+/// different answers for "the probe itself failed", so each keeps its own+/// `sockaddr_un` version; there is one Windows implementation because+/// `uds_windows` gives no way to spell those differences anyway.+///+/// Windows answers a connect to a stale socket file and to a path that does+/// not exist with the same `WSAECONNREFUSED`, so that error is the only one+/// that means nobody is there. Every other failure counts as someone,+/// because binding over a live server is the outcome this exists to+/// prevent. A listener whose accept backlog is full also answers+/// `WSAECONNREFUSED` and will be read as absent; that is the same gap the+/// unix daemon path closes with a pid file, and Windows has none.+#[cfg(windows)]+pub fn someone_listens(path: &Path) -> bool {+ const WSAECONNREFUSED: i32 = 10061;+ match UnixStream::connect(path) {+ Ok(_) => true,+ Err(err) => err.raw_os_error() != Some(WSAECONNREFUSED),+ }+}++#[cfg(all(test, windows))]+mod tests {+ /// A live listener answers; the file it leaves behind does not. The+ /// second half is what makes the probe usable: Windows keeps the socket+ /// file after the listener is gone, so its presence proves nothing.+ #[test]+ fn only_a_live_listener_answers() {+ let path = std::env::temp_dir()+ .join(format!("wezterm-uds-probe-{}.sock", std::process::id()));+ let _ = std::fs::remove_file(&path);+ assert!(+ !super::someone_listens(&path),+ "nothing is bound at {}",+ path.display()+ );++ let listener = super::UnixListener::bind(&path).unwrap();+ assert!(super::someone_listens(&path), "the listener must answer");++ drop(listener);+ assert!(path.exists(), "windows leaves the socket file behind");+ assert!(+ !super::someone_listens(&path),+ "a stale file must not read as a listener"+ );+ let _ = std::fs::remove_file(&path);+ }+}