Repository navigation
feat(power): add configurable shutdown timeout for deep sleep - #871
dysprosium wants to merge 1 commit into
Conversation
Pobega
left a comment
There was a problem hiding this comment.
This reads like unreviewed AI slop output; there's a lot of dead/redundant code, leftover mock env vars with no tests using them, and straight up hallucinations (calling sync to persist an environment variable? What planet is this)
Besides that, the PR is ~700 lines across 16 files with contradictory behavior per-platform and actual bugs. I'm sorry, I generally try to be polite but this can't be considered for merging in this state.
Also, your PR description is out of date; it still refers to "Auto poweroff" / "Off" instead of "Shutdown timeout" / "Never", and takes credit for SRM/RTC/quicksave preservation that Menu_beforeSleep() already did.
| shutdown_timeout_deadline=$((now + shutdown_timeout_rtc_deadline - rtc_now)) | ||
| >&2 echo "Shutdown timeout armed for $shutdown_timeout_secs seconds (RTC $rtc_now -> $shutdown_timeout_rtc_deadline)" | ||
| # Persist the armed deadline before an automatic wake and poweroff. | ||
| sync |
There was a problem hiding this comment.
I'm not sure what this sync is for? "Persist the armed deadline before an automatic wake and poweroff" has real LLM hallucination smell, it's not really explaining why you'd want to flush the cached filesystem writes.
| rtc_dir=${NEXTUI_RTC_DIR:-} | ||
| rtc_device=${NEXTUI_RTC_DEVICE:-} | ||
| if [ -z "$rtc_dir" ] && [ -n "$rtc_device" ]; then | ||
| rtc_dir=/sys/class/rtc/${rtc_device##*/} | ||
| fi | ||
| if [ -z "$rtc_dir" ]; then | ||
| for rtc_candidate_dir in /sys/class/rtc/rtc*; do | ||
| [ -d "$rtc_candidate_dir" ] || continue | ||
| if [ "$(cat "$rtc_candidate_dir/name" 2>/dev/null)" = "sunxi-rtc" ]; then | ||
| rtc_dir=$rtc_candidate_dir | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
| [ -n "$rtc_dir" ] || rtc_dir=/sys/class/rtc/rtc0 | ||
| [ -n "$rtc_device" ] || rtc_device=/dev/${rtc_dir##*/} | ||
| rtc_since_epoch=$rtc_dir/since_epoch | ||
| rtc_wakeup=$rtc_dir/device/power/wakeup | ||
| rtc_name=$(cat "$rtc_dir/name" 2>/dev/null) | ||
| case "$rtc_name" in | ||
| sunxi-rtc) rtc_alarm_resolution=1 ;; | ||
| *) rtc_alarm_resolution=60 ;; | ||
| esac | ||
| if [ -n "${NEXTUI_RTC_RESOLUTION_SECONDS:-}" ]; then | ||
| rtc_alarm_resolution=$NEXTUI_RTC_RESOLUTION_SECONDS | ||
| fi | ||
| case "$rtc_alarm_resolution" in | ||
| ''|*[!0-9]*|0) rtc_alarm_resolution=60 ;; | ||
| esac | ||
| rtc_alarm_tool=${NEXTUI_RTC_ALARM_TOOL:-$SYSTEM_PATH/bin/rtc_alarm} |
There was a problem hiding this comment.
What are the NEXTUI_RTC* variables for? Mock/dependency injection? I'm a bit confused what they do, could you elaborate?
There's no tests that use them, so I can only imagine they were for testing during development and I'm not sure why they made it into the PR if so
| if [ -w "$os_sleep" ]; then | ||
| if printf '16' > "$os_sleep"; then | ||
| >&2 echo "Deep sleep policy: Super Standby (os_sleep=16)" | ||
| else | ||
| >&2 echo "Deep sleep policy warning: failed to enable Super Standby" | ||
| fi | ||
| fi |
There was a problem hiding this comment.
I'm gonna be honest I don't like this whole block. There are simpler ways to write bash scripts, but additionally this is unrelated debugging logging noise.
| rtc_alarm_available() { | ||
| if ! existing_alarm=$(cat "$rtc_wakealarm" 2>/dev/null); then | ||
| >&2 echo "Shutdown timeout unavailable: failed to read RTC wake alarm" | ||
| return 1 | ||
| fi | ||
|
|
||
| case "$existing_alarm" in | ||
| '') return 0 ;; | ||
| *[!0-9]*) | ||
| >&2 echo "Shutdown timeout unavailable: invalid RTC wake alarm '$existing_alarm'" | ||
| return 1 | ||
| ;; | ||
| *) | ||
| >&2 echo "Shutdown timeout skipped: preserving existing RTC alarm $existing_alarm" | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } |
There was a problem hiding this comment.
Again, I'm a bit confused. This refuses to arm the timeout if an existing RTC alarm is present (preserving existing RTC alarm), but the H700 implementation (rtc_alarm.c:238-244) unconditionally calls RTC_AIE_OFF and wipes any prior alarm. Is there a reason for different behavior per-platform that's just undocumented? If not, then we should probably have it be consistent across platforms.
| now=$(date +%s) | ||
| if printf '+%s\n' "$shutdown_timeout_secs" > "$rtc_wakealarm" 2>/dev/null; then | ||
| shutdown_timeout_deadline=$((now + shutdown_timeout_secs)) | ||
| >&2 echo "Shutdown timeout armed for $shutdown_timeout_secs seconds" | ||
| else | ||
| >&2 echo "Shutdown timeout unavailable: failed to arm RTC wake alarm" | ||
| fi | ||
| } | ||
|
|
||
| clear_shutdown_timeout() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 0 | ||
| printf '0\n' > "$rtc_wakealarm" 2>/dev/null || true | ||
| } | ||
|
|
||
| shutdown_timeout_expired() { | ||
| [ -n "$shutdown_timeout_deadline" ] || return 1 | ||
| now=$(date +%s) | ||
| [ "$now" -ge "$shutdown_timeout_deadline" ] | ||
| } |
There was a problem hiding this comment.
There's a real race condition here, if I'm understanding correctly. wakealarm fires based on the hardware RTC (dropping sub-seconds), while shutdown_timeout_expired checks date +%s. If the RTC ticks over even a fraction of a second ahead of the system clock (or drifts by a second) the alarm wakes the device while date +%s is still 1s short of the deadline, so it exits 0 and turns the screen back on instead of powering off.
| static int read_wakeup_status(const char *path, char *status, size_t status_size) | ||
| { | ||
| int fd = open(path, O_RDONLY | O_CLOEXEC); | ||
| if (fd < 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to open wake source %s: %s\n", | ||
| path, strerror(errno)); | ||
| return -1; | ||
| } | ||
|
|
||
| ssize_t length = read(fd, status, status_size - 1); | ||
| int read_errno = errno; | ||
| if (close(fd) < 0 && length >= 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to close wake source %s: %s\n", | ||
| path, strerror(errno)); | ||
| return -1; | ||
| } | ||
| if (length < 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to read wake source %s: %s\n", | ||
| path, strerror(read_errno)); | ||
| return -1; | ||
| } | ||
| if (length == 0) { | ||
| fprintf(stderr, "rtc_alarm: wake source %s returned an empty status\n", path); | ||
| return -1; | ||
| } | ||
|
|
||
| status[length] = '\0'; | ||
| status[strcspn(status, "\r\n")] = '\0'; | ||
| return 0; | ||
| } | ||
|
|
||
| static int enable_wakeup(const char *path) | ||
| { | ||
| char status[32]; | ||
| if (read_wakeup_status(path, status, sizeof(status)) < 0) | ||
| return -1; | ||
| if (strcmp(status, "enabled") == 0) { | ||
| fprintf(stderr, "rtc_alarm: wake source already enabled (%s)\n", path); | ||
| return 0; | ||
| } | ||
| if (strcmp(status, "disabled") != 0) { | ||
| fprintf(stderr, "rtc_alarm: invalid wake source status '%s' from %s\n", | ||
| status, path); | ||
| return -1; | ||
| } | ||
|
|
||
| int fd = open(path, O_WRONLY | O_CLOEXEC); | ||
| if (fd < 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to open wake source %s for writing: %s\n", | ||
| path, strerror(errno)); | ||
| return -1; | ||
| } | ||
|
|
||
| static const char enabled[] = "enabled\n"; | ||
| ssize_t written = write(fd, enabled, sizeof(enabled) - 1); | ||
| int write_errno = errno; | ||
| if (close(fd) < 0 && written >= 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to close wake source %s: %s\n", | ||
| path, strerror(errno)); | ||
| return -1; | ||
| } | ||
| if (written < 0) { | ||
| fprintf(stderr, "rtc_alarm: failed to enable wake source %s: %s\n", | ||
| path, strerror(write_errno)); | ||
| return -1; | ||
| } | ||
| if (written != (ssize_t)(sizeof(enabled) - 1)) { | ||
| fprintf(stderr, "rtc_alarm: short write enabling wake source %s (%zd of %zu)\n", | ||
| path, written, sizeof(enabled) - 1); | ||
| return -1; | ||
| } | ||
|
|
||
| if (read_wakeup_status(path, status, sizeof(status)) < 0) | ||
| return -1; | ||
| if (strcmp(status, "enabled") != 0) { | ||
| fprintf(stderr, "rtc_alarm: wake source %s remained '%s' after enable\n", | ||
| path, status); | ||
| return -1; | ||
| } | ||
|
|
||
| fprintf(stderr, "rtc_alarm: wake source enabled (%s)\n", path); | ||
| return 0; | ||
| } |
There was a problem hiding this comment.
83 lines of C just to echo enabled > "$rtc_wakeup" from a shell script that already reads that sysfs file. Actually, why is this a 358-line C binary at all?
| /* Explicitly disable the previous alarm interrupt before every arm. This | ||
| * follows the reliable clear-then-set sequence while avoiding wakealarm | ||
| * sysfs EBUSY handling. */ | ||
| if (clear_alarm(fd) < 0) { | ||
| fprintf(stderr, "rtc_alarm: refusing to arm without clearing the prior alarm interrupt\n"); | ||
| return -1; | ||
| } |
There was a problem hiding this comment.
This unconditionally clears any existing alarm which contradicts rtc_alarm_available() on tg5040/tg5050.
| if (strcmp(argv[1], "enable") == 0) | ||
| return enable_wakeup(rtc_wakeup) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
There was a problem hiding this comment.
As far as I can tell this is dead code, nothing ever called rtc_alarm enable.
| { | ||
| if (argc < 2 || (strcmp(argv[1], "arm") == 0 && argc != 3) || | ||
| (strcmp(argv[1], "clear") == 0 && argc != 2) || | ||
| (strcmp(argv[1], "enable") == 0 && argc != 2)) { |
| } else if (strcmp(argv[1], "clear") == 0) { | ||
| result = clear_alarm(fd); | ||
| } else { | ||
| result = clear_alarm(fd); | ||
| } |
There was a problem hiding this comment.
Line 318 already rejected everything except "arm" and "clear", so this else branch is both unreachable and identical to the else if right above it.
Tested on the RG SP, RG 35XXH, RG CubeXX, TrimUI Brick
Original PR from H700 branch: pvaibhav#27