fix(session): read the compressed client back when it is in front of a running game (#2600)

* fix(session): read the compressed client back when it is in front of a running game

* fix(session): keep the client compressed while focus is unknown

A missing or unreadable focus.json no longer counts as the client in front
of the game, so the readback timer starts only on a confirmed focus change.

* fix(session): invalidate focus snapshots after collection failures

* fix(session): retry focus snapshot publication
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-17 11:20:29 -05:00
1 parent 8e49dfe190
commit 8c8cae85cf
6 files changed
+221 -17

No files matched your search

@@ -391,18 +391,26 @@ class FocusWatcher(threading.Thread):
x.close()
except (OSError, AgentError) as e:
log("focus snapshot failed: %s" % e)
self.publish({})
time.sleep(2)
continue
if tag_game_windows(snap):
self.changed.set()
continue
body = {k: v for k, v in snap.items() if k != "t"}
if body == self.last:
continue
self.last = body
self.seq += 1
snap["seq"] = self.seq
write_json(self.path, snap)
self.publish(snap)
def publish(self, snap):
body = {k: v for k, v in snap.items() if k != "t"}
if body == self.last:
return
try:
write_json(self.path, dict(snap, seq=self.seq + 1))
except OSError as e:
log("focus publication failed: %s" % e)
self.changed.set()
return
self.seq += 1
self.last = body
def listen(self):
while True:
@@ -1104,7 +1104,7 @@ case $mode in
local id
id=$(sed -n 's/.*"focusedApp"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' \
"$BL_LAUNCH_DIR/agent/focus.json" 2>/dev/null | head -1)
[ -n "$id" ] || return "${1:-1}"
[ -n "$id" ] || return "${1:-2}"
case " ${_bl_apps-} " in
*" $id "*) return 0 ;;
esac
@@ -1122,7 +1122,7 @@ case $mode in
}
_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
_bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_back=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=()
_bl_client_pending=0 _bl_client_reserve=0 _bl_client_targets=()
_bl_client_pending=0 _bl_client_reserve=0 _bl_client_targets=() _bl_away=
_bl_sample_at= _bl_sample_available=0 _bl_sample_gpu=0 _bl_sample_unevictable=0 _bl_sample_shmem=0
while kill -0 $$ 2>/dev/null; do
bl_zram_scan
@@ -1155,7 +1155,10 @@ case $mode in
_bl_preserved_growth=$_bl_growth _bl_preserved_fall=$_bl_fall
bl_session_pressure
bl_client_pressure_finish
if [ "$_bl_game" = 1 ] && bl_game_focused; then
_bl_focus=1
[ "$_bl_game" = 1 ] && { bl_game_focused && _bl_focus=0 || _bl_focus=$?; }
if [ "$_bl_game" = 1 ] && [ "$_bl_focus" = 0 ]; then
_bl_away=
if [ "$_bl_since" -lt 0 ] || [ "$_bl_since_apps" != "$_bl_apps" ]; then
_bl_since=$SECONDS _bl_since_apps=$_bl_apps
fi
@@ -1165,15 +1168,31 @@ case $mode in
_bl_packed=$SECONDS
fi
elif [ "$_bl_game" != 1 ]; then
_bl_away=
if [ "$_bl_packed" -ge 0 ]; then
_bl_unpack=1
fi
_bl_since=-1 _bl_packed=-1 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=()
else
_bl_since=-1
if [ "$_bl_focus" = 1 ]; then
[ -n "${_bl_away-}" ] || _bl_away=$SECONDS
else
_bl_away=
fi
if [ "$_bl_packed" -ge 0 ]; then
_bl_unpack=1
fi
fi
if [ "$_bl_unpack" = 1 ] && [ "$_bl_game" = 0 ] && ! bl_over_cap && bl_client_can_readback; then
bl_zram_send 0 "memory recovered, reading the client back" client
if [ "$_bl_unpack" = 1 ] &&
{ [ "$_bl_game" = 0 ] || { [ "${_bl_ram_cap:-0}" = 0 ] && [ -n "${_bl_away-}" ] && [ $((SECONDS - _bl_away)) -ge 5 ]; }; } &&
! bl_over_cap && bl_client_can_readback; then
if [ "$_bl_game" = 0 ]; then
bl_zram_send 0 "memory recovered, reading the client back" client
else
bl_zram_send 0 "the client is in front of the game, reading it back" client
_bl_packed=-1
fi
_bl_unpack=0
fi
bl_watch_nap
+99 -4
View File
@@ -80,7 +80,7 @@ class ClientZramFocusTest(unittest.TestCase):
def test_refocus_restarts_the_delay_and_game_exit_restores_a_packed_client(self):
source = SCRIPT.read_text()
start = source.index(' if [ "$_bl_game" = 1 ] && bl_game_focused; then')
start = source.index(' _bl_focus=1\n')
stop = source.index(' bl_watch_nap\n', start)
body = source[start:stop]
harness = '''
@@ -125,7 +125,7 @@ bl_client_pressure_finish() { :; }
bl_session_pressure() { :; }
bl_client_can_pageout() { return 0; }
bl_client_can_readback() { [ "${readback:-1}" = 1 ]; }
bl_game_focused() { [ "$focused" = 1 ]; }
bl_game_focused() { [ "$focused" = 1 ] && return 0; [ "$focused" = 2 ] && return 2; return 1; }
bl_cap_due() { [ "$cap_due" = 1 ]; }
bl_over_cap() { [ "$over_cap" = 1 ]; }
bl_zram_send() { echo "send:$1:$3"; }
@@ -199,6 +199,97 @@ echo "restored:$_bl_unpack"
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'deferred:1', 'send:0:client', 'restored:0'], result.stdout.splitlines())
def test_client_in_front_of_a_running_game_is_read_back_and_compressed_again_later(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
tick 20 1 1 0 0
tick 25 1 0 0 0
echo "away:$_bl_packed"
tick 30 1 0 0 0
echo "read:$_bl_packed:$_bl_unpack"
tick 35 1 0 0 0
tick 40 1 1 0 0
tick 55 1 1 0 0
tick 60 1 1 0 0
echo "again:$_bl_packed"
tick 65 0 0 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'away:20', 'send:0:client', 'read:-1:0', 'send:1:client', 'again:60', 'send:0:client'],
result.stdout.splitlines())
def test_unknown_focus_keeps_the_client_compressed(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
tick 20 1 1 0 0
tick 25 1 2 0 0
tick 40 1 2 0 0
echo "unknown:$_bl_packed:$_bl_unpack"
tick 45 1 0 0 0
tick 48 1 2 0 0
tick 52 1 0 0 0
tick 56 1 0 0 0
echo "flicker:$_bl_packed"
tick 57 1 0 0 0
echo "read:$_bl_packed"
tick 60 0 2 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'unknown:20:1', 'flicker:20', 'send:0:client', 'read:-1'],
result.stdout.splitlines())
def test_a_short_look_at_the_client_keeps_it_compressed(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
tick 20 1 1 0 0
tick 25 1 0 0 0
tick 28 1 1 0 0
tick 30 1 0 0 0
tick 34 1 0 0 0
echo "packed:$_bl_packed"
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'packed:20'], result.stdout.splitlines())
def test_client_in_front_waits_for_memory_and_a_ram_cap_keeps_it_compressed(self):
result = run_bash(self.monitor_harness() + '''
tick 0 1 1 0 0
tick 20 1 1 0 0
readback=0
tick 25 1 0 0 0
tick 35 1 0 0 0
echo "tight:$_bl_packed"
readback=1
tick 40 1 0 0 0
echo "eased:$_bl_packed"
_bl_ram_cap=1048576
tick 60 1 1 0 0
tick 80 1 1 0 0
tick 85 1 0 0 0
tick 100 1 0 0 0
echo "capped:$_bl_packed"
tick 105 0 0 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'tight:20', 'send:0:client', 'eased:-1', 'send:1:client', 'capped:80', 'send:0:client'],
result.stdout.splitlines())
def test_client_in_front_without_compression_reads_nothing_back(self):
result = run_bash(self.monitor_harness() + '''
BL_ZRAM_CLIENT=0
tick 0 1 1 0 0
tick 25 1 0 0 0
tick 40 1 0 0 0
tick 45 0 0 0 0
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual('', result.stdout)
def test_scan_keeps_the_running_game_and_ignores_steams_own_reaper(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
@@ -219,7 +310,8 @@ echo "restored:$_bl_unpack"
%s
check() {
BL_DESKTOP=$1 BL_LAUNCH_DIR=$2 _bl_apps=$3
if bl_game_focused; then echo yes; else echo no; fi
bl_game_focused
echo $?
}
check "" "$dir" " 413150"
check "" "$dir" " 1245620"
@@ -227,7 +319,10 @@ printf '%%s\\n' '{"focusedApp": 999}' > "$dir/agent/focus.json"
check "" "$dir" " 413150"
printf '%%s\\n' '{"focusedApp": 769}' > "$dir/agent/focus.json"
check "" "$dir" " 413150"
printf '%%s\\n' '{"t": 2}' > "$dir/agent/focus.json"
check "" "$dir" " 413150"
rm -f "$dir/agent/focus.json"
check "" "$dir" " 413150"
check 1 "$dir" " 413150"
check "" "" " 413150"
''' % focus
@@ -238,7 +333,7 @@ check "" "" " 413150"
(launch / 'agent' / 'focus.json').write_text('{"t": 1, "focusedApp": 413150}\n')
result = run_bash('dir=%s\n%s' % (launch, script))
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual(['yes', 'no', 'no', 'no', 'yes', 'yes'], result.stdout.splitlines())
self.assertEqual(['0', '1', '1', '1', '2', '2', '0', '0'], result.stdout.splitlines())
@staticmethod
def write_cmd(root, pid, args):
+76
View File
@@ -867,6 +867,82 @@ const value = subscribe => {
return e
class FocusSnapshotTest(unittest.TestCase):
def test_publication_failure_retries_unknown_and_recovers(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'focus.json'
watcher = agent.FocusWatcher(path)
watcher.publish({'focusedApp': 42})
write_json = agent.write_json
writes = []
def write(path, value):
writes.append(value)
if len(writes) == 1:
raise OSError('unavailable')
write_json(path, value)
with patch.object(threading.Thread, 'start'), \
patch.object(watcher.changed, 'wait', side_effect=[True, True, True, StopIteration]), \
patch.object(agent, 'vr_scene_app', return_value=None), \
patch.object(agent, 'focus_snapshot', side_effect=[OSError('unavailable'), OSError('unavailable'), {'focusedApp': 42}]), \
patch.object(agent, 'write_json', side_effect=write), \
patch.object(agent.time, 'sleep'), \
patch.object(agent, 'log'):
with self.assertRaises(StopIteration):
watcher.run()
self.assertEqual(writes, [{'seq': 2}, {'seq': 2}, {'focusedApp': 42, 'seq': 3}])
self.assertEqual(json.loads(path.read_text()), {'focusedApp': 42, 'seq': 3})
def test_successful_snapshot_write_failure_retries(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'focus.json'
watcher = agent.FocusWatcher(path)
with patch.object(agent, 'write_json', side_effect=OSError('unavailable')), patch.object(agent, 'log'):
watcher.publish({'focusedApp': 42})
self.assertIsNone(watcher.last)
self.assertEqual(watcher.seq, 0)
self.assertTrue(watcher.changed.is_set())
watcher.publish({'focusedApp': 42})
self.assertEqual(json.loads(path.read_text()), {'focusedApp': 42, 'seq': 1})
def test_collection_failure_invalidates_and_recovery_republishes(self):
for error in (OSError('unavailable'), agent.AgentError('X', 'unavailable')):
with self.subTest(error=type(error).__name__), tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'focus.json'
watcher = agent.FocusWatcher(path)
snapshot = {'focusedApp': 42, 't': 1}
watcher.publish(snapshot)
observed = []
def collect():
observed.append(json.loads(path.read_text()))
if len(observed) == 1:
raise error
return dict(snapshot, t=2)
with patch.object(threading.Thread, 'start'), \
patch.object(watcher.changed, 'wait', side_effect=[True, True, StopIteration]), \
patch.object(agent, 'vr_scene_app', return_value=None), \
patch.object(agent, 'focus_snapshot', side_effect=collect), \
patch.object(agent.time, 'sleep'), \
patch.object(agent, 'log'):
with self.assertRaises(StopIteration):
watcher.run()
self.assertEqual(observed, [dict(snapshot, seq=1), {'seq': 2}])
self.assertEqual(json.loads(path.read_text()), {'focusedApp': 42, 't': 2, 'seq': 3})
def test_unchanged_successful_focus_stays_valid(self):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / 'focus.json'
watcher = agent.FocusWatcher(path)
watcher.publish({'focusedApp': 769, 't': 1})
with patch.object(agent, 'write_json') as write:
watcher.publish({'focusedApp': 769, 't': 100})
write.assert_not_called()
self.assertEqual(json.loads(path.read_text()), {'focusedApp': 769, 't': 1, 'seq': 1})
class VrFocusTest(unittest.TestCase):
def test_scene_dashboard_and_external_focus_changes(self):
focus = agent.VrSceneFocus()
+1 -1
View File
@@ -455,7 +455,7 @@ class ReclaimDispatchTest(unittest.TestCase):
source = source.replace('/tmp/.bl-zram', str(root / 'ctl'))
monitor = SCRIPT.read_text()
start = monitor.index(' _bl_preserved_growth=$_bl_growth', monitor.index('while kill -0 $$'))
stop = monitor.index(' if [ "$_bl_game" = 1 ] && bl_game_focused', start)
stop = monitor.index(' _bl_focus=1\n', start)
cycle = monitor[start:stop] if body is None else body
script = f'''
set -u