From 9fd7ffa6f0357226bd45d89719abe0c2677fbaaa Mon Sep 17 00:00:00 2001 From: hsxian <940100027@qq.com> Date: Fri, 20 Nov 2026 09:43:10 +0800 Subject: [PATCH] fix(portal): bound the recovery-lock wait and log lock transitions and reopens A desktop connection that starts while the session is locked stays on the synthetic locked video until the auto unlock, and only the maintain loop reacts to that unlock. This morning a chain of such connections produced no maintain activity at all: no reopen log, no recovery-deferred log, no outcome - the viewer sat on black frames until its video timeout. Three blameless suspects can all go silent the same way: an attempt cancelled by backoff or supersession, the opening gate held by a zombie worker, or recover() spinning forever on a recovery lock another opener never released. - recover(): bound the try_lock wait to 15s and return code 3, so a wedged holder surfaces as 'recovery deferred' instead of an attempt that never ends. - maintain(): log each lock-state transition the loop reacts to, and log when it stops because a newer connection superseded it. - portal_attempt(): log every real reopen so a hang after this line pins the stall to the blocking open/recovery worker. --- src/desktop_session.rs | 16 ++++++++++++++-- src/portal.rs | 8 ++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/desktop_session.rs b/src/desktop_session.rs index 70883a7..dc45a00 100644 --- a/src/desktop_session.rs +++ b/src/desktop_session.rs @@ -281,7 +281,10 @@ impl PortalDesktop { // that locking or a failed initialization releases the inhibition. let mut _inhibitor: Option = None; loop { - if !owner.is_current() { return Ok(()); } + if !owner.is_current() { + log::info!("Portal maintain stopped: superseded by a newer desktop connection"); + return Ok(()); + } let state = *locked.borrow_and_update(); if state == Some(false) { defer_until_unlock = false; } if state != Some(false) && previous == Some(Some(false)) { self.pause(&owner); } @@ -294,7 +297,13 @@ impl PortalDesktop { if attempt_allowed(state, allow_locked) && !defer_until_unlock { // A fresh transition into an attempted state gets an immediate wake/recovery attempt. let force_wake = fresh_state(previous, state); - if force_wake { retry_at = Instant::now(); } + if force_wake { + retry_at = Instant::now(); + // One line per lock-state transition: connects that start locked only become + // picture-capable after the auto unlock, and this is the marker that the + // supervisor actually reacted to the unlock (its absence means the loop is dead). + log::info!("Portal desktop lock transition: state={state:?} defer={defer_until_unlock}"); + } let mut attempt_locked = locked.clone(); let outcome = tokio::select! { biased; @@ -394,6 +403,9 @@ impl PortalDesktop { // A new controller can arrive while the cached Portal handle is alive // but the display has gone to sleep without a lock transition. if healthy { return AttemptOutcome::Ready(screen, inhibitor); } + // Marks every real reopen: paired with the lock-transition log, the absence of an + // outcome after this line pins a stall to the blocking open/recovery worker below. + log::info!("Portal attempt: reopening the capture session (force_wake={force_wake})"); let worker_cancel = cancel.clone(); let result = tokio::task::spawn_blocking(move || { // The worker owns the gate until its real cleanup completes; a diff --git a/src/portal.rs b/src/portal.rs index 363e97a..a7d7528 100644 --- a/src/portal.rs +++ b/src/portal.rs @@ -547,11 +547,19 @@ mod native { use std::sync::atomic::Ordering; if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); } // Serialize with the encoder's stream-loss recovery: two openers would ask for consent twice. + // The wait is bounded: another opener (encoder reopen, consent rebuild) can hold this lock + // for as long as a consent dialog waits, and an unbounded spin here would leave the desktop + // supervisor's attempt silently running with no log and no frames until the viewer times out. + let lock_wait_deadline = std::time::Instant::now() + std::time::Duration::from_secs(15); let mut recovery = loop { match self.recovery.try_lock() { Ok(recovery) => break recovery, Err(TryLockError::WouldBlock) => { if cancelled.load(Ordering::Acquire) { return Err(super::PortalError { code: 13 }); } + if std::time::Instant::now() >= lock_wait_deadline { + log::warn!("Portal recovery lock held by another opener for 15s; retrying later"); + return Err(super::PortalError { code: 3 }); + } std::thread::sleep(std::time::Duration::from_millis(20)); } Err(TryLockError::Poisoned(_)) => return Err(super::PortalError { code: 3 }),