fix(wayland): no "select the screen" hint when the restore token skips the picker (#17319)

With a restore token saved, the portal answers without showing its
picker, yet every first-viewer login still told the peer "Please Select
the screen to be shared(Operate on the peer side)". CAP_DISPLAY_INFO is
cleared when the last viewer leaves, so is_inited() reports "not
inited" on each new connection.

When the server process has a token saved, the login now holds the
message back for 3 s and drops it if the display refresh (portal request
and capturer setup) finishes first. If the portal turns the token down
and shows its picker, the message goes out after 3 s. Without a token
the message is sent at once, as before.

Fixes #17317


Claude-Session: https://claude.ai/code/session_01JAxkkig1bcC5Xrr1uwX47b

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RustDeskandClaude Opus 5.5 authored and GitHub committed 2026-11-30 15:31:34 +08:00
1 parent 6380d8c7a3
commit b250103ff5
4 files changed
+103 -4

No files matched your search

+4
View File
@@ -1053,6 +1053,10 @@ const RESTORE_TOKEN: &str = "restore_token";
const RESTORE_TOKEN_CONF_KEY: &str = "wayland-restore-token";
const PIPEWIRE_DISPLAY_OFFSET_CONF_KEY: &str = "wayland-pipewire-display-offset";
pub fn has_restore_token() -> bool {
!config::LocalConfig::get_option(RESTORE_TOKEN_CONF_KEY).is_empty()
}
pub fn get_available_cursor_modes() -> Result<u32, dbus::Error> {
let conn = SyncConnection::new_session()?;
let portal = get_portal(&conn);
+11 -4
View File
@@ -2199,13 +2199,20 @@ impl Connection {
self.last_supported_encoding = Some(supported_encoding.clone());
log::info!("peer info supported_encoding: {:?}", supported_encoding);
pi.encoding = Some(supported_encoding).into();
if let Some(msg_out) = super::display_service::is_inited_msg() {
self.send(msg_out).await;
}
let select_screen_msg = super::display_service::is_inited_msg();
try_activate_screen();
match super::display_service::update_get_sync_displays_on_login().await {
let displays_on_login = match select_screen_msg {
Some(msg_out) => {
super::display_service::update_get_sync_displays_on_login_with_msg(
self.send(msg_out),
)
.await
}
None => super::display_service::update_get_sync_displays_on_login().await,
};
match displays_on_login {
Err(err) => {
res.set_error(format!("{}", err));
}
+73
View File
@@ -895,6 +895,38 @@ pub fn is_inited_msg() -> Option<Message> {
None
}
/// `update_get_sync_displays_on_login` for a login that owes the peer `is_inited_msg`'s message;
/// `send_msg` delivers it, at once or, on Wayland with a restore token saved, only if the refresh
/// is still waiting for the capture after `is_inited_msg_delay_ms`.
pub async fn update_get_sync_displays_on_login_with_msg(
send_msg: impl std::future::Future<Output = ()>,
) -> ResultType<(Vec<DisplayInfo>, usize)> {
#[cfg(target_os = "linux")]
let delay_ms = super::wayland::is_inited_msg_delay_ms();
#[cfg(not(target_os = "linux"))]
let delay_ms = 0;
late_unless_done(update_get_sync_displays_on_login(), delay_ms, send_msg).await
}
async fn late_unless_done<T>(
work: impl std::future::Future<Output = T>,
delay_ms: u64,
late: impl std::future::Future<Output = ()>,
) -> T {
if delay_ms == 0 {
late.await;
return work.await;
}
tokio::pin!(work);
match timeout(delay_ms, &mut work).await {
Ok(res) => res,
Err(_) => {
late.await;
work.await
}
}
}
// Return the primary index with the refreshed list so login cannot mix display snapshots.
pub async fn update_get_sync_displays_on_login() -> ResultType<(Vec<DisplayInfo>, usize)> {
#[cfg(target_os = "linux")]
@@ -1029,6 +1061,47 @@ pub fn try_get_displays_(add_amyuni_headless: bool) -> ResultType<Vec<Display>>
Ok(displays)
}
#[cfg(test)]
mod login_msg_tests {
use super::late_unless_done;
use hbb_common::tokio;
use std::sync::atomic::{AtomicBool, Ordering};
// The restore token case: the capture comes up without anyone selecting a screen, so the peer
// is not told to go and select one.
#[tokio::test]
async fn the_message_is_dropped_when_the_work_ends_within_the_delay() {
let sent = AtomicBool::new(false);
let res = late_unless_done(async { 7 }, 60_000, async {
sent.store(true, Ordering::SeqCst);
})
.await;
assert_eq!(res, 7);
assert!(!sent.load(Ordering::SeqCst));
}
// The portal turned the token down and waits at its picker: the peer still gets the message.
#[tokio::test]
async fn the_message_goes_out_when_the_work_outlasts_the_delay() {
let (answer, picker) = tokio::sync::oneshot::channel::<()>();
let sent = AtomicBool::new(false);
let res = late_unless_done(
async {
picker.await.ok();
7
},
10,
async {
sent.store(true, Ordering::SeqCst);
answer.send(()).ok();
},
)
.await;
assert_eq!(res, 7);
assert!(sent.load(Ordering::SeqCst));
}
}
#[cfg(all(test, target_os = "linux", feature = "drm"))]
mod input_map_tests {
use super::run_uinput_apply;
+15
View File
@@ -425,6 +425,21 @@ pub(super) fn is_inited() -> Option<Message> {
}
}
// With a restore token saved the portal normally answers without showing its picker, and
// `is_inited`'s message would send the peer to select a screen nobody is asked to select. The
// login holds the message back this long and drops it if the capture is up by then. A portal that
// turned the token down is still showing its picker, and the message goes out.
const SELECT_SCREEN_MSG_DELAY_MS: u64 = 3_000;
pub(super) fn is_inited_msg_delay_ms() -> u64 {
// Only the server process hands the token to the portal, see scrap's `is_server_running`.
if crate::is_server() && scrap::wayland::pipewire::has_restore_token() {
SELECT_SCREEN_MSG_DELAY_MS
} else {
0
}
}
pub(super) async fn check_init() -> ResultType<()> {
if !is_x11() {
// try_read for the same reason as is_inited; when busy, the same look waits off the worker.