`tokio::fs::File` routes every `.read()` through `spawn_blocking`. On an idle FIFO opened r+w, that thread blocks in `read()` forever — and when the test runtime tries to shut down, the blocking-pool thread is stuck on the syscall and the test binary hangs (which is exactly what just took out CI run 11). Switch to `std::fs::File` opened `O_RDWR | O_NONBLOCK` wrapped in `tokio::io::unix::AsyncFd`. Reads now drive off epoll readiness with `try_io`; no blocking-pool threads, no hung shutdown. `Drop` still aborts the task so per-test cleanup is clean.
This commit is contained in:
@@ -4,17 +4,28 @@
|
||||
//! The daemon reads from that FIFO and broadcasts the bytes to all attached
|
||||
//! WS clients via a [`tokio::sync::broadcast`] channel.
|
||||
//!
|
||||
//! The reader opens the FIFO with `O_RDWR` so the file's writer count never
|
||||
//! drops to zero from our side — that keeps `read()` from returning EOF when
|
||||
//! tmux's `cat` exits (e.g., during a pipe-pane re-issue or a brief tmux hiccup).
|
||||
//! Implementation notes:
|
||||
//!
|
||||
//! - The FIFO is opened `O_RDWR | O_NONBLOCK`. `O_RDWR` keeps the file's
|
||||
//! writer count non-zero from our side, so `read()` never returns EOF when
|
||||
//! tmux's `cat` exits (e.g., during a pipe-pane re-issue or a brief tmux
|
||||
//! hiccup). `O_NONBLOCK` is essential: without it, `tokio::fs::File`'s
|
||||
//! reads route through `spawn_blocking` and block one of those threads in
|
||||
//! `read()` until data arrives — which never happens for idle sessions.
|
||||
//! A pile of such threads also prevents the tokio runtime from shutting
|
||||
//! down cleanly between tests.
|
||||
//! - We wrap the non-blocking file descriptor in [`AsyncFd`] and use
|
||||
//! `try_io` to read directly off the FD, leaving the blocking pool alone.
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::io;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use tokio::fs::OpenOptions;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::unix::AsyncFd;
|
||||
use tokio::io::Interest;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
@@ -28,7 +39,8 @@ pub const PTY_BROADCAST_CAPACITY: usize = 256;
|
||||
const READ_BUF_BYTES: usize = 4096;
|
||||
|
||||
/// Owns a FIFO + the reader task that pumps bytes into a broadcast channel.
|
||||
/// Dropping the bridge aborts the reader and closes the channel.
|
||||
/// Dropping the bridge aborts the reader and closes the channel; the FIFO
|
||||
/// file is left on disk for `SessionManager::kill` to remove.
|
||||
#[derive(Debug)]
|
||||
pub struct PtyBridge {
|
||||
fifo_path: PathBuf,
|
||||
@@ -37,17 +49,25 @@ pub struct PtyBridge {
|
||||
}
|
||||
|
||||
impl PtyBridge {
|
||||
/// Create the FIFO (if it doesn't exist) and start the reader task.
|
||||
/// Create the FIFO (if it doesn't exist), open it non-blocking, and start
|
||||
/// the reader task.
|
||||
pub async fn start(fifo_path: PathBuf) -> io::Result<Self> {
|
||||
if let Some(parent) = fifo_path.parent() {
|
||||
tokio::fs::create_dir_all(parent).await?;
|
||||
}
|
||||
mkfifo(&fifo_path)?;
|
||||
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.custom_flags(libc::O_NONBLOCK)
|
||||
.open(&fifo_path)?;
|
||||
let async_fd = AsyncFd::with_interest(file, Interest::READABLE)?;
|
||||
|
||||
let (sender, _) = broadcast::channel::<Vec<u8>>(PTY_BROADCAST_CAPACITY);
|
||||
let tx = sender.clone();
|
||||
let path_for_task = fifo_path.clone();
|
||||
let task = tokio::spawn(async move {
|
||||
run_reader(path_for_task, tx).await;
|
||||
run_reader(async_fd, tx).await;
|
||||
});
|
||||
Ok(Self {
|
||||
fifo_path,
|
||||
@@ -68,34 +88,42 @@ impl PtyBridge {
|
||||
impl Drop for PtyBridge {
|
||||
fn drop(&mut self) {
|
||||
self.task.abort();
|
||||
// FIFO file left on disk so a restart can re-pipe into it; the
|
||||
// SessionManager removes it explicitly on KillSession.
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_reader(path: PathBuf, tx: broadcast::Sender<Vec<u8>>) {
|
||||
let mut file = match OpenOptions::new().read(true).write(true).open(&path).await {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
tracing::error!(error = ?e, fifo = %path.display(), "failed to open fifo");
|
||||
return;
|
||||
}
|
||||
};
|
||||
async fn run_reader(async_fd: AsyncFd<File>, tx: broadcast::Sender<Vec<u8>>) {
|
||||
let mut buf = [0u8; READ_BUF_BYTES];
|
||||
loop {
|
||||
match file.read(&mut buf).await {
|
||||
Ok(0) => {
|
||||
// Shouldn't normally happen because we hold the write end too.
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
let mut guard = match async_fd.readable().await {
|
||||
Ok(g) => g,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = ?e, "fifo readability wait failed");
|
||||
return;
|
||||
}
|
||||
Ok(n) => {
|
||||
// ignore SendError when there are no subscribers
|
||||
};
|
||||
// `&File: Read`, so we can read through the immutable ref the AsyncFd
|
||||
// hands us inside try_io.
|
||||
let result = guard.try_io(|inner| {
|
||||
let mut file: &File = inner.get_ref();
|
||||
file.read(&mut buf)
|
||||
});
|
||||
match result {
|
||||
Ok(Ok(0)) => {
|
||||
// 0 bytes despite holding a write fd; just yield and retry.
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
Ok(Ok(n)) => {
|
||||
let _ = tx.send(buf[..n].to_vec());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(error = ?e, "pty fifo read error");
|
||||
Ok(Err(e)) => {
|
||||
tracing::warn!(error = ?e, "fifo read error");
|
||||
return;
|
||||
}
|
||||
Err(_would_block) => {
|
||||
// AsyncFd reported readable but read() said WouldBlock.
|
||||
// Clear readiness so we wait again. try_io did that for us.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,7 +145,7 @@ fn mkfifo(path: &Path) -> io::Result<()> {
|
||||
}
|
||||
|
||||
/// The shell command tmux's `pipe-pane` will run to forward pane output into
|
||||
/// `fifo_path`. `cat` here writes raw bytes through unchanged.
|
||||
/// `fifo_path`. `cat` writes raw bytes through unchanged.
|
||||
pub fn pipe_pane_command(fifo_path: &Path) -> String {
|
||||
format!("cat >> {}", shell_quote(&fifo_path.display().to_string()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user