Skip to content

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.

fix(ssh): close sftp handles before the sftp session goes

RoversXedb86319250d
diff --git a/wezterm-ssh/src/sessioninner.rs b/wezterm-ssh/src/sessioninner.rs
index 1d568d2a..1316078e 100644
--- a/wezterm-ssh/src/sessioninner.rs
+++ b/wezterm-ssh/src/sessioninner.rs
@@ -258,7 +258,9 @@ impl SessionInner {
}
sess.set_blocking(false);
let mut sess = SessionWrap::with_libssh(sess);
- self.request_loop(&mut sess)
+ let result = self.request_loop(&mut sess);
+ self.close_sftp_handles(&mut sess);
+ result
}
#[cfg(feature = "ssh2")]
@@ -322,7 +324,9 @@ impl SessionInner {
sess.set_blocking(false);
let mut sess = SessionWrap::with_ssh2(sess);
- self.request_loop(&mut sess)
+ let result = self.request_loop(&mut sess);
+ self.close_sftp_handles(&mut sess);
+ result
}
/// Explicitly and directly connect to the requested host because
@@ -1232,6 +1236,16 @@ impl SessionInner {
}
}
+ /// Closes the SFTP files and directories still open as the loop ends,
+ /// while `sess` still holds the SFTP session they were opened on: libssh
+ /// closes one through that session, and it goes with `sess`. Blocking, as
+ /// a close the loop is asked for is.
+ fn close_sftp_handles(&mut self, sess: &mut SessionWrap) {
+ sess.set_blocking(true);
+ self.files.clear();
+ self.dirs.clear();
+ }
+
pub fn identity_agent(&self) -> Option<String> {
self.config
.get("identityagent")
diff --git a/wezterm-ssh/tests/e2e/sftp/file.rs b/wezterm-ssh/tests/e2e/sftp/file.rs
index d59c1787..70d358bc 100644
--- a/wezterm-ssh/tests/e2e/sftp/file.rs
+++ b/wezterm-ssh/tests/e2e/sftp/file.rs
@@ -215,3 +215,40 @@ fn should_support_async_close(#[future] session: SessionWithSshd) {
.expect("Failed to close file second time");
})
}
+
+#[rstest]
+#[cfg_attr(not(any(target_os = "macos", target_os = "linux")), ignore)]
+fn dir_left_open_should_be_closed_before_the_session_ends(#[future] session: SessionWithSshd) {
+ if !sshd_available() {
+ return;
+ }
+ // libssh closes an sftp handle through the sftp session it came from,
+ // which goes when the session loop ends: one closed after that reads
+ // freed memory. MallocScribble=1 (macOS) or MALLOC_PERTURB_=85 (glibc)
+ // makes such a read crash every time.
+ smol::block_on(async {
+ let session: SessionWithSshd = session.await;
+
+ let temp = TempDir::new().unwrap();
+ let sftp = session.sftp();
+ let remote_dir = sftp
+ .open_dir(temp.path().to_path_buf())
+ .await
+ .expect("Failed to open remote directory");
+ remote_dir
+ .read_dir()
+ .await
+ .expect("Failed to read remote directory");
+
+ // Any clone of a session tells the loop, as it goes, that the session
+ // was dropped: with no channel open, the loop ends with the
+ // directory still open.
+ drop(session.clone());
+ smol::Timer::after(std::time::Duration::from_secs(1)).await;
+ assert!(
+ sftp.symlink_metadata(temp.path().to_path_buf()).await.is_err(),
+ "the session should have ended"
+ );
+ drop(remote_dir);
+ })
+}