Files
zed/crates/remote_server/src/server.rs
T
Sergio Ugalde 7dc397b61b remote_server: Use CreateProcess to spawn daemon on Windows (#55292)
When connecting to a Windows host over SSH, the proxy fails to start the
daemon and the user sees "failed to spawn server."

**Root cause**

`spawn_server_windows` invokes the daemon via
`shell_execute_from_explorer`, which goes through COM `ShellWindows` /
`IShellDispatch2::ShellExecute`. That path requires both:

- `explorer.exe` running in the caller's session, and
- an interactive desktop / window station.

Neither is available when `zed-remote-server proxy` is launched by sshd:
sshd-spawned processes live in a non-interactive window station with no
Explorer. The COM call fails, the `run` daemon never starts, the proxy's
10s socket-existence poll times out, and the connection fails.

A commenter on #48966 reports the connection succeeds after first
logging in via RDP, which is consistent with this diagnosis: RDP login
populates the session with `explorer.exe` and an interactive window
station.

**Fix**

Replace the COM path with a direct `Command::spawn`, matching the
non-Windows branch. Use `DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP`
creation flags so the daemon has no inherited console and is not killed
by Ctrl+C/Break broadcast to the parent's group when the proxy exits.

Bypass `util::new_command` because it presets `CREATE_NO_WINDOW`, which
Win32 disallows combining with `DETACHED_PROCESS`. Drop one layer to
`smol::process::Command` directly to keep the disallowed-methods lint
happy.

Also drop the now-unused `shell_execute_from_explorer` module and the
`windows` crate dependency it pulled in.

No automated test is included: exercising this path requires a headless
OpenSSH-Server-on-Windows host with a non-interactive window station,
which CI doesn't currently provide. Verified manually that macOS client
-> Windows host over SSH establishes the connection successfully where
it previously timed out.

Closes #48966

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Fixed remote server failing to start when connecting to a Windows host
over SSH

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: John Tur <john-tur@outlook.com>
2026-10-14 08:56:21 +00:00

1808 lines
62 KiB
Rust

mod headless_project;
#[cfg(test)]
mod remote_editing_tests;
pub use headless_project::{HeadlessAppState, HeadlessProject};
use anyhow::{Context as _, Result, anyhow};
use clap::Subcommand;
use client::ProxySettings;
use collections::HashMap;
use extension::ExtensionHostProxy;
use fs::{Fs, RealFs};
use futures::{
AsyncRead, AsyncWrite, AsyncWriteExt, FutureExt, SinkExt,
channel::{mpsc, oneshot},
select, select_biased,
};
use git::GitHostingProviderRegistry;
use gpui::{App, AppContext as _, Context, Entity, UpdateGlobal as _};
use gpui_tokio::Tokio;
use http_client::{Url, read_proxy_from_env};
use language::LanguageRegistry;
use net::async_net::{UnixListener, UnixStream};
use node_runtime::{NodeBinaryOptions, NodeRuntime};
use paths::logs_dir;
use project::{project_settings::ProjectSettings, trusted_worktrees};
use proto::CrashReport;
use release_channel::{AppCommitSha, AppVersion, RELEASE_CHANNEL, ReleaseChannel};
#[cfg(unix)]
use remote::command::RemoteCommand;
use remote::{
RemoteClient,
json_log::LogRecord,
protocol::{read_message, write_message},
proxy::ProxyLaunchError,
};
use reqwest_client::ReqwestClient;
use rpc::proto::{self, Envelope, REMOTE_SERVER_PROJECT_ID};
use rpc::{AnyProtoClient, TypedEnvelope};
use settings::{Settings, SettingsStore, watch_config_file};
use smol::{
Timer,
channel::{Receiver, Sender},
io::AsyncReadExt,
stream::StreamExt as _,
};
use std::{
ffi::OsStr,
fs::File,
io::Write,
mem,
path::{Path, PathBuf},
str::FromStr,
sync::{Arc, LazyLock},
time::Instant,
};
#[cfg(unix)]
use std::{
ffi::OsString,
os::{
fd::AsFd as _,
unix::{ffi::OsStrExt as _, process::CommandExt as _},
},
};
use thiserror::Error;
use util::{ResultExt, command::new_command};
#[derive(Subcommand)]
pub enum Commands {
Run {
#[arg(long)]
log_file: PathBuf,
#[arg(long)]
pid_file: PathBuf,
#[arg(long)]
stdin_socket: PathBuf,
#[arg(long)]
stdout_socket: PathBuf,
#[arg(long)]
stderr_socket: PathBuf,
},
Proxy {
#[arg(long)]
reconnect: bool,
#[arg(long)]
identifier: String,
},
Version,
#[cfg(unix)]
Exec,
}
pub fn run(command: Commands) -> anyhow::Result<()> {
use anyhow::Context;
use release_channel::{RELEASE_CHANNEL, ReleaseChannel};
match command {
Commands::Run {
log_file,
pid_file,
stdin_socket,
stdout_socket,
stderr_socket,
} => execute_run(
log_file,
pid_file,
stdin_socket,
stdout_socket,
stderr_socket,
),
Commands::Proxy {
identifier,
reconnect,
} => execute_proxy(identifier, reconnect).context("running proxy on the remote server"),
#[cfg(unix)]
Commands::Exec => execute_command(),
Commands::Version => {
let release_channel = *RELEASE_CHANNEL;
match release_channel {
ReleaseChannel::Stable | ReleaseChannel::Preview => {
println!("{}", env!("ZED_PKG_VERSION"))
}
ReleaseChannel::Nightly | ReleaseChannel::Dev => {
let commit_sha =
option_env!("ZED_COMMIT_SHA").unwrap_or(release_channel.dev_name());
let build_id = option_env!("ZED_BUILD_ID");
if let Some(build_id) = build_id {
println!("{}+{}", build_id, commit_sha)
} else {
println!("{commit_sha}");
}
}
};
Ok(())
}
}
}
pub static VERSION: LazyLock<String> = LazyLock::new(|| match *RELEASE_CHANNEL {
ReleaseChannel::Stable | ReleaseChannel::Preview => env!("ZED_PKG_VERSION").to_owned(),
ReleaseChannel::Nightly | ReleaseChannel::Dev => {
let commit_sha = option_env!("ZED_COMMIT_SHA").unwrap_or("missing-zed-commit-sha");
let build_identifier = option_env!("ZED_BUILD_ID");
if let Some(build_id) = build_identifier {
format!("{build_id}+{commit_sha}")
} else {
commit_sha.to_owned()
}
}
});
#[cfg(unix)]
fn execute_command() -> Result<()> {
let mut stdin = File::from(std::io::stdin().as_fd().try_clone_to_owned()?);
let descriptor = RemoteCommand::read(&mut stdin)?;
drop(stdin);
let working_dir = descriptor
.working_dir
.as_deref()
.map(|working_dir| shellexpand::tilde(working_dir).into_owned());
if let Some(working_dir) = &working_dir {
std::env::set_current_dir(working_dir)
.with_context(|| format!("Failed to change directory to {working_dir}"))?;
}
let error = exec_with_script_fallback(&descriptor, working_dir.as_deref(), |mut command| {
command.exec()
});
Err(error).with_context(|| format!("Failed to execute {}", descriptor.program))
}
#[cfg(unix)]
fn exec_with_script_fallback(
descriptor: &RemoteCommand,
working_dir: Option<&str>,
mut exec: impl FnMut(std::process::Command) -> std::io::Error,
) -> std::io::Error {
let error = exec(exec_command(
descriptor,
working_dir,
&descriptor.program,
&descriptor.args,
));
if error.raw_os_error() != Some(libc::ENOEXEC) {
return error;
}
let path = descriptor
.env
.get("PATH")
.map(OsString::from)
.or_else(|| std::env::var_os("PATH"));
for candidate in script_candidates(&descriptor.program, path.as_deref()) {
let candidate_error = exec(exec_command(
descriptor,
working_dir,
&candidate,
&descriptor.args,
));
match candidate_error.raw_os_error() {
Some(libc::ENOEXEC) => {
let args = std::iter::once(candidate.into_os_string())
.chain(descriptor.args.iter().map(OsString::from))
.collect::<Vec<_>>();
return exec(exec_command(descriptor, working_dir, "/bin/sh", &args));
}
Some(libc::ENOENT | libc::ENOTDIR | libc::EACCES) => {}
_ => return candidate_error,
}
}
error
}
#[cfg(unix)]
fn script_candidates(program: &str, path: Option<&OsStr>) -> Vec<PathBuf> {
let explicit = |candidate: PathBuf| {
if candidate.is_absolute() {
candidate
} else {
Path::new(".").join(candidate)
}
};
if program.contains('/') {
return vec![explicit(PathBuf::from(program))];
}
path.unwrap_or(OsStr::new(DEFAULT_PATH))
.as_bytes()
.split(|byte| *byte == b':')
.map(|directory| explicit(Path::new(OsStr::from_bytes(directory)).join(program)))
.collect()
}
#[cfg(unix)]
const DEFAULT_PATH: &str = "/usr/local/bin:/bin:/usr/bin";
#[cfg(unix)]
fn exec_command(
descriptor: &RemoteCommand,
working_dir: Option<&str>,
program: impl AsRef<OsStr>,
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
) -> std::process::Command {
let mut command = std::process::Command::new(program);
command.args(args);
if let Some(working_dir) = working_dir {
command.env("PWD", working_dir);
}
command.envs(&descriptor.env);
command
}
fn init_logging_proxy() {
env_logger::builder()
.format(|buf, record| {
let mut log_record = LogRecord::new(record);
log_record.message =
std::borrow::Cow::Owned(format!("(remote proxy) {}", log_record.message));
serde_json::to_writer(&mut *buf, &log_record)?;
buf.write_all(b"\n")?;
Ok(())
})
.init();
}
const REMOTE_SERVER_LOG_MAX_BYTES: u64 = 1024 * 1024;
struct RotatingLogFile {
path: PathBuf,
file: File,
size_bytes: u64,
}
impl RotatingLogFile {
fn open(path: &Path) -> Result<Self> {
if std::fs::metadata(path)
.map(|metadata| metadata.len() >= REMOTE_SERVER_LOG_MAX_BYTES)
.unwrap_or(false)
{
rotate_log_file(path, &rotated_log_path(path))
.context("failed to rotate existing remote server log")?;
}
let file = open_log_file(path).context("failed to open remote server log")?;
let size_bytes = file
.metadata()
.context("failed to read remote server log metadata")?
.len();
Ok(Self {
path: path.to_path_buf(),
file,
size_bytes,
})
}
fn rotate(&mut self) -> std::io::Result<()> {
self.file.flush()?;
rotate_log_file(&self.path, &rotated_log_path(&self.path))?;
self.file = open_log_file(&self.path)?;
self.size_bytes = 0;
Ok(())
}
}
impl Write for RotatingLogFile {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if self.size_bytes.saturating_add(buf.len() as u64) > REMOTE_SERVER_LOG_MAX_BYTES {
self.rotate()?;
}
self.file.write_all(buf)?;
self.size_bytes += buf.len() as u64;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}
fn open_log_file(path: &Path) -> std::io::Result<File> {
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
}
fn rotated_log_path(path: &Path) -> PathBuf {
path.with_extension("1.log")
}
fn rotate_log_file(path: &Path, rotated_path: &Path) -> std::io::Result<()> {
match std::fs::remove_file(rotated_path) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
match std::fs::rename(path, rotated_path) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
Ok(())
}
fn init_logging_server(log_file_path: &Path) -> Result<Receiver<Vec<u8>>> {
struct MultiWrite {
file: RotatingLogFile,
channel: Sender<Vec<u8>>,
buffer: Vec<u8>,
}
impl Write for MultiWrite {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.file.write_all(buf)?;
self.buffer.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.channel
.send_blocking(self.buffer.clone())
.map_err(std::io::Error::other)?;
self.buffer.clear();
self.file.flush()
}
}
let log_file = RotatingLogFile::open(log_file_path)
.context("Failed to open rotating remote server log file")?;
let (tx, rx) = async_channel::unbounded();
let target = Box::new(MultiWrite {
file: log_file,
channel: tx,
buffer: Vec::new(),
});
let old_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
let backtrace = std::backtrace::Backtrace::force_capture();
let message = info.payload_as_str().unwrap_or("Box<Any>").to_owned();
let location = info
.location()
.map_or_else(|| "<unknown>".to_owned(), |location| location.to_string());
let current_thread = std::thread::current();
let thread_name = current_thread.name().unwrap_or("<unnamed>");
let msg = format!("thread '{thread_name}' panicked at {location}:\n{message}\n{backtrace}");
log::error!("{msg}");
old_hook(info);
}));
env_logger::Builder::new()
.filter_level(log::LevelFilter::Info)
.parse_default_env()
.target(env_logger::Target::Pipe(target))
.format(|buf, record| {
let mut log_record = LogRecord::new(record);
log_record.message =
std::borrow::Cow::Owned(format!("(remote server) {}", log_record.message));
serde_json::to_writer(&mut *buf, &log_record)?;
buf.write_all(b"\n")?;
Ok(())
})
.init();
Ok(rx)
}
/// Initializes the telemetry queue on the remote server, forwarding every
/// emitted event to the connected client over the proto channel.
///
/// The remote server cannot upload telemetry itself (it has no logged-in user,
/// no checksum seed, and no Telemetry instance), so without this its
/// `telemetry::event!` calls are silently dropped. The client attributes these
/// events to the remote host using the platform it already detected during
/// connection setup, so no OS metadata needs to be sent here.
fn init_telemetry_forwarding(session: AnyProtoClient, cx: &mut App) {
let (tx, mut rx) = mpsc::unbounded::<telemetry::Event>();
telemetry::init(tx);
cx.background_spawn(async move {
while let Some(event) = rx.next().await {
let Some(event_json) = serde_json::to_string(&event).log_err() else {
continue;
};
session
.send(proto::TelemetryEvent {
project_id: REMOTE_SERVER_PROJECT_ID,
event_json,
})
.log_err();
}
})
.detach();
}
fn handle_crash_files_requests(project: &Entity<HeadlessProject>, client: &AnyProtoClient) {
client.add_request_handler(
project.downgrade(),
|_, _: TypedEnvelope<proto::GetCrashFiles>, _cx| async move {
let mut legacy_panics = Vec::new();
let mut crashes = Vec::new();
let mut children = smol::fs::read_dir(paths::logs_dir()).await?;
while let Some(child) = children.next().await {
let child = child?;
let child_path = child.path();
let extension = child_path.extension();
if extension == Some(OsStr::new("panic")) {
let filename = if let Some(filename) = child_path.file_name() {
filename.to_string_lossy()
} else {
continue;
};
if !filename.starts_with("zed") {
continue;
}
let file_contents = smol::fs::read_to_string(&child_path)
.await
.context("error reading panic file")?;
legacy_panics.push(file_contents);
smol::fs::remove_file(&child_path)
.await
.context("error removing panic")
.log_err();
} else if extension == Some(OsStr::new("dmp")) {
let mut json_path = child_path.clone();
json_path.set_extension("json");
if let Ok(json_content) = smol::fs::read_to_string(&json_path).await {
crashes.push(CrashReport {
metadata: json_content,
minidump_contents: smol::fs::read(&child_path).await?,
});
smol::fs::remove_file(&child_path).await.log_err();
smol::fs::remove_file(&json_path).await.log_err();
} else {
log::error!("Couldn't find json metadata for crash: {child_path:?}");
}
}
}
anyhow::Ok(proto::GetCrashFilesResponse { crashes })
},
);
}
struct ServerListeners {
stdin: UnixListener,
stdout: UnixListener,
stderr: UnixListener,
}
impl ServerListeners {
pub fn new(stdin_path: PathBuf, stdout_path: PathBuf, stderr_path: PathBuf) -> Result<Self> {
Ok(Self {
stdin: UnixListener::bind(stdin_path).context("failed to bind stdin socket")?,
stdout: UnixListener::bind(stdout_path).context("failed to bind stdout socket")?,
stderr: UnixListener::bind(stderr_path).context("failed to bind stderr socket")?,
})
}
}
fn start_server(
listeners: ServerListeners,
log_rx: Receiver<Vec<u8>>,
cx: &mut App,
is_wsl_interop: bool,
) -> AnyProtoClient {
// This is the server idle timeout. If no connection comes in this timeout, the server will shut down.
const IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10 * 60);
let (incoming_tx, incoming_rx) = mpsc::unbounded::<Envelope>();
let (outgoing_tx, mut outgoing_rx) = mpsc::unbounded::<Envelope>();
let (app_quit_tx, mut app_quit_rx) = mpsc::unbounded::<()>();
cx.on_app_quit(move |_| {
let mut app_quit_tx = app_quit_tx.clone();
async move {
log::info!("app quitting. sending signal to server main loop");
app_quit_tx.send(()).await.ok();
}
})
.detach();
cx.spawn(async move |cx| {
loop {
let streams = futures::future::join3(
listeners.stdin.accept(),
listeners.stdout.accept(),
listeners.stderr.accept(),
);
log::info!("accepting new connections");
let result = select! {
streams = streams.fuse() => {
let (Ok((stdin_stream, _)), Ok((stdout_stream, _)), Ok((stderr_stream, _))) = streams else {
log::error!("failed to accept new connections");
break;
};
log::info!("accepted new connections");
anyhow::Ok((stdin_stream, stdout_stream, stderr_stream))
}
_ = futures::FutureExt::fuse(cx.background_executor().timer(IDLE_TIMEOUT)) => {
log::warn!("timed out waiting for new connections after {:?}. exiting.", IDLE_TIMEOUT);
cx.update(|cx| {
// TODO: This is a hack, because in a headless project, shutdown isn't executed
// when calling quit, but it should be.
cx.shutdown();
cx.quit();
});
break;
}
_ = app_quit_rx.next().fuse() => {
log::info!("app quit requested");
break;
}
};
let Ok((mut stdin_stream, mut stdout_stream, mut stderr_stream)) = result else {
break;
};
let _log_task = cx.background_spawn({
let log_rx = log_rx.clone();
async move {
while let Ok(log_message) = log_rx.recv().await {
if let Err(error) = stderr_stream.write_all(&log_message).await {
log::error!("failed to write log message to stderr: {error:?}");
break;
}
if let Err(error) = stderr_stream.flush().await {
log::error!("failed to flush stderr stream: {error:?}");
break;
}
}
}
});
let mut input_buffer = Vec::new();
let mut output_buffer = Vec::new();
let stdin_task = cx.background_spawn({
let incoming_tx = incoming_tx.clone();
async move {
loop {
match read_message(&mut stdin_stream, &mut input_buffer).await {
Ok(msg) => {
if let Err(error) = incoming_tx.unbounded_send(msg) {
log::error!("failed to send message to application: {error:?}. exiting.");
return Err(anyhow!(error));
}
}
Err(error) => {
log::warn!("stdin read failed: {error:?}");
break;
}
}
}
anyhow::Ok(())
}
});
let stdout_task = async {
loop {
let Some(message) = outgoing_rx.next().await else {
log::error!("stdout handler, no message");
break;
};
if let Err(error) =
write_message(&mut stdout_stream, &mut output_buffer, message).await
{
log::error!("failed to write stdout message: {:?}", error);
break;
}
if let Err(error) = stdout_stream.flush().await {
log::error!("failed to flush stdout message: {:?}", error);
break;
}
}
};
select_biased! {
_ = app_quit_rx.next().fuse() => {
return anyhow::Ok(());
}
result = stdin_task.fuse() => {
result?;
}
_ = stdout_task.fuse() => {}
}
}
anyhow::Ok(())
})
.detach();
RemoteClient::proto_client_from_channels(incoming_rx, outgoing_tx, cx, "server", is_wsl_interop)
}
fn init_paths() -> anyhow::Result<()> {
for path in [
paths::config_dir(),
paths::extensions_dir(),
paths::languages_dir(),
paths::logs_dir(),
paths::temp_dir(),
paths::hang_traces_dir(),
paths::remote_extensions_dir(),
paths::remote_extensions_uploads_dir(),
]
.iter()
{
std::fs::create_dir_all(path).with_context(|| format!("creating directory {path:?}"))?;
}
Ok(())
}
pub fn execute_run(
log_file: PathBuf,
pid_file: PathBuf,
stdin_socket: PathBuf,
stdout_socket: PathBuf,
stderr_socket: PathBuf,
) -> Result<()> {
init_paths()?;
let startup_time = Instant::now();
let app = gpui_platform::headless();
let pid = std::process::id();
let id = pid.to_string();
let should_install_crash_handler =
client::telemetry::should_install_crash_handler(*RELEASE_CHANNEL);
let crash_handler = if should_install_crash_handler {
Some(app.background_executor().spawn(crashes::init(
crashes::InitCrashHandler {
session_id: id,
zed_version: VERSION.to_owned(),
binary: "zed-remote-server".to_string(),
release_channel: release_channel::RELEASE_CHANNEL_NAME.clone(),
commit_sha: option_env!("ZED_COMMIT_SHA").unwrap_or("no_sha").to_owned(),
},
{
let background_executor = app.background_executor();
move |task| {
background_executor.spawn(task).detach();
}
},
|pid| paths::temp_dir().join(format!("zed-remote-server-crash-handler-{pid}")),
// we are running outside gpui
#[allow(clippy::disallowed_methods)]
|duration| FutureExt::map(Timer::after(duration), |_| ()),
)))
} else {
crashes::force_backtrace();
None
};
let log_rx = init_logging_server(&log_file)?;
log::info!(
"starting up with PID {}:\npid_file: {:?}, log_file: {:?}, stdin_socket: {:?}, stdout_socket: {:?}, stderr_socket: {:?}",
pid,
pid_file,
log_file,
stdin_socket,
stdout_socket,
stderr_socket
);
write_pid_file(&pid_file, pid)
.with_context(|| format!("failed to write pid file: {pid_file:?}"))?;
let listeners = ServerListeners::new(stdin_socket, stdout_socket, stderr_socket)?;
rayon::ThreadPoolBuilder::new()
.num_threads(std::thread::available_parallelism().map_or(1, |n| n.get().div_ceil(2)))
.stack_size(10 * 1024 * 1024)
.thread_name(|ix| format!("RayonWorker{}", ix))
.build_global()
.unwrap();
#[cfg(unix)]
let shell_env_loaded_rx = {
let (shell_env_loaded_tx, shell_env_loaded_rx) = oneshot::channel();
app.background_executor()
.spawn(async {
util::load_login_shell_environment().await.log_err();
shell_env_loaded_tx.send(()).ok();
})
.detach();
Some(shell_env_loaded_rx)
};
#[cfg(windows)]
let shell_env_loaded_rx: Option<oneshot::Receiver<()>> = None;
let git_hosting_provider_registry = Arc::new(GitHostingProviderRegistry::new());
let run = move |cx: &mut App| {
if let Some(crash_handler) = crash_handler {
cx.spawn(async move |_cx| {
let _crash_handler = crash_handler.await;
// cx.update(|cx| cx.set_global(CrashHandler(crash_handler)))
})
.detach();
}
settings::init(cx);
let app_commit_sha = option_env!("ZED_COMMIT_SHA").map(|s| AppCommitSha::new(s.to_owned()));
let app_version = AppVersion::load(
env!("ZED_PKG_VERSION"),
option_env!("ZED_BUILD_ID"),
app_commit_sha,
);
release_channel::init(app_version, cx);
gpui_tokio::init(cx);
HeadlessProject::init(cx);
let is_wsl_interop = if cfg!(target_os = "linux") {
// See: https://learn.microsoft.com/en-us/windows/wsl/filesystems#disable-interoperability
matches!(std::fs::read_to_string("/proc/sys/fs/binfmt_misc/WSLInterop").or_else(|_| std::fs::read_to_string("/proc/sys/fs/binfmt_misc/WSLInterop-late")), Ok(s) if s.contains("enabled"))
} else {
false
};
log::info!("gpui app started, initializing server");
let session = start_server(listeners, log_rx, cx, is_wsl_interop);
init_telemetry_forwarding(session.clone(), cx);
trusted_worktrees::init(HashMap::default(), cx);
GitHostingProviderRegistry::set_global(git_hosting_provider_registry, cx);
git_hosting_providers::init(cx);
dap_adapters::init(cx);
extension::init(cx);
let extension_host_proxy = ExtensionHostProxy::global(cx);
json_schema_store::init(cx);
let project = cx.new(|cx| {
let fs = RealFs::new(None, cx.background_executor().clone());
let node_settings_rx = initialize_settings(session.clone(), fs.clone(), cx);
let proxy_url = read_proxy_settings(cx);
let http_client = {
let _guard = Tokio::handle(cx).enter();
Arc::new(
ReqwestClient::proxy_and_user_agent(
proxy_url,
&format!(
"Zed-Server/{} ({}; {})",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
std::env::consts::ARCH
),
)
.expect("Could not start HTTP client"),
)
};
let node_runtime =
NodeRuntime::new(http_client.clone(), shell_env_loaded_rx, node_settings_rx);
let mut languages = LanguageRegistry::new(cx.background_executor().clone());
languages.set_language_server_download_dir(paths::languages_dir().clone());
let languages = Arc::new(languages);
HeadlessProject::new(
HeadlessAppState {
session: session.clone(),
fs,
http_client,
node_runtime,
languages,
extension_host_proxy,
startup_time,
},
true,
cx,
)
});
handle_crash_files_requests(&project, &session);
cx.background_spawn(async move {
cleanup_old_binaries_wsl();
cleanup_old_binaries()
})
.detach();
mem::forget(project);
};
// We do not reuse any of the state after unwinding, so we don't run risk of observing broken invariants.
let app = std::panic::AssertUnwindSafe(app);
let run = std::panic::AssertUnwindSafe(run);
let res = std::panic::catch_unwind(move || { app }.0.run({ run }.0));
if let Err(_) = res {
log::error!("app panicked. quitting.");
Err(anyhow::anyhow!("panicked"))
} else {
log::info!("gpui app is shut down. quitting.");
Ok(())
}
}
#[derive(Debug, Error)]
pub enum ServerPathError {
#[error("Failed to create server_dir `{path}`")]
CreateServerDir {
#[source]
source: std::io::Error,
path: PathBuf,
},
#[error("Failed to create logs_dir `{path}`")]
CreateLogsDir {
#[source]
source: std::io::Error,
path: PathBuf,
},
}
#[derive(Clone, Debug)]
struct ServerPaths {
log_file: PathBuf,
pid_file: PathBuf,
stdin_socket: PathBuf,
stdout_socket: PathBuf,
stderr_socket: PathBuf,
}
impl ServerPaths {
fn new(identifier: &str) -> Result<Self, ServerPathError> {
let server_dir = paths::remote_server_state_dir().join(identifier);
std::fs::create_dir_all(&server_dir).map_err(|source| {
ServerPathError::CreateServerDir {
source,
path: server_dir.clone(),
}
})?;
let log_dir = logs_dir();
std::fs::create_dir_all(log_dir).map_err(|source| ServerPathError::CreateLogsDir {
source,
path: log_dir.clone(),
})?;
let pid_file = server_dir.join("server.pid");
let stdin_socket = server_dir.join("stdin.sock");
let stdout_socket = server_dir.join("stdout.sock");
let stderr_socket = server_dir.join("stderr.sock");
let log_file = logs_dir().join(format!("server-{}.log", identifier));
Ok(Self {
pid_file,
stdin_socket,
stdout_socket,
stderr_socket,
log_file,
})
}
}
#[derive(Debug, Error)]
pub enum ExecuteProxyError {
#[error("Failed to init server paths: {0:#}")]
ServerPath(#[from] ServerPathError),
#[error(transparent)]
ServerNotRunning(#[from] ProxyLaunchError),
#[error("Failed to check PidFile '{path}': {source:#}")]
CheckPidFile {
#[source]
source: CheckPidError,
path: PathBuf,
},
#[error("Failed to kill existing server with pid '{pid}'")]
KillRunningServer { pid: u32 },
#[error("failed to spawn server")]
SpawnServer(#[source] SpawnServerError),
#[error("stdin_task failed: {0:#}")]
StdinTask(#[source] anyhow::Error),
#[error("stdout_task failed: {0:#}")]
StdoutTask(#[source] anyhow::Error),
#[error("stderr_task failed: {0:#}")]
StderrTask(#[source] anyhow::Error),
}
impl ExecuteProxyError {
pub fn to_exit_code(&self) -> i32 {
match self {
ExecuteProxyError::ServerNotRunning(proxy_launch_error) => {
proxy_launch_error.to_exit_code()
}
_ => 1,
}
}
}
pub(crate) fn execute_proxy(
identifier: String,
is_reconnecting: bool,
) -> Result<(), ExecuteProxyError> {
init_logging_proxy();
let server_paths = ServerPaths::new(&identifier)?;
let id = std::process::id().to_string();
let should_install_crash_handler =
client::telemetry::should_install_crash_handler(*RELEASE_CHANNEL);
if should_install_crash_handler {
smol::spawn(crashes::init(
crashes::InitCrashHandler {
session_id: id,
zed_version: VERSION.to_owned(),
binary: "zed-remote-proxy".to_string(),
release_channel: release_channel::RELEASE_CHANNEL_NAME.clone(),
commit_sha: option_env!("ZED_COMMIT_SHA").unwrap_or("no_sha").to_owned(),
},
|task| {
smol::spawn(task).detach();
},
|pid| paths::temp_dir().join(format!("zed-remote-server-proxy-crash-handler-{pid}")),
// we are running outside gpui
#[allow(clippy::disallowed_methods)]
|duration| FutureExt::map(Timer::after(duration), |_| ()),
))
.detach();
};
log::info!("starting proxy process. PID: {}", std::process::id());
let server_pid = {
let server_pid = check_pid_file(&server_paths.pid_file).map_err(|source| {
ExecuteProxyError::CheckPidFile {
source,
path: server_paths.pid_file.clone(),
}
})?;
if is_reconnecting {
match server_pid {
None => {
log::error!("attempted to reconnect, but no server running");
return Err(ExecuteProxyError::ServerNotRunning(
ProxyLaunchError::ServerNotRunning,
));
}
Some(server_pid) => server_pid,
}
} else {
if let Some(pid) = server_pid {
log::info!(
"proxy found server already running with PID {}. Killing process and cleaning up files...",
pid
);
kill_running_server(pid, &server_paths)?;
}
gpui::block_on(spawn_server(&server_paths)).map_err(ExecuteProxyError::SpawnServer)?;
std::fs::read_to_string(&server_paths.pid_file)
.and_then(|contents| {
contents.parse::<u32>().map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid PID file contents",
)
})
})
.map_err(SpawnServerError::ProcessStatus)
.map_err(ExecuteProxyError::SpawnServer)?
}
};
let stdin_task = smol::spawn(async move {
let stdin = smol::Unblock::new(std::io::stdin());
let stream = UnixStream::connect(&server_paths.stdin_socket)
.await
.with_context(|| {
format!(
"Failed to connect to stdin socket {}",
server_paths.stdin_socket.display()
)
})?;
handle_io(stdin, stream, "stdin").await
});
let stdout_task: smol::Task<Result<()>> = smol::spawn(async move {
let stdout = smol::Unblock::new(std::io::stdout());
let stream = UnixStream::connect(&server_paths.stdout_socket)
.await
.with_context(|| {
format!(
"Failed to connect to stdout socket {}",
server_paths.stdout_socket.display()
)
})?;
handle_io(stream, stdout, "stdout").await
});
let stderr_task: smol::Task<Result<()>> = smol::spawn(async move {
let mut stderr = smol::Unblock::new(std::io::stderr());
let mut stream = UnixStream::connect(&server_paths.stderr_socket)
.await
.with_context(|| {
format!(
"Failed to connect to stderr socket {}",
server_paths.stderr_socket.display()
)
})?;
let mut stderr_buffer = vec![0; 2048];
loop {
match stream
.read(&mut stderr_buffer)
.await
.context("reading stderr")?
{
0 => {
let error =
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "stderr closed");
Err(anyhow!(error))?;
}
n => {
stderr.write_all(&stderr_buffer[..n]).await?;
stderr.flush().await?;
}
}
}
});
if let Err(forwarding_result) = gpui::block_on(async move {
futures::select! {
result = stdin_task.fuse() => result.map_err(ExecuteProxyError::StdinTask),
result = stdout_task.fuse() => result.map_err(ExecuteProxyError::StdoutTask),
result = stderr_task.fuse() => result.map_err(ExecuteProxyError::StderrTask),
}
}) {
log::error!("encountered error while forwarding messages: {forwarding_result:#}",);
if !matches!(gpui::block_on(check_server_running(server_pid)), Ok(true)) {
log::error!("server exited unexpectedly");
return Err(ExecuteProxyError::ServerNotRunning(
ProxyLaunchError::ServerNotRunning,
));
}
return Err(forwarding_result);
}
Ok(())
}
fn kill_running_server(pid: u32, paths: &ServerPaths) -> Result<(), ExecuteProxyError> {
log::info!("killing existing server with PID {}", pid);
let system = sysinfo::System::new_with_specifics(
sysinfo::RefreshKind::nothing().with_processes(sysinfo::ProcessRefreshKind::nothing()),
);
if let Some(process) = system.process(sysinfo::Pid::from_u32(pid)) {
let killed = process.kill();
if !killed {
return Err(ExecuteProxyError::KillRunningServer { pid });
}
}
for file in [
&paths.pid_file,
&paths.stdin_socket,
&paths.stdout_socket,
&paths.stderr_socket,
] {
log::debug!("cleaning up file {:?} before starting new server", file);
std::fs::remove_file(file).ok();
}
Ok(())
}
#[derive(Debug, Error)]
pub enum SpawnServerError {
#[error("failed to remove stdin socket")]
RemoveStdinSocket(#[source] std::io::Error),
#[error("failed to remove stdout socket")]
RemoveStdoutSocket(#[source] std::io::Error),
#[error("failed to remove stderr socket")]
RemoveStderrSocket(#[source] std::io::Error),
#[error("failed to get current_exe")]
CurrentExe(#[source] std::io::Error),
#[error("failed to launch server process")]
ProcessStatus(#[source] std::io::Error),
#[error("failed to wait for server to be ready to accept connections")]
Timeout,
}
async fn spawn_server(paths: &ServerPaths) -> Result<(), SpawnServerError> {
log::info!("spawning server process",);
if paths.stdin_socket.exists() {
std::fs::remove_file(&paths.stdin_socket).map_err(SpawnServerError::RemoveStdinSocket)?;
}
if paths.stdout_socket.exists() {
std::fs::remove_file(&paths.stdout_socket).map_err(SpawnServerError::RemoveStdoutSocket)?;
}
if paths.stderr_socket.exists() {
std::fs::remove_file(&paths.stderr_socket).map_err(SpawnServerError::RemoveStderrSocket)?;
}
let binary_name = std::env::current_exe().map_err(SpawnServerError::CurrentExe)?;
#[cfg(windows)]
{
spawn_server_windows(&binary_name, paths)?;
}
#[cfg(not(windows))]
{
spawn_server_normal(&binary_name, paths)?;
}
let mut total_time_waited = std::time::Duration::from_secs(0);
let wait_duration = std::time::Duration::from_millis(20);
while !paths.stdout_socket.exists()
|| !paths.stdin_socket.exists()
|| !paths.stderr_socket.exists()
{
log::debug!("waiting for server to be ready to accept connections...");
std::thread::sleep(wait_duration);
total_time_waited += wait_duration;
if total_time_waited > std::time::Duration::from_secs(10) {
return Err(SpawnServerError::Timeout);
}
}
log::info!(
"server ready to accept connections. total time waited: {:?}",
total_time_waited
);
Ok(())
}
#[cfg(windows)]
fn spawn_server_windows(binary_name: &Path, paths: &ServerPaths) -> Result<(), SpawnServerError> {
use smol::process::windows::CommandExt;
// Detach from the console and break away from sshd's job so closing the SSH session doesn't kill the daemon.
const DETACHED_PROCESS: u32 = 0x0000_0008;
const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;
smol::process::Command::new(binary_name)
.stdin(util::command::Stdio::null())
.stdout(util::command::Stdio::null())
.stderr(util::command::Stdio::null())
.arg("run")
.arg("--log-file")
.arg(&paths.log_file)
.arg("--pid-file")
.arg(&paths.pid_file)
.arg("--stdin-socket")
.arg(&paths.stdin_socket)
.arg("--stdout-socket")
.arg(&paths.stdout_socket)
.arg("--stderr-socket")
.arg(&paths.stderr_socket)
.creation_flags(DETACHED_PROCESS | CREATE_BREAKAWAY_FROM_JOB)
.spawn()
.map_err(SpawnServerError::ProcessStatus)?;
Ok(())
}
#[cfg(not(windows))]
fn spawn_server_normal(binary_name: &Path, paths: &ServerPaths) -> Result<(), SpawnServerError> {
let mut server_process = new_command(binary_name);
server_process
.stdin(util::command::Stdio::null())
.stdout(util::command::Stdio::null())
.stderr(util::command::Stdio::null())
.arg("run")
.arg("--log-file")
.arg(&paths.log_file)
.arg("--pid-file")
.arg(&paths.pid_file)
.arg("--stdin-socket")
.arg(&paths.stdin_socket)
.arg("--stdout-socket")
.arg(&paths.stdout_socket)
.arg("--stderr-socket")
.arg(&paths.stderr_socket);
server_process
.spawn()
.map_err(SpawnServerError::ProcessStatus)?;
Ok(())
}
#[derive(Debug, Error)]
#[error("Failed to remove PID file for missing process (pid `{pid}`")]
pub struct CheckPidError {
#[source]
source: std::io::Error,
pid: u32,
}
async fn check_server_running(pid: u32) -> std::io::Result<bool> {
new_command("kill")
.arg("-0")
.arg(pid.to_string())
.output()
.await
.map(|output| output.status.success())
}
fn check_pid_file(path: &Path) -> Result<Option<u32>, CheckPidError> {
let Some(pid) = std::fs::read_to_string(path)
.ok()
.and_then(|contents| contents.parse::<u32>().ok())
else {
return Ok(None);
};
log::debug!("Checking if process with PID {} exists...", pid);
let system = sysinfo::System::new_with_specifics(
sysinfo::RefreshKind::nothing().with_processes(sysinfo::ProcessRefreshKind::nothing()),
);
if system.process(sysinfo::Pid::from_u32(pid)).is_some() {
log::debug!(
"Process with PID {} exists. NOT spawning new server, but attaching to existing one.",
pid
);
Ok(Some(pid))
} else {
log::debug!("Found PID file, but process with that PID does not exist. Removing PID file.");
std::fs::remove_file(path).map_err(|source| CheckPidError { source, pid })?;
Ok(None)
}
}
fn write_pid_file(path: &Path, pid: u32) -> Result<()> {
if path.exists() {
std::fs::remove_file(path)?;
}
log::debug!("writing PID {} to file {:?}", pid, path);
std::fs::write(path, pid.to_string()).context("Failed to write PID file")
}
async fn handle_io<R, W>(mut reader: R, mut writer: W, socket_name: &str) -> Result<()>
where
R: AsyncRead + Unpin,
W: AsyncWrite + Unpin,
{
use remote::protocol::{read_message_raw, write_size_prefixed_buffer};
let mut buffer = Vec::new();
loop {
read_message_raw(&mut reader, &mut buffer)
.await
.with_context(|| format!("failed to read message from {}", socket_name))?;
write_size_prefixed_buffer(&mut writer, &mut buffer)
.await
.with_context(|| format!("failed to write message to {}", socket_name))?;
writer.flush().await?;
buffer.clear();
}
}
fn initialize_settings(
session: AnyProtoClient,
fs: Arc<dyn Fs>,
cx: &mut App,
) -> watch::Receiver<Option<NodeBinaryOptions>> {
let (user_settings_file_rx, watcher_task) =
watch_config_file(cx.background_executor(), fs, paths::settings_file().clone());
handle_settings_file_changes(user_settings_file_rx, watcher_task, cx, {
move |err, _cx| {
if let Some(e) = err {
log::info!("Server settings failed to change: {}", e);
session
.send(proto::Toast {
project_id: REMOTE_SERVER_PROJECT_ID,
notification_id: "server-settings-failed".to_string(),
message: format!(
"Error in settings on remote host {:?}: {}",
paths::settings_file(),
e
),
})
.log_err();
} else {
session
.send(proto::HideToast {
project_id: REMOTE_SERVER_PROJECT_ID,
notification_id: "server-settings-failed".to_string(),
})
.log_err();
}
}
});
let (mut tx, rx) = watch::channel(None);
let mut node_settings = None;
cx.observe_global::<SettingsStore>(move |cx| {
let new_node_settings = &ProjectSettings::get_global(cx).node;
if Some(new_node_settings) != node_settings.as_ref() {
log::info!("Got new node settings: {new_node_settings:?}");
let options = NodeBinaryOptions {
allow_path_lookup: !new_node_settings.ignore_system_version,
// TODO: Implement this setting
allow_binary_download: true,
use_paths: new_node_settings.path.as_ref().map(|node_path| {
let node_path = PathBuf::from(shellexpand::tilde(node_path).as_ref());
let npm_path = new_node_settings
.npm_path
.as_ref()
.map(|path| PathBuf::from(shellexpand::tilde(&path).as_ref()));
(
node_path.clone(),
npm_path.unwrap_or_else(|| {
let base_path = PathBuf::new();
node_path.parent().unwrap_or(&base_path).join("npm")
}),
)
}),
};
node_settings = Some(new_node_settings.clone());
tx.send(Some(options)).ok();
}
})
.detach();
rx
}
pub fn handle_settings_file_changes(
mut server_settings_file: mpsc::UnboundedReceiver<String>,
watcher_task: gpui::Task<()>,
cx: &mut App,
settings_changed: impl Fn(Option<anyhow::Error>, &mut App) + 'static,
) {
let server_settings_content = cx
.foreground_executor()
.block_on(server_settings_file.next())
.unwrap();
SettingsStore::update_global(cx, |store, cx| {
store
.set_server_settings(&server_settings_content, cx)
.log_err();
});
cx.spawn(async move |cx| {
let _watcher_task = watcher_task;
while let Some(server_settings_content) = server_settings_file.next().await {
cx.update_global(|store: &mut SettingsStore, cx| {
let result = store.set_server_settings(&server_settings_content, cx);
if let Err(err) = &result {
log::error!("Failed to load server settings: {err}");
}
settings_changed(result.err(), cx);
cx.refresh_windows();
});
}
})
.detach();
}
fn read_proxy_settings(cx: &mut Context<HeadlessProject>) -> Option<Url> {
let proxy_str = ProxySettings::get_global(cx).proxy.to_owned();
proxy_str
.as_deref()
.map(str::trim)
.filter(|input| !input.is_empty())
.and_then(|input| {
input
.parse::<Url>()
.inspect_err(|e| log::error!("Error parsing proxy settings: {}", e))
.ok()
})
.or_else(read_proxy_from_env)
}
fn cleanup_old_binaries() -> Result<()> {
let server_dir = paths::remote_server_dir_relative();
let release_channel = release_channel::RELEASE_CHANNEL.dev_name();
let prefix = format!("zed-remote-server-{}-", release_channel);
for entry in std::fs::read_dir(server_dir.as_std_path())? {
let path = entry?.path();
if let Some(file_name) = path.file_name()
&& let Some(version) = file_name.to_string_lossy().strip_prefix(&prefix)
&& !is_new_version(version)
&& !is_file_in_use(file_name)
{
log::info!("removing old remote server binary: {:?}", path);
std::fs::remove_file(&path)?;
}
}
Ok(())
}
// Remove this once 223 goes stable, we only have this to clean up old binaries on WSL
// we no longer download them into this folder, we use the same folder as other remote servers
fn cleanup_old_binaries_wsl() {
let server_dir = paths::remote_wsl_server_dir_relative();
if let Ok(()) = std::fs::remove_dir_all(server_dir.as_std_path()) {
log::info!("removing old wsl remote server folder: {:?}", server_dir);
}
}
fn is_new_version(version: &str) -> bool {
semver::Version::from_str(version)
.ok()
.zip(semver::Version::from_str(env!("ZED_PKG_VERSION")).ok())
.is_some_and(|(version, current_version)| version >= current_version)
}
fn is_file_in_use(file_name: &OsStr) -> bool {
let info = sysinfo::System::new_with_specifics(sysinfo::RefreshKind::nothing().with_processes(
sysinfo::ProcessRefreshKind::nothing().with_exe(sysinfo::UpdateKind::Always),
));
for process in info.processes().values() {
if process
.exe()
.is_some_and(|exe| exe.file_name().is_some_and(|name| name == file_name))
{
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
mod unix {
use super::*;
use gpui::TestAppContext;
use proto::EnvelopedMessage as _;
// Windows did not reproduce socket backpressure.
#[gpui::test]
async fn rpc_responds_when_stderr_is_blocked(cx: &mut TestAppContext) {
cx.executor().allow_parking();
let temp_dir = tempfile::tempdir().expect("create socket directory");
let stdin_path = temp_dir.path().join("stdin.sock");
let stdout_path = temp_dir.path().join("stdout.sock");
let stderr_path = temp_dir.path().join("stderr.sock");
let listeners =
ServerListeners::new(stdin_path.clone(), stdout_path.clone(), stderr_path.clone())
.expect("bind server sockets");
let (log_tx, log_rx) = async_channel::unbounded();
let handler = cx.new(|_| ());
let server = cx.update(|cx| start_server(listeners, log_rx, cx, false));
server.add_request_handler::<proto::Ping, _, _, _>(
handler.downgrade(),
|_, _, _| async { Ok(proto::Ack {}) },
);
select! {
_ = async {
let (mut stdin, mut stdout, mut stderr) = futures::future::try_join3(
UnixStream::connect(&stdin_path),
UnixStream::connect(&stdout_path),
UnixStream::connect(&stderr_path),
).await.expect("connect server sockets");
log_tx
.try_send(vec![b'x'; 16 * 1024 * 1024])
.expect("queue a log larger than the socket buffer");
stderr
.read_exact(&mut [0])
.await
.expect("wait until log forwarding starts");
let mut buffer = Vec::new();
write_message(
&mut stdin,
&mut buffer,
proto::Ping {}.into_envelope(1, None, None),
).await.expect("send ping");
loop {
let response = read_message(&mut stdout, &mut buffer)
.await
.expect("read server message");
if response.responding_to == Some(1) {
assert!(matches!(response.payload, Some(proto::envelope::Payload::Ack(_))));
break;
}
}
}.fuse() => {}
_ = cx.executor().timer(std::time::Duration::from_secs(2)).fuse() => {
panic!("RPC response blocked by an unread log stream");
}
}
}
#[gpui::test]
async fn ping_is_handled_while_stdout_is_blocked(cx: &mut TestAppContext) {
cx.executor().allow_parking();
let temp_dir = tempfile::tempdir().expect("create socket directory");
let stdin_path = temp_dir.path().join("stdin.sock");
let stdout_path = temp_dir.path().join("stdout.sock");
let stderr_path = temp_dir.path().join("stderr.sock");
let listeners =
ServerListeners::new(stdin_path.clone(), stdout_path.clone(), stderr_path.clone())
.expect("bind server sockets");
let (_log_tx, log_rx) = async_channel::unbounded();
let (ping_tx, mut ping_rx) = mpsc::unbounded();
let handler = cx.new(|_| ());
let server = cx.update(|cx| start_server(listeners, log_rx, cx, false));
server.add_request_handler::<proto::Ping, _, _, _>(
handler.downgrade(),
move |_, _, _| {
ping_tx.unbounded_send(()).expect("report handled ping");
async { Ok(proto::Ack {}) }
},
);
select! {
_ = async {
let (mut stdin, mut stdout, _stderr) = futures::future::try_join3(
UnixStream::connect(&stdin_path),
UnixStream::connect(&stdout_path),
UnixStream::connect(&stderr_path),
).await.expect("connect server sockets");
let mut buffer = Vec::new();
read_message(&mut stdout, &mut buffer)
.await
.expect("read server startup message");
server.send(proto::CreateBufferForPeer {
variant: Some(proto::create_buffer_for_peer::Variant::State(proto::BufferState {
base_text: "x".repeat(16 * 1024 * 1024),
..Default::default()
})),
..Default::default()
}).expect("queue a buffer larger than the socket buffer");
stdout.read_exact(&mut [0]).await.expect("wait until buffer forwarding starts");
write_message(
&mut stdin,
&mut buffer,
proto::Ping {}.into_envelope(1, None, None),
).await.expect("send ping");
ping_rx.next().await.expect("ping handled without draining stdout");
}.fuse() => {}
_ = cx.executor().timer(std::time::Duration::from_secs(2)).fuse() => {
panic!("Ping handling blocked by an unread stdout stream");
}
}
}
}
#[test]
fn rotated_remote_log_path_uses_numbered_log_suffix() {
assert_eq!(
rotated_log_path(Path::new("server-workspace-12.log")),
PathBuf::from("server-workspace-12.1.log")
);
}
#[test]
fn opening_remote_log_rotates_existing_oversized_log() {
let temp_dir = tempfile::tempdir().expect("create temp dir");
let log_path = temp_dir.path().join("server-workspace-12.log");
let rotated_path = temp_dir.path().join("server-workspace-12.1.log");
let existing_contents = vec![b'x'; REMOTE_SERVER_LOG_MAX_BYTES as usize];
std::fs::write(&log_path, &existing_contents).expect("write oversized log");
let _log_file = RotatingLogFile::open(&log_path).expect("open rotating log file");
assert_eq!(
std::fs::read(&rotated_path).expect("read rotated log"),
existing_contents
);
assert_eq!(
std::fs::metadata(&log_path)
.expect("active log metadata")
.len(),
0
);
}
#[test]
fn writing_remote_log_rotates_before_exceeding_size_limit() {
let temp_dir = tempfile::tempdir().expect("create temp dir");
let log_path = temp_dir.path().join("server-workspace-12.log");
let rotated_path = temp_dir.path().join("server-workspace-12.1.log");
let existing_contents = vec![b'x'; REMOTE_SERVER_LOG_MAX_BYTES as usize - 1];
let new_contents = b"yz";
std::fs::write(&log_path, &existing_contents).expect("write existing log contents");
let mut log_file = RotatingLogFile::open(&log_path).expect("open rotating log file");
log_file
.write_all(new_contents)
.expect("write log contents");
log_file.flush().expect("flush log file");
assert_eq!(
std::fs::read(&rotated_path).expect("read rotated log"),
existing_contents
);
assert_eq!(
std::fs::read(&log_path).expect("read active log"),
new_contents
);
}
#[cfg(unix)]
#[test]
fn script_candidates_never_start_with_a_dash() {
assert_eq!(
script_candidates("agent", Some(OsStr::new("/usr/bin:bin::-bin"))),
[
PathBuf::from("/usr/bin/agent"),
PathBuf::from("./bin/agent"),
PathBuf::from("./agent"),
PathBuf::from("./-bin/agent"),
]
);
assert_eq!(
script_candidates("-a", Some(OsStr::new("/opt/bin"))),
[PathBuf::from("/opt/bin/-a")]
);
assert_eq!(
script_candidates("-x/agent", Some(OsStr::new("/opt/bin"))),
[PathBuf::from("./-x/agent")]
);
assert_eq!(
script_candidates("/srv/agent", Some(OsStr::new("/opt/bin"))),
[PathBuf::from("/srv/agent")]
);
assert_eq!(
script_candidates("agent", Some(OsStr::from_bytes(b"/not-utf8-\xff"))),
[PathBuf::from(OsStr::from_bytes(b"/not-utf8-\xff/agent"))]
);
assert_eq!(
script_candidates("agent", None),
[
PathBuf::from("/usr/local/bin/agent"),
PathBuf::from("/bin/agent"),
PathBuf::from("/usr/bin/agent"),
]
);
}
#[cfg(unix)]
#[test]
fn script_fallback_runs_option_like_wrapper_instead_of_its_arguments() {
use std::cell::RefCell;
use std::os::unix::fs::PermissionsExt as _;
let directory = tempfile::tempdir().expect("create temp dir");
let missing_directory = directory.path().join("missing");
let binary_directory = directory.path().join("bin");
std::fs::create_dir(&binary_directory).expect("create bin dir");
let wrapper = binary_directory.join("-a");
std::fs::write(
&wrapper,
"printf 'WRAPPER_RAN|%s|%s|%s|%s\\n' \"$TOKEN\" \"$#\" \"$1\" \"$2\"\n",
)
.expect("write wrapper");
std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755))
.expect("chmod wrapper");
let descriptor = RemoteCommand {
program: String::from("-a"),
args: vec![
String::from("spoofed-argv0"),
String::from("/usr/bin/printf"),
String::from("OPTION_INTERPRETED"),
],
env: HashMap::from_iter([
(String::from("TOKEN"), String::from("controlled")),
(
String::from("PATH"),
format!(
"{}:{}",
missing_directory.display(),
binary_directory.display()
),
),
]),
working_dir: None,
};
let attempts = RefCell::new(Vec::new());
let shell_output = RefCell::new(None);
let error = exec_with_script_fallback(&descriptor, None, |command| {
let program = command.get_program().to_owned();
if program == "/bin/sh" {
let output = smol::block_on(smol::process::Command::from(command).output())
.expect("run /bin/sh");
*shell_output.borrow_mut() = Some(output);
return std::io::Error::from_raw_os_error(libc::EIO);
}
attempts.borrow_mut().push(program.clone());
if program == "-a" || program == wrapper.as_os_str() {
std::io::Error::from_raw_os_error(libc::ENOEXEC)
} else {
std::io::Error::from_raw_os_error(libc::ENOENT)
}
});
assert_eq!(error.raw_os_error(), Some(libc::EIO));
assert_eq!(
attempts.into_inner(),
[
OsString::from("-a"),
missing_directory.join("-a").into_os_string(),
wrapper.into_os_string(),
]
);
let shell_output = shell_output.into_inner().expect("shell fallback ran");
assert_eq!(
String::from_utf8_lossy(&shell_output.stderr),
"",
"{shell_output:?}"
);
assert_eq!(
String::from_utf8_lossy(&shell_output.stdout),
"WRAPPER_RAN|controlled|3|spoofed-argv0|/usr/bin/printf\n"
);
assert_eq!(shell_output.status.code(), Some(0));
let attempts = RefCell::new(0);
let error = exec_with_script_fallback(&descriptor, None, |_| {
*attempts.borrow_mut() += 1;
std::io::Error::from_raw_os_error(libc::EACCES)
});
assert_eq!(error.raw_os_error(), Some(libc::EACCES));
assert_eq!(attempts.into_inner(), 1);
let error = exec_with_script_fallback(&descriptor, None, |command| {
if command.get_program() == "-a" {
std::io::Error::from_raw_os_error(libc::ENOEXEC)
} else {
std::io::Error::from_raw_os_error(libc::ELOOP)
}
});
assert_eq!(error.raw_os_error(), Some(libc::ELOOP));
}
}