When the leader is hidden and no visible session exists, the code falls back to leader_sid (the hidden leader) without re-electing. However, the condition replacement and replacement != leader_sid is checked after _select_graph_leader with preferred_sid=sid. If _select_graph_leader returns the requester (which is also hidden, since all tabs are hidden), replacement will equal sid, which is not leader_sid, so it will set the requester as the new leader. This is correct per the test. But if _select_graph_leader returns None (e.g., require_graph_active=True is not passed, but list_sessions returns empty due to a race), the code falls through to target_sid = leader_sid, keeping the hidden leader. This is intentional. The real issue: when _select_graph_leader returns the same hidden leader (because it's the only session), replacement == leader_sid is true, so target_sid stays leader_sid. That's fine. However, the else branch after if replacement and replacement != leader_sid sets target_sid = leader_sid without checking if replacement is None. If _select_graph_leader returns None (e.g., all sessions disconnected between the is_session_active check and list_sessions), target_sid becomes leader_sid, which may now be disconnected. The subsequent socketio.emit to that room will silently fail, and the sync request is lost. This is a pre-existing pattern in the codebase, but the new logic amplifies the window where _select_graph_leader can return None due to the two-tier selection. The test test_relay_collaboration_event_sync_request_all_hidden_falls_back_to_requester mocks list_sessions to return sessions, so it doesn't cover the None case.