Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix Zed OOM-ing when macOS file descriptors become invalid (#45669)
Kirill Bulatova50c5b2c10fe
diff --git a/crates/fs/src/fs.rs b/crates/fs/src/fs.rsindex 3b2f318119f..61dd521cf79 100644--- a/crates/fs/src/fs.rs+++ b/crates/fs/src/fs.rs@@ -335,12 +335,11 @@ impl FileHandle for std::fs::File {let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };- if result == -1 {- anyhow::bail!("fcntl returned -1".to_string());- }+ anyhow::ensure!(result != -1, "fcntl returned -1");// SAFETY: `fcntl` will initialize the path buffer.let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };+ anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));Ok(path)}@@ -372,12 +371,11 @@ impl FileHandle for std::fs::File {kif.kf_structsize = libc::KINFO_FILE_SIZE;let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_KINFO, kif.as_mut_ptr()) };- if result == -1 {- anyhow::bail!("fcntl returned -1".to_string());- }+ anyhow::ensure!(result != -1, "fcntl returned -1");// SAFETY: `fcntl` will initialize the kif.let c_str = unsafe { CStr::from_ptr(kif.assume_init().kf_path.as_ptr()) };+ anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));Ok(path)}@@ -398,18 +396,21 @@ impl FileHandle for std::fs::File {// Query required buffer size (in wide chars)let required_len =unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };- if required_len == 0 {- anyhow::bail!("GetFinalPathNameByHandleW returned 0 length");- }+ anyhow::ensure!(+ required_len != 0,+ "GetFinalPathNameByHandleW returned 0 length"+ );// Allocate buffer and retrieve the pathlet mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };- if written == 0 {- anyhow::bail!("GetFinalPathNameByHandleW failed to write path");- }+ anyhow::ensure!(+ written != 0,+ "GetFinalPathNameByHandleW failed to write path"+ );let os_str: OsString = OsString::from_wide(&buf[..written as usize]);+ anyhow::ensure!(!os_str.is_empty(), "Could find a path for the file handle");Ok(PathBuf::from(os_str))}}diff --git a/crates/fsevent/src/fsevent.rs b/crates/fsevent/src/fsevent.rsindex 8af57c19ee2..0fe3592010f 100644--- a/crates/fsevent/src/fsevent.rs+++ b/crates/fsevent/src/fsevent.rs@@ -76,7 +76,7 @@ impl EventStream {cf::CFRelease(cf_path);cf::CFRelease(cf_url);} else {- log::error!("Failed to create CFURL for path: {}", path.display());+ log::error!("Failed to create CFURL for path: {path:?}");}}diff --git a/crates/project/src/environment.rs b/crates/project/src/environment.rsindex c4e807621ee..0d590d6adcb 100644--- a/crates/project/src/environment.rs+++ b/crates/project/src/environment.rs@@ -216,7 +216,7 @@ impl ProjectEnvironment {let shell = shell.clone();let tx = self.environment_error_messages_tx.clone();cx.spawn(async move |cx| {- let mut shell_env = cx+ let mut shell_env = match cx.background_spawn(load_directory_shell_environment(shell,abs_path.clone(),@@ -224,7 +224,15 @@ impl ProjectEnvironment {tx,)).await- .log_err();+ {+ Ok(shell_env) => Some(shell_env),+ Err(e) => {+ log::error!(+ "Failed to load shell environment for directory {abs_path:?}: {e:#}"+ );+ None+ }+ };if let Some(shell_env) = shell_env.as_mut() {let path = shell_envdiff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rsindex 43f63d90789..3f650d98c13 100644--- a/crates/project_panel/src/project_panel.rs+++ b/crates/project_panel/src/project_panel.rs@@ -3599,6 +3599,7 @@ impl ProjectPanel {== worktree_snapshot.root_entry(){let Some(path_name) = worktree_abs_path.file_name() else {+ entry_iter.advance();continue;};let depth = 0;diff --git a/crates/worktree/src/worktree.rs b/crates/worktree/src/worktree.rsindex ca5500249e9..7d682c03215 100644--- a/crates/worktree/src/worktree.rs+++ b/crates/worktree/src/worktree.rs@@ -3995,21 +3995,27 @@ impl BackgroundScanner {.snapshot.root_file_handle.clone()- .and_then(|handle| handle.current_path(&self.fs).log_err())+ .and_then(|handle| match handle.current_path(&self.fs) {+ Ok(new_path) => Some(new_path),+ Err(e) => {+ log::error!("Failed to refresh worktree root path: {e:#}");+ None+ }+ }).map(|path| SanitizedPath::new_arc(&path)).filter(|new_path| *new_path != root_path);if let Some(new_path) = new_path {log::info!(- "root renamed from {} to {}",- root_path.as_path().display(),- new_path.as_path().display()+ "root renamed from {:?} to {:?}",+ root_path.as_path(),+ new_path.as_path(),);self.status_updates_tx.unbounded_send(ScanState::RootUpdated { new_path }).ok();} else {- log::warn!("root path could not be canonicalized: {:#}", err);+ log::error!("root path could not be canonicalized: {err:#}");}return;}@@ -4457,7 +4463,7 @@ impl BackgroundScanner {Ok(Some(metadata)) => metadata,Ok(None) => continue,Err(err) => {- log::error!("error processing {child_abs_path:?}: {err:?}");+ log::error!("error processing {child_abs_path:?}: {err:#}");continue;}};