From 98a7df06cb8fe618f37533059ae3a3934e64ba9d Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 24 Sep 2026 21:19:13 +0800 Subject: [PATCH] fix(clipboard): keep a file list while it is being read, throttle read errors A unix paste side fetches every new file list as soon as the host copies, so each copy retires the previous list, and four copies during one long transfer evicted the list it was still reading. A kept list that serves a read now becomes the most recent (unix: back of the retired queue, Windows: last_served), so the next eviction takes a list nobody is reading. A failed file-contents read logged an error per request, and peers can now trigger new ones (an unknown list id, an over-limit range). Log it through throttled_log! instead. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../clipboard/src/platform/unix/serv_files.rs | 58 +++++++++++++++---- libs/clipboard/src/windows/wf_cliprdr.c | 2 + src/clipboard_file.rs | 7 ++- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/libs/clipboard/src/platform/unix/serv_files.rs b/libs/clipboard/src/platform/unix/serv_files.rs index 61024aca8..2afda56aa 100644 --- a/libs/clipboard/src/platform/unix/serv_files.rs +++ b/libs/clipboard/src/platform/unix/serv_files.rs @@ -324,19 +324,24 @@ fn select_clip_files<'a>( conn_id: i32, clip_data_id: Option, ) -> Option<&'a mut ClipFiles> { - match clip_data_id { - Some(id) if current.id == id => Some(current), + let pos = match clip_data_id { + Some(id) if current.id == id => return Some(current), Some(id) => retired - .iter_mut() - .rev() - .find(|files| files.id == id && files.served_to.contains(&conn_id)), - None if current.served_to.contains(&conn_id) => Some(current), - None => retired - .iter_mut() - .rev() - .find(|files| files.served_to.contains(&conn_id)) - .or(Some(current)), - } + .iter() + .rposition(|files| files.id == id && files.served_to.contains(&conn_id))?, + None if current.served_to.contains(&conn_id) => return Some(current), + None => match retired + .iter() + .rposition(|files| files.served_to.contains(&conn_id)) + { + Some(pos) => pos, + None => return Some(current), + }, + }; + // A list being read moves to the back, so it is not the next one evicted. + let files = retired.remove(pos)?; + retired.push_back(files); + retired.back_mut() } // Keep a list a peer was sent, so its streams do not read the new copy at the same indexes. @@ -836,4 +841,33 @@ mod sig_test { clear_files(); } + + #[test] + fn a_list_being_read_outlives_newer_copies() { + let tmp = TmpDir::new("read_keeps_list"); + let files: Vec = (0..=MAX_RETIRED_CLIP_FILES + 1) + .map(|i| { + let file = tmp.join(&format!("{i}.bin")); + fs::write(&file, format!("{i}{i}{i}{i}{i}{i}{i}{i}")).unwrap(); + file + }) + .collect(); + + let _guard = lock_clip_files(); + clear_files(); + sync_files(&[path_str(&files[0])]).unwrap(); + let id = file_list_id(&get_file_list_pdu(1)); + let idx = CLIP_FILES.lock().first_file_index as i32; + let read = || range_data(read_file_contents(1, 7, idx, 0x2, 0, 0, 4, Some(id))); + + // A unix peer fetches every new list as soon as it is copied, so each copy retires the + // one before it. Reads in between keep the list being transferred from being evicted. + for file in &files[1..] { + sync_files(&[path_str(file)]).unwrap(); + get_file_list_pdu(2); + assert_eq!(read(), b"0000"); + } + + clear_files(); + } } diff --git a/libs/clipboard/src/windows/wf_cliprdr.c b/libs/clipboard/src/windows/wf_cliprdr.c index 2cd099545..96cb14098 100644 --- a/libs/clipboard/src/windows/wf_cliprdr.c +++ b/libs/clipboard/src/windows/wf_cliprdr.c @@ -2671,6 +2671,8 @@ static BOOL wf_cliprdr_read_served_file_list(wfClipboard *clipboard, } if (!list || request->listIndex >= list->nFiles) return FALSE; + // A list being read is not the next one evicted. + list->last_served = ++clipboard->served_file_list_seq; if (request->dwFlags == FILECONTENTS_SIZE) { diff --git a/src/clipboard_file.rs b/src/clipboard_file.rs index 6a05e7e1c..8b746051b 100644 --- a/src/clipboard_file.rs +++ b/src/clipboard_file.rs @@ -383,7 +383,12 @@ pub mod unix_file_clip { .map(|res| match res { Ok(data) => clip_2_msg(data), Err(e) => { - log::error!("failed to read file contents: {:?}", e); + hbb_common::throttled_log!( + std::time::Duration::from_secs(5), + error, + "failed to read file contents: {:?}", + e + ); resp_file_contents_fail(stream_id) } })