fix: fix periodic freeze in print stack

This commit is contained in:
2026-05-14 11:52:28 +02:00
parent bb12aeb9b9
commit eac8feb62c
3 changed files with 126 additions and 52 deletions
+26 -3
View File
@@ -16,6 +16,12 @@ extern "C" {
using namespace print;
static constexpr bool ENABLE_BACKGROUND_PROBE = false;
static constexpr uint64_t DAEMON_STATE_HEARTBEAT_MS = 15000;
static char g_last_state_phase[32] = {};
static char g_last_state_subject[128] = {};
static char g_last_state_detail[256] = {};
static uint64_t g_last_state_write_ms = 0;
enum class ClaimResult {
None,
@@ -25,6 +31,23 @@ enum class ClaimResult {
static void write_daemon_state(const char* phase, const char* subject = nullptr,
const char* detail = nullptr) {
const char* safe_phase = phase ? phase : "";
const char* safe_subject = subject ? subject : "";
const char* safe_detail = detail ? detail : "";
uint64_t now_ms = montauk::get_milliseconds();
if (g_last_state_write_ms != 0
&& strcmp(g_last_state_phase, safe_phase) == 0
&& strcmp(g_last_state_subject, safe_subject) == 0
&& strcmp(g_last_state_detail, safe_detail) == 0
&& now_ms - g_last_state_write_ms < DAEMON_STATE_HEARTBEAT_MS)
return;
safe_copy(g_last_state_phase, sizeof(g_last_state_phase), safe_phase);
safe_copy(g_last_state_subject, sizeof(g_last_state_subject), safe_subject);
safe_copy(g_last_state_detail, sizeof(g_last_state_detail), safe_detail);
g_last_state_write_ms = now_ms;
char now[32] = {};
now_string(now, sizeof(now));
@@ -33,9 +56,9 @@ static void write_daemon_state(const char* phase, const char* subject = nullptr,
"time=%s\npid=%d\nphase=%s\nsubject=%s\ndetail=%s\n",
now,
montauk::getpid(),
phase ? phase : "",
subject ? subject : "",
detail ? detail : "");
safe_phase,
safe_subject,
safe_detail);
if (n < 0) return;
write_text_file_atomic(SPOOL_DAEMON_STATE_PATH, buf);
}
+51 -6
View File
@@ -423,11 +423,35 @@ static void compute_config_layout(ConfigLayout* lo) {
lo->cancel_btn = {pad + 88, g_config.height - BTN_H - 16, 80, BTN_H};
}
static void refresh_state(bool force = false) {
static bool refresh_state(bool force = false) {
uint64_t now = montauk::get_milliseconds();
if (!force && g_app.printers_loaded && now - g_app.last_refresh_ms < 1000) return;
if (!force && g_app.printers_loaded && now - g_app.last_refresh_ms < 1000) return false;
ensure_spool_dirs();
bool was_loaded = g_app.printers_loaded;
char old_printer_uri[MAX_PATH_LEN];
safe_copy(old_printer_uri, sizeof(old_printer_uri), g_app.printer_uri);
bool old_daemon_running = g_app.daemon_running;
int old_daemon_pid = g_app.daemon_pid;
int old_queue_count = g_app.queue_count;
int old_active_count = g_app.active_count;
int old_done_count = g_app.done_count;
int old_failed_count = g_app.failed_count;
JobMeta old_active = g_app.last_active;
JobMeta old_failed = g_app.last_failed;
bool old_has_active = g_app.has_active;
bool old_has_failed = g_app.has_failed;
IppCapabilities old_probe_caps = g_app.probe_caps;
bool old_probe_valid = g_app.probe_valid;
bool old_probe_ok = g_app.probe_ok;
char old_probe_message[sizeof(g_app.probe_message)];
char old_probe_time[sizeof(g_app.probe_time)];
char old_probe_detail[sizeof(g_app.probe_detail)];
safe_copy(old_probe_message, sizeof(old_probe_message), g_app.probe_message);
safe_copy(old_probe_time, sizeof(old_probe_time), g_app.probe_time);
safe_copy(old_probe_detail, sizeof(old_probe_detail), g_app.probe_detail);
if (force || !g_app.printers_loaded)
ensure_spool_dirs();
if (!read_default_printer_uri(g_app.printer_uri, sizeof(g_app.printer_uri)))
g_app.printer_uri[0] = '\0';
@@ -449,6 +473,25 @@ static void refresh_state(bool force = false) {
g_app.has_failed = load_latest_job(SPOOL_FAILED_DIR, &g_app.last_failed);
g_app.printers_loaded = true;
g_app.last_refresh_ms = now;
return !was_loaded
|| !montauk::streq(old_printer_uri, g_app.printer_uri)
|| old_daemon_running != g_app.daemon_running
|| old_daemon_pid != g_app.daemon_pid
|| old_queue_count != g_app.queue_count
|| old_active_count != g_app.active_count
|| old_done_count != g_app.done_count
|| old_failed_count != g_app.failed_count
|| old_has_active != g_app.has_active
|| (g_app.has_active && memcmp(&old_active, &g_app.last_active, sizeof(g_app.last_active)) != 0)
|| old_has_failed != g_app.has_failed
|| (g_app.has_failed && memcmp(&old_failed, &g_app.last_failed, sizeof(g_app.last_failed)) != 0)
|| old_probe_valid != g_app.probe_valid
|| old_probe_ok != g_app.probe_ok
|| !montauk::streq(old_probe_message, g_app.probe_message)
|| !montauk::streq(old_probe_time, g_app.probe_time)
|| !montauk::streq(old_probe_detail, g_app.probe_detail)
|| memcmp(&old_probe_caps, &g_app.probe_caps, sizeof(g_app.probe_caps)) != 0;
}
static void probe_printer() {
@@ -1207,10 +1250,12 @@ extern "C" void _start() {
int r = g_win.poll(&ev);
if (r < 0) break;
if (r == 0) {
uint64_t last_refresh = g_app.last_refresh_ms;
refresh_state(false);
if (g_app.last_refresh_ms != last_refresh)
if (refresh_state(false))
redraw_main = true;
if (g_app.status_msg[0] && !status_visible()) {
g_app.status_msg[0] = '\0';
redraw_main = true;
}
if (redraw_main)
render();