Skip to content

fix: three defects around offline CPUs and a blocked UI thread - #38

Merged
vietanhdev merged 1 commit into
mainfrom
fix/monitor-blocking-and-offline-cpus
Jul 20, 2026
Merged

fix: three defects around offline CPUs and a blocked UI thread#38
vietanhdev merged 1 commit into
mainfrom
fix/monitor-blocking-and-offline-cpus

Conversation

@vietanhdev

Copy link
Copy Markdown
Owner

Three independent runtime defects found while auditing the backend. All three are invisible in normal operation on a machine with every core online.

1. The Monitor view blocks the UI thread ~10% of the time

get_system_monitor was a non-async #[tauri::command]. Tauri's default is ExecutionContext::Blocking, so the body runs inline on the IPC thread — not on the async runtime.

That body sleeps 200ms between the two /proc/stat samples it has to diff, and additionally shells out to df and ps aux synchronously. monitor.js polls every 2s.

So for as long as the Monitor view is open, the main thread is hard-blocked for ≥200ms out of every 2000ms — a visible duty cycle of dropped frames in scrolling, hover states, and window dragging.

spawn_blocking moves the whole collection off, subprocesses included, rather than just converting the sleep.

2. Setting the CPU governor fails wholesale if any CPU is offline

The old script counted cpuN directories, then emitted a bare redirect per index 0..count under set -e. Two bad assumptions: the count includes CPUs that are present but offline — whose cpufreq/ directory the kernel removes — and the numbering is assumed contiguous.

Disable SMT (echo 0 > .../cpu5/online) and the redirect for cpu5 fails with ENOENT, set -e aborts, pkexec returns non-zero — after cpu0–cpu4 were already changed. Mixed governors, and a UI saying the operation failed.

Now globs at execution time (as root, so it sees whatever is online at that moment) and treats only a total failure as an error.

3. Per-core stats are shifted past an offline CPU

parse_cpu_snapshot parsed each core id purely to check it was numeric, then discarded it and let the caller's enumerate() index stand in.

/proc/stat omits offline CPUs entirely. With cpu5 offline the file lists cpu0–4, cpu6, cpu7 — so vector index 5 is really cpu6. Every core past the gap was labelled one too low and read its frequency from the wrong CPU. Index 5 pointed at the offline cpu5, which has no cpufreq directory: 0 MHz.

The id is now carried through, and the two snapshots are matched by id rather than by position, so a CPU going offline between the two 200ms-apart reads can't diff one core's counters against another's.

Verification

104 tests, clippy clean.

The governor tests are checked against the genuine original implementation rather than a paraphrase — restoring it fails all four:

one_refused_write_does_not_discard_the_successful_ones ... FAILED
no_writable_cpu_at_all_is_still_an_error ... FAILED
non_contiguous_cpu_numbering_is_handled ... FAILED
an_offline_cpu_does_not_abort_the_others ... FAILED

Worth recording: an earlier mutation that only re-added set -e passed, because bash suppresses errexit inside an if condition. The original's bare redirects were what made it fire — so the weaker mutation proved nothing, and the test needed the real thing to be trustworthy.

get_system_monitor was a non-async #[tauri::command], which tauri runs
inline on the IPC thread. It sleeps 200ms between the two /proc/stat
samples it has to diff, and shells out to df and ps aux. The Monitor view
polls every 2s, so the UI was hard-blocked for at least 200ms out of every
2000 -- a visible ~10% duty cycle of dropped frames while that view was
open. spawn_blocking moves the whole collection off, subprocesses included.

set_cpu_governor counted cpuN directories and emitted one bare redirect
per index under set -e. That count includes CPUs that are present but
OFFLINE, whose cpufreq/ directory the kernel removes, and it assumes
contiguous numbering. Offlining a core aborted the script partway, so
earlier cores were already changed while the UI reported failure. Now
globs at execution time and only errors when nothing applied at all.

parse_cpu_snapshot parsed each core id to check it was numeric and then
discarded it, letting the Vec index stand in. /proc/stat omits offline
CPUs, so past a gap every core was labelled one too low and read its
frequency from the wrong CPU -- the offline one, showing 0 MHz. The id is
now carried, and the two snapshots are matched by id rather than position
so a CPU going offline between them cannot diff one core against another.

The governor tests are verified against the genuine original
implementation, not a paraphrase: restoring it fails all four. An earlier
mutation that only re-added 'set -e' passed, because bash suppresses
errexit inside an if condition -- the original's bare redirects were what
made it fire.

104 tests.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
❌ Deployment failed
View logs
thinkutils 6a72077 Jul 20 2026, 03:14 AM

@vietanhdev
vietanhdev merged commit 44f3dc8 into main Jul 20, 2026
9 of 10 checks passed
@vietanhdev
vietanhdev deleted the fix/monitor-blocking-and-offline-cpus branch July 20, 2026 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant