Fix memory leak in nvim-core by sweeping timed-out pending requests

This commit is contained in:
Riz Ashraf committed 2026-09-21 04:38:41 +01:00
1 parent 9ce5c84d9c
commit 2191ea5ce7
1 file changed
+15 -3
+15 -3
View File
@@ -203,11 +203,9 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
} }
continue; continue;
} }
Err(_) => { Err(rmpv::decode::Error::InvalidMarkerRead(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
if offset > 0 {
resp_buf.drain(..offset); resp_buf.drain(..offset);
offset = 0; offset = 0;
}
let read_future = read_half.read(&mut chunk); let read_future = read_half.read(&mut chunk);
match tokio::time::timeout(tokio::time::Duration::from_secs(60), read_future).await { match tokio::time::timeout(tokio::time::Duration::from_secs(60), read_future).await {
@@ -220,6 +218,10 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
} }
} }
} }
Err(_) => {
tracing::error!("Failed to parse message from Neovim socket");
break;
}
} }
} }
@@ -230,6 +232,16 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
} }
}); });
// Cleanup task for timed-out requests
let pending_clone3 = Arc::clone(&pending_requests);
tokio::spawn(async move {
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(10));
loop {
interval.tick().await;
pending_clone3.lock().await.retain(|_, sender| !sender.is_closed());
}
});
let mut conn_lock = NVIM_CONN.lock().await; let mut conn_lock = NVIM_CONN.lock().await;
*conn_lock = Some(tx.clone()); *conn_lock = Some(tx.clone());
Ok(tx) Ok(tx)