[Fix] (Server): a lost session's window lease is waited for by the fresh session, and a window wait holds no backend turn.

This commit is contained in:
2026-10-03 09:05:54 -04:00
parent f52a546707
commit f22ae625cb
10 changed files with 1005 additions and 14 deletions
+40 -4
View File
@@ -204,7 +204,8 @@ namespace MobileGL::MG_Remote::Server {
{
const std::lock_guard<std::mutex> lock(m_mutex);
if (!m_installed) return ServerWindowAcquire::NoDisplay;
if (m_leaseHolder != nullptr && m_leaseHolder != holder) return ServerWindowAcquire::LeasedElsewhere;
if (m_leaseHolder != nullptr && m_leaseHolder != holder && !m_leaseEnding)
return ServerWindowAcquire::LeasedElsewhere;
hooks = m_hooks;
interrupts = m_interrupts;
previousWidth = m_requestedWidth;
@@ -231,14 +232,27 @@ namespace MobileGL::MG_Remote::Server {
std::unique_lock<std::mutex> lock(m_mutex);
const auto deadline = Clock::now() + std::chrono::milliseconds(timeoutMs);
Bool attachedSeen = false;
Bool waitedForEndingHolder = false;
auto geometryDeadline = deadline;
for (;;) {
if (!m_installed) return ServerWindowAcquire::NoDisplay;
if (m_interrupts != interrupts) return ServerWindowAcquire::Interrupted;
if (cancel != nullptr && cancel(cancelUser)) return ServerWindowAcquire::Cancelled;
if (m_leaseHolder != nullptr && m_leaseHolder != holder) return ServerWindowAcquire::LeasedElsewhere;
// ANOTHER HOLDER: refused at once while its session goes on, waited for while it ends
// (its EndLease wakes this). The window is judged only once the lease is free.
const Bool heldElsewhere = m_leaseHolder != nullptr && m_leaseHolder != holder;
if (heldElsewhere && !m_leaseEnding) return ServerWindowAcquire::LeasedElsewhere;
if (heldElsewhere && !waitedForEndingHolder) {
waitedForEndingHolder = true;
MGLOG_I("MG_Remote server: the server window is still leased by a session that is ending; this "
"session waits for that lease to end (at most %u ms)",
timeoutMs);
}
const auto now = Clock::now();
if (m_window != nullptr) {
if (heldElsewhere) {
attachedSeen = false;
geometryDeadline = deadline;
} else if (m_window != nullptr) {
if (!attachedSeen) {
attachedSeen = true;
geometryDeadline = std::min(deadline, now + std::chrono::milliseconds(kGeometryGraceMs));
@@ -249,6 +263,7 @@ namespace MobileGL::MG_Remote::Server {
m_onLost = onLost;
m_onResized = onResized;
m_lostRequested = false;
m_leaseEnding = false;
// A suspended holder that leased a window again waits for no other.
m_suspended.erase(std::remove_if(m_suspended.begin(), m_suspended.end(),
[holder](const SuspendedHolder& entry) {
@@ -273,13 +288,18 @@ namespace MobileGL::MG_Remote::Server {
"at that size and the client is told so",
m_width, m_height, kGeometryGraceMs);
}
if (waitedForEndingHolder) {
MGLOG_I("MG_Remote server: the ending session's lease ended; the server window is leased to "
"this session");
}
return ServerWindowAcquire::Acquired;
}
} else {
attachedSeen = false;
geometryDeadline = deadline;
}
if (now >= deadline) return ServerWindowAcquire::NoWindow;
if (now >= deadline)
return heldElsewhere ? ServerWindowAcquire::LeasedElsewhere : ServerWindowAcquire::NoWindow;
const auto wake = std::min({deadline, geometryDeadline, now + kCancelPollInterval});
m_cv.wait_until(lock, wake);
}
@@ -291,6 +311,7 @@ namespace MobileGL::MG_Remote::Server {
m_onLost = nullptr;
m_onResized = nullptr;
m_lostRequested = false;
m_leaseEnding = false;
releases->swap(m_releaseAfterLease);
*hooks = m_hooks;
// An Uninstall that ran while this lease still owed releases kept the hooks for them.
@@ -317,6 +338,16 @@ namespace MobileGL::MG_Remote::Server {
ReleaseAfterLease(hooks, releases);
}
void ServerDisplay::MarkLeaseEnding(void* holder) {
{
const std::lock_guard<std::mutex> lock(m_mutex);
if (holder == nullptr || m_leaseHolder != holder || m_leaseEnding) return;
m_leaseEnding = true;
}
// A request already refused is not waiting; one made from here on waits for the EndLease.
m_cv.notify_all();
}
void ServerDisplay::SuspendLease(void* holder, ServerWindowAttachedHook onAttached) {
if (holder == nullptr) return;
std::vector<void*> releases;
@@ -379,6 +410,11 @@ namespace MobileGL::MG_Remote::Server {
return m_leaseHolder != nullptr;
}
Bool ServerDisplay::LeaseEnding() const {
const std::lock_guard<std::mutex> lock(m_mutex);
return m_leaseHolder != nullptr && m_leaseEnding;
}
Uint64 ServerDisplay::Generation() const {
const std::lock_guard<std::mutex> lock(m_mutex);
return m_generation;
+20 -1
View File
@@ -35,6 +35,14 @@
// reference is kept and released when the lease finally ends - and Detach returns TimedOut, by
// name, so the UI thread is never held longer than its bound.
//
// A LEASE THAT IS ENDING IS WAITED FOR, NOT REFUSED. A holder whose session is over - it latched a
// fault (a device loss among them) or is stopping - says so (MarkLeaseEnding) before its client can
// learn of it; its lease still lasts until its backend has let go of the window. A client that loses
// its session re-creates its window surface on a fresh session at once, typically while the old
// session's apply thread is still tearing its backend down: that request waits for the old lease
// to end, bounded by its own timeout, instead of being refused as LeasedElsewhere. A holder whose
// session goes on keeps the window exclusively, as before.
//
// SUSPEND, NOT END (screen off / lock). A holder whose backend can let go of the window and keep
// everything else - its contexts, objects and device - ends its lease with SuspendLease instead of
// EndLease and names a second hook: the next Attach of a NEW window calls it, so the holder can
@@ -96,7 +104,9 @@ namespace MobileGL::MG_Remote::Server {
Interrupted, // Interrupt() (the in-process server is stopping)
// P14 S2: NOT A BUG ANY MORE. Several sessions share one process and the display's window
// is one window, so a second ON-SCREEN session asking for it is refused by name here
// (ServerLoop logs the holder); offscreen sessions take no lease and are unaffected.
// (ServerLoop logs the holder); offscreen sessions take no lease and are unaffected. A holder
// whose session is ending (MarkLeaseEnding) is waited for instead, and this is the answer
// only when its lease outlasted the wait.
LeasedElsewhere, // another session in this process holds the window: see the note above
};
@@ -156,6 +166,11 @@ namespace MobileGL::MG_Remote::Server {
// whole backend). Wakes a waiting Detach; releases a reference Detach left behind on timeout.
// A holder that holds no lease is a no-op.
void EndLease(void* holder);
// `holder`'s session is ending (it latched, or it is stopping): its lease ends once its
// backend has let go of the window, and another holder's AcquireFor waits for that rather
// than being refused (see the header block). A no-op for a holder that holds no lease; the
// mark ends with the lease.
void MarkLeaseEnding(void* holder);
// EndLease for a holder that only SUSPENDED its use of the window (its backend let go of the
// window and kept everything else). Also registers `onAttached`, which the next Attach of a
// new window calls - at once when a window is already attached. The registration ends when
@@ -169,6 +184,8 @@ namespace MobileGL::MG_Remote::Server {
// Diagnostics, for logs and tests.
Bool Attached() const;
Bool Leased() const;
// The lease is held by a holder whose session is ending (MarkLeaseEnding).
Bool LeaseEnding() const;
Uint64 Generation() const;
// How many holders wait for a window to resume on (SuspendLease).
Uint32 SuspendedHolders() const;
@@ -207,6 +224,8 @@ namespace MobileGL::MG_Remote::Server {
ServerWindowLostHook m_onLost = nullptr;
ServerWindowResizedHook m_onResized = nullptr;
Bool m_lostRequested = false;
// The holder's session is ending (MarkLeaseEnding); cleared with the lease.
Bool m_leaseEnding = false;
// References a timed-out Detach could not release yet; EndLease releases them.
std::vector<void*> m_releaseAfterLease;
// SuspendLease's registrations, told about the next new window.
+75 -8
View File
@@ -79,6 +79,24 @@ namespace MobileGL::MG_Remote::Server {
++m_serving;
m_turn.notify_all();
}
// Gives the turn up however deep the calling thread holds it, for a wait that does no
// backend work; answers the depth Resume restores (0: the thread did not hold it).
Uint32 YieldIfHeld() {
const std::lock_guard<std::mutex> lock(m_mutex);
if (m_owner != std::this_thread::get_id()) return 0;
const Uint32 depth = m_depth;
m_depth = 0;
m_owner = std::thread::id{};
++m_serving;
m_turn.notify_all();
return depth;
}
void Resume(Uint32 depth) {
if (depth == 0) return;
Lock();
const std::lock_guard<std::mutex> lock(m_mutex);
m_depth = depth;
}
private:
std::mutex m_mutex;
@@ -103,6 +121,31 @@ namespace MobileGL::MG_Remote::Server {
ScopedBackendTurn& operator=(const ScopedBackendTurn&) = delete;
};
// A WAIT FOR THE SERVER WINDOW HOLDS NO TURN. It can last seconds - a window that is not up
// yet, or the lease of a session that is ending - and the other sessions' apply threads need
// the turn meanwhile: the ending one to tear its backend down, which is what ends its lease.
// Nothing of the backend is touched while it is given up; it is taken back (a handoff, when
// another thread had it) before the waiter goes on.
class ScopedBackendTurnYield {
public:
ScopedBackendTurnYield() : m_depth(BackendTurn().YieldIfHeld()) {}
~ScopedBackendTurnYield() { BackendTurn().Resume(m_depth); }
ScopedBackendTurnYield(const ScopedBackendTurnYield&) = delete;
ScopedBackendTurnYield& operator=(const ScopedBackendTurnYield&) = delete;
private:
const Uint32 m_depth;
};
// The process-wide test seam (ServerLoop.h).
std::atomic<ServerLoop::ServerOwnedSurfaceHook> g_serverOwnedSurfaceHook{nullptr};
// FatalFunnel's latch observer. The latching thread is one of the session's own (its scope
// resolves ServerLoopInstance to that session's loop): a session that latched is over, and
// its lease on the server window, if it holds one, ends once its apply thread has torn the
// backend down - which a fresh session of the same client waits for (MarkLeaseEnding).
void MarkLatchedSessionsLeaseEnding() { ServerDisplayInstance().MarkLeaseEnding(&ServerLoopInstance()); }
// The bounded join. InProcessTransportTest.cpp:344 uses five seconds for the same
// reason: a lost wakeup must be a RED TEST and not a hung CI job.
constexpr Uint32 kJoinTimeoutMs = 5000;
@@ -333,6 +376,10 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_OK;
}
void ServerLoop::SetServerOwnedSurfaceHookForTesting(ServerOwnedSurfaceHook hook) {
g_serverOwnedSurfaceHook.store(hook, std::memory_order_release);
}
// ---------------------------------------------------------------------------------
// Start / the loop / Stop
// ---------------------------------------------------------------------------------
@@ -347,6 +394,8 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_ERR_NOT_INITIALIZED;
}
m_session = &session;
// A session that latches marks its window lease as ending before its client hears of it.
SetSessionLatchObserver(&MarkLatchedSessionsLeaseEnding);
m_stopRequested.store(false, std::memory_order_release);
m_abandonQueue.store(false, std::memory_order_release);
// PH-6 fix round: the session's copy of the request, for ReserveEventOrBlock's wait. Lowered
@@ -735,6 +784,11 @@ namespace MobileGL::MG_Remote::Server {
// AND AFTER AbandonQueuedRecords (P12 review fix), for the forfeit's reason: the server is
// stopping under a client that may still be streaming, and draining what it keeps queuing
// is the unbounded hold Stop()'s bounded join turns into an abort.
//
// THE SESSION IS ENDING, whatever ended it: its lease on the server window (if it holds one)
// ends below, once the backend is gone, and a fresh session of the same client that asks for
// the window meanwhile waits for that instead of being refused.
ServerDisplayInstance().MarkLeaseEnding(this);
std::optional<ScopedBackendTurn> exitTurn(std::in_place);
const Bool forfeited = session.ReverseChannelForfeited();
const Bool abandoned = m_abandonQueue.load(std::memory_order_acquire);
@@ -1293,6 +1347,9 @@ namespace MobileGL::MG_Remote::Server {
m_running.store(false, std::memory_order_release);
return;
}
// As on the apply thread's own way out: the window lease ends once the thread has torn the
// backend down, and a fresh session waits for it meanwhile.
ServerDisplayInstance().MarkLeaseEnding(this);
m_stopRequested.store(true, std::memory_order_release);
if (m_session != nullptr) {
// PH-6 fix round: the apply thread may be parked in ReserveEventOrBlock rather than in
@@ -1820,9 +1877,12 @@ namespace MobileGL::MG_Remote::Server {
ServerDisplay& display = ServerDisplayInstance();
ServerWindowLease local;
ServerWindowLease* const lease = out != nullptr ? out : &local;
const ServerWindowAcquire acquired = display.AcquireFor(width, height, timeoutMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, lease,
&ServerWindowResizedThunk);
ServerWindowAcquire acquired = ServerWindowAcquire::NoDisplay;
{
const ScopedBackendTurnYield yield;
acquired = display.AcquireFor(width, height, timeoutMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, lease, &ServerWindowResizedThunk);
}
switch (acquired) {
case ServerWindowAcquire::Acquired:
m_holdsWindowLease = true;
@@ -1902,7 +1962,10 @@ namespace MobileGL::MG_Remote::Server {
// Review fix: the backend is told which window is the server's own, so its surface init
// reports the extent for this window and for no window a client named (MGPipeServerOwnedWindow).
NoteServerWindowExtent(lease.window, lease.width, lease.height);
frame.ok = backend->CreateEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), window);
const ServerOwnedSurfaceHook surfaceHook = g_serverOwnedSurfaceHook.load(std::memory_order_acquire);
const EGLSurface ownedSurface = HandleFromToken<EGLSurface>(frame.surface);
frame.ok = surfaceHook != nullptr ? surfaceHook(backend, ownedSurface, window)
: backend->CreateEGLWindowSurface(ownedSurface, window);
if (!frame.ok) {
MGLOG_E("MG_Remote server: the backend could not create a window surface on the server window %p "
"(%ux%u) for ServerOwned seq %llu",
@@ -2084,10 +2147,14 @@ namespace MobileGL::MG_Remote::Server {
// have survived it). Bounded by the geometry grace: the window IS attached - that is what woke
// this - so only its size is waited for.
ServerWindowLease lease;
const ServerWindowAcquire acquired =
ServerDisplayInstance().AcquireFor(m_serverWindowWantWidth, m_serverWindowWantHeight,
ServerDisplay::kGeometryGraceMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, &lease, &ServerWindowResizedThunk);
ServerWindowAcquire acquired = ServerWindowAcquire::NoDisplay;
{
const ScopedBackendTurnYield yield;
acquired = ServerDisplayInstance().AcquireFor(m_serverWindowWantWidth, m_serverWindowWantHeight,
ServerDisplay::kGeometryGraceMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, &lease,
&ServerWindowResizedThunk);
}
if (acquired != ServerWindowAcquire::Acquired) {
// Still registered: the next new window wakes this again.
MGLOG_I("MG_Remote server: a suspended on-screen session did not get the new server window (%s); it "
+9
View File
@@ -449,6 +449,15 @@ namespace MobileGL::MG_Remote::Server {
// suspend, for the window-loss cases). Before Start(); fails when one is already installed.
MobileGLResult InstallBackendForTesting(UniquePtr<MG_Backend::BackendObject> backend);
// PROCESS-WIDE TEST SEAM for a server forked from a test (every session's loop reads it; null -
// the default - is the production path): the backend surface behind a ServerOwned window
// surface. A host has no window a backend can draw into, so a case that serves a stand-in
// window gives the backend a surface of the window's extent another way (a pbuffer). Called on
// the apply thread with the lease held, in place of CreateEGLWindowSurface.
using ServerOwnedSurfaceHook = Bool (*)(MG_Backend::BackendObject* backend, EGLSurface surface,
const MG_Backend::WindowHandle& window);
static void SetServerOwnedSurfaceHookForTesting(ServerOwnedSurfaceHook hook);
private:
RemoteControlSink m_remoteSink = nullptr;
void* m_remoteSinkUser = nullptr;