Skip to content

Commit 4e56c7c

Browse files
committed
fix(ui): two controls that could disagree with the hardware
The MCP toggle read its own button label to decide what to do: const isRunning = btn.textContent === 'Stop Server'; loadMcpStatus only relabels the button when the status call succeeds, and its catch merely logs. So one failed status read left the label stuck on "Starting..." while the server was in fact running. The next click evaluated that as "not running" and issued a second start against the already-bound port -- reporting "Address already in use" for a healthy server, with Stop unreachable short of restarting the app. State now comes from the backend and is explicitly null when unknown, in which case the toggle resolves it before acting instead of guessing. A failed read says so in the UI rather than leaving the previous render up. Separately, Home's 2s poll reassigned the turbo checkbox from sysfs while the user's own change was still waiting on pkexec. sysfs still reports the old value at that point, so the toggle flipped back under them while the password prompt was open. It corrected itself afterwards, so nothing stayed wrong -- it just read as the app rejecting the click. Refreshes now leave a control alone while its privileged write is in flight. 115 tests, lint and format clean.
1 parent 594d744 commit 4e56c7c

3 files changed

Lines changed: 84 additions & 10 deletions

File tree

‎src/js/hardwareControls.js‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ const { invoke } = window.__TAURI__.core;
2424
*/
2525
const GOVERNOR_SETTLE_MS = 500;
2626

27+
/**
28+
* Controls with a privileged write in flight.
29+
*
30+
* These writes go through pkexec, so they sit unresolved for as long as the
31+
* password prompt is on screen — often many seconds. Meanwhile Home polls every
32+
* 2s and reassigns `toggle.checked` from sysfs, which still reports the OLD
33+
* value because nothing has been written yet. The user watches their toggle
34+
* flip back while they are still typing their password.
35+
*
36+
* It did correct itself afterwards, so this is not a lasting desync — but it
37+
* reads as the app rejecting the click, which is the opposite of what happened.
38+
*/
39+
const inFlight = new Set();
40+
41+
/** Whether a refresh should leave this control alone. */
42+
export function isControlBusy(name) {
43+
return inFlight.has(name);
44+
}
45+
2746
/**
2847
* Run a privileged hardware action with consistent status reporting.
2948
*
@@ -95,13 +114,21 @@ export async function setCpuGovernor(governor, refresh, busy = []) {
95114
}
96115

97116
export async function setTurboBoost(enabled, refresh, toggleEl) {
98-
const ok = await runAction({
99-
pending: `${enabled ? 'Enabling' : 'Disabling'} turbo boost...`,
100-
success: `Turbo boost ${enabled ? 'enabled' : 'disabled'}`,
101-
invokeName: 'set_turbo_boost',
102-
args: { enabled },
103-
refresh
104-
});
117+
inFlight.add('turbo');
118+
let ok;
119+
try {
120+
ok = await runAction({
121+
pending: `${enabled ? 'Enabling' : 'Disabling'} turbo boost...`,
122+
success: `Turbo boost ${enabled ? 'enabled' : 'disabled'}`,
123+
invokeName: 'set_turbo_boost',
124+
args: { enabled },
125+
refresh
126+
});
127+
} finally {
128+
// Cleared before the failure handling below, so the corrective flip on a
129+
// rejected write is not itself suppressed by a later refresh.
130+
inFlight.delete('turbo');
131+
}
105132

106133
// A checkbox that stays flipped after a failed write tells the user the
107134
// opposite of what happened.

‎src/js/views/home.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Home View
22
const { invoke } = window.__TAURI__.core;
3-
import { setPowerProfile, setCpuGovernor, setTurboBoost, bindOnce } from '../hardwareControls.js';
3+
import {
4+
setPowerProfile,
5+
setCpuGovernor,
6+
setTurboBoost,
7+
bindOnce,
8+
isControlBusy
9+
} from '../hardwareControls.js';
410

511
export async function updateHomeView() {
612
try {
@@ -148,8 +154,11 @@ export async function updateHomeView() {
148154
if (statusEl) {
149155
statusEl.textContent = enabled ? 'Enabled' : 'Disabled';
150156
}
157+
// Not while the user's own change is still waiting on pkexec: sysfs
158+
// still reports the old value, so this would flip the toggle back under
159+
// them mid-authentication.
151160
const toggle = document.getElementById('home-turbo-toggle');
152-
if (toggle) {
161+
if (toggle && !isControlBusy('turbo')) {
153162
toggle.checked = enabled;
154163
}
155164
}

‎src/js/views/mcp.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ const { invoke } = window.__TAURI__.core;
33

44
let mcpSetupDone = false;
55

6+
/**
7+
* Whether the server is running, as last reported by the backend.
8+
*
9+
* `null` means unknown — the status call failed, so nothing has confirmed the
10+
* state. Deriving this from the toggle button's label instead meant a single
11+
* failed status read could strand the UI showing "Starting..." forever while
12+
* the server ran fine.
13+
*/
14+
let serverRunning = null;
15+
616
export function setupMcpView() {
717
if (mcpSetupDone) {
818
return;
@@ -61,6 +71,7 @@ export async function loadMcpStatus() {
6171
const response = await invoke('get_mcp_status');
6272
if (response.success && response.data) {
6373
const { running, host, port, path } = response.data;
74+
serverRunning = running;
6475
if (running) {
6576
dot.className = 'status-dot installed';
6677
text.textContent = `Running on ${host}:${port}`;
@@ -81,9 +92,17 @@ export async function loadMcpStatus() {
8192

8293
// Update config snippets with current host/port
8394
updateConfigSnippets(host, port, path);
95+
} else {
96+
// Backend answered but could not report state. Say so rather than leaving
97+
// whatever the last render showed.
98+
serverRunning = null;
99+
dot.className = 'status-dot not-installed';
100+
text.textContent = `Status unavailable: ${response.error ?? 'unknown error'}`;
84101
}
85102
} catch (error) {
86103
console.error('[MCP] Status check failed:', error);
104+
serverRunning = null;
105+
text.textContent = 'Status unavailable';
87106
}
88107
}
89108

@@ -137,7 +156,26 @@ async function toggleMcpServer() {
137156
return;
138157
}
139158

140-
const isRunning = btn.textContent === 'Stop Server';
159+
// Read the tracked state, not the button's own label. loadMcpStatus only
160+
// relabels the button when the status call succeeds, and its catch merely
161+
// logs -- so one failed status read left the label reading "Starting..."
162+
// while the server was in fact running. The next click then evaluated
163+
// "not Stop Server" as "not running" and issued a second start against the
164+
// bound port, reporting "Address already in use" for a healthy server, with
165+
// Stop unreachable short of restarting the app.
166+
if (serverRunning === null) {
167+
// State unknown after a failed read: resolve it before acting rather than
168+
// guessing, since guessing wrong is what caused the deadlock.
169+
await loadMcpStatus();
170+
if (serverRunning === null) {
171+
if (text) {
172+
text.textContent = 'Cannot reach the server status - try again';
173+
}
174+
return;
175+
}
176+
}
177+
178+
const isRunning = serverRunning;
141179
btn.disabled = true;
142180

143181
try {

0 commit comments

Comments
 (0)