From c868977f1c3e9d6166ab7a3659e1a56778f5420a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Martin?= Date: Tue, 6 Oct 2026 10:08:10 +0200 Subject: [PATCH] A key sent through the Debug Console could turn the screen "off" for a tick The `key` command stamps the power timer from millis(); the power tick then compared with its pass's older time, and the unsigned difference read as 49 days without a key. The screen state went Off for one tick, and the next key was swallowed as a wake-up: about one remote key in twenty-five. Keys from the keyboard pass the loop's own time and were never affected. The same shape as #46. PowerPolicy::update now treats a stamp from the future as "just now", with a test. rdbg.py: piped lines written while it was still connecting stayed in Python's read buffer until the next line arrived (readline() behind select()). It reads the descriptor directly now. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT --- lib/services/src/power_policy.cpp | 5 ++++- scripts/rdbg.py | 11 ++++++++--- test/test_power/test_power.cpp | 10 ++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/services/src/power_policy.cpp b/lib/services/src/power_policy.cpp index 0fd5b01..bb48c4a 100644 --- a/lib/services/src/power_policy.cpp +++ b/lib/services/src/power_policy.cpp @@ -10,7 +10,10 @@ bool PowerPolicy::activity(uint32_t nowMs) { } ScreenState PowerPolicy::update(uint32_t nowMs) { - uint32_t idle = nowMs - lastActivityMs_; + // Activity stamped from a clock read after this pass's nowMs (the Debug Console's `key`) is in + // the future, not 49 days ago: unsigned, the screen went off for a tick and ate the next key. + int32_t since = static_cast(nowMs - lastActivityMs_); + uint32_t idle = since < 0 ? 0 : static_cast(since); state_ = idle >= offMs_ ? ScreenState::Off : idle >= dimMs_ ? ScreenState::Dimmed : ScreenState::On; if (notifying_) { if (static_cast(nowMs - notifyUntilMs_) >= 0) diff --git a/scripts/rdbg.py b/scripts/rdbg.py index d75bba8..deccc60 100755 --- a/scripts/rdbg.py +++ b/scripts/rdbg.py @@ -224,10 +224,15 @@ def interactive(sock): sys.stdout.write(data.decode(errors="replace")) sys.stdout.flush() if sys.stdin in ready: - line = sys.stdin.readline() - if not line: + # Straight from the descriptor: readline() takes every waiting line into Python's own + # buffer and hands over one, and select() then sees nothing more to read. Piped input + # written while we were still connecting got stuck until the next line came. + data = os.read(sys.stdin.fileno(), 4096) + if not data: return - sock.sendall(line.encode()) + sock.setblocking(True) + sock.sendall(data) + sock.setblocking(False) def main(): diff --git a/test/test_power/test_power.cpp b/test/test_power/test_power.cpp index 60bd00d..93fba26 100644 --- a/test/test_power/test_power.cpp +++ b/test/test_power/test_power.cpp @@ -31,6 +31,15 @@ void test_key_on_off_screen_only_wakes() { TEST_ASSERT_EQUAL(static_cast(ScreenState::On), static_cast(p.update(70001))); } +// A key stamped a few milliseconds after the time the same pass updates with must not read as +// 49 days of idleness: the screen stays on, and the next key acts. +void test_activity_stamped_after_the_pass_time_keeps_the_screen_on() { + PowerPolicy p(30000, 60000); + TEST_ASSERT_FALSE(p.activity(10003)); + TEST_ASSERT_TRUE(p.update(10000) == ScreenState::On); + TEST_ASSERT_FALSE(p.activity(10050)); +} + void test_new_timeouts_apply_immediately() { PowerPolicy p(30000, 60000); p.activity(0); @@ -91,6 +100,7 @@ int main() { RUN_TEST(test_screen_dims_then_turns_off_after_inactivity); RUN_TEST(test_key_on_dimmed_screen_wakes_and_still_acts); RUN_TEST(test_key_on_off_screen_only_wakes); + RUN_TEST(test_activity_stamped_after_the_pass_time_keeps_the_screen_on); RUN_TEST(test_new_timeouts_apply_immediately); RUN_TEST(test_notification_lights_an_off_screen_dimmed_until_it_ends); RUN_TEST(test_notification_does_not_count_as_activity);