Act on four reviews: state machine, resource bounds, teardown
Four agents reviewed this in parallel -- correctness, GNOME integration, edge cases, security. Everything below was reproduced before being fixed; several findings that survived the first reading did not survive a probe and are not here. State machine, the two that mattered most. A pending permission prompt was erased by any subagent bookkeeping event: SubagentStop or the next PreToolUse recomputed the state from scratch, so a session sat at "working" with a dialog open and nothing ever raised it again. Blocked now outlives everything except evidence the question was answered. Separately, the stale-event guard refused whole events, including the subagent counter's increments and decrements -- but those are deltas and deltas commute, so a "+1" that lost a timestamp race left the count short and the batch freed the session while a subagent was still running. The guard now gates the state decision only. Corrupt or hostile state files could wedge the panel or take the hook down for every session: a non-numeric pid raised inside sweep_dead before the hook wrote its own file, so one bad byte stopped new sessions appearing at all. Numbers read back from disk are coerced, one unreadable file no longer aborts the sweep, and a stored timestamp far in the future -- corruption, or a clock stepped backwards by NTP -- no longer refuses every later event forever. Resource bounds, all in the compositor process. A state file was read whole with no size check: a symlink to /dev/zero took a test process past 4 GB in three seconds, which in gnome-shell ends the session. Sizes are checked before the read, sessions and zellij subprocesses are capped, labels ellipsize, and cwd and messages are truncated at the hook. Teardown hung off an overridden destroy(), which only runs when JS calls it. An actor destroyed any other way -- another extension rebuilding the panel boxes -- left the timer and the file monitor running against a disposed actor. It is a destroy signal now. The zellij child is killed rather than merely abandoned. The glyph was pinned to physical pixels and rendered half-size on HiDPI; size comes from the stylesheet, and the foreground colour is normalised by inspection rather than assuming which colour struct the shell hands back. Chip labels: non-Latin names all collapsed to "?", because the split treated every Cyrillic letter as a separator -- notable for a tool whose own README is Russian. Seniority also ranked by time-in-state rather than session age, so after a shell restart the older session could take the digit; the hook now records when the session began. zellij: a dump ends with new_tab_template and swap_tiled_layout blocks whose tab lines carry no name, and their panes were being attached to the last real tab -- which then answered for every unmatched directory, confidently and wrongly. install.py no longer widens the mode of a settings.json someone narrowed to 0600, no longer overwrites the pristine .bak on a second run, no longer replaces a symlink out of a dotfiles repository with a regular file, and quotes the hook path. The debug log is capped and README now says plainly that it records prompts verbatim. Not fixed, deliberately: the panel does push the clock about 70 px left with three labelled chips, which is inherent to putting them in the centre box; two different projects abbreviating alike still read as one project with a digit; GNOME 48 remains unverified for the colour struct and for St.BoxLayout's vertical property, both flagged rather than guessed at.
This commit is contained in:
+39
-11
@@ -33,6 +33,17 @@ const UNKNOWN_PID_MAX_AGE = 36 * 3600; // seconds
|
||||
// lose that update.
|
||||
const TMP_MAX_AGE = 300; // seconds
|
||||
|
||||
// A state file is a few hundred bytes. Anything larger is corrupt or hostile,
|
||||
// and reading it whole would happen inside the compositor: a symlink to
|
||||
// /dev/zero took a test process past 4 GB in three seconds, which in
|
||||
// gnome-shell is the session ending. The size is checked before the read.
|
||||
const MAX_STATE_BYTES = 64 * 1024;
|
||||
|
||||
// Work here is on the compositor's main loop, and every session costs a menu
|
||||
// row of five actors. Well past any real use, and cheap insurance against a
|
||||
// directory someone filled up.
|
||||
const MAX_SESSIONS = 64;
|
||||
|
||||
export function stateRank(state) {
|
||||
const i = STATES.indexOf(state);
|
||||
return i < 0 ? STATES.length : i;
|
||||
@@ -134,16 +145,20 @@ export const SessionStore = GObject.registerClass({
|
||||
let enumerator;
|
||||
try {
|
||||
enumerator = await this._dir.enumerate_children_async(
|
||||
'standard::name,time::modified', Gio.FileQueryInfoFlags.NONE,
|
||||
'standard::name,standard::size,time::modified', Gio.FileQueryInfoFlags.NONE,
|
||||
GLib.PRIORITY_DEFAULT, cancellable);
|
||||
} catch (e) {
|
||||
// No directory yet means no sessions have ever run; not an error.
|
||||
if (e.matches?.(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND))
|
||||
// NOT_DIRECTORY means something took the path -- also not worth a
|
||||
// stack trace every 20 seconds for as long as it stays that way.
|
||||
if (e.matches?.(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND) ||
|
||||
e.matches?.(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_DIRECTORY))
|
||||
return [];
|
||||
throw e;
|
||||
}
|
||||
|
||||
const names = [];
|
||||
const locks = [];
|
||||
for (;;) {
|
||||
const batch = await enumerator.next_files_async(
|
||||
32, GLib.PRIORITY_DEFAULT, cancellable);
|
||||
@@ -153,15 +168,29 @@ export const SessionStore = GObject.registerClass({
|
||||
const name = info.get_name();
|
||||
// Only ".json" is state. ".lock" belongs to the hook, "debug"
|
||||
// is its opt-in event log, and ".tmp" is an interrupted write.
|
||||
if (name.endsWith('.json'))
|
||||
if (name.endsWith('.json')) {
|
||||
if (info.get_size() > MAX_STATE_BYTES) {
|
||||
// Not read at all: the point is to never allocate it.
|
||||
continue;
|
||||
}
|
||||
names.push(name);
|
||||
else if (name.endsWith('.tmp'))
|
||||
this._sweepTemp(info, name);
|
||||
} else if (name.endsWith('.tmp')) {
|
||||
this._sweepStale(info, name);
|
||||
} else if (name.endsWith('.json.lock')) {
|
||||
locks.push({ info, name });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A lock whose state file is gone belongs to nothing; the hook only
|
||||
// removes the pair together, so nobody else would ever clear it.
|
||||
for (const { info, name } of locks) {
|
||||
if (!names.includes(name.slice(0, -'.lock'.length)))
|
||||
this._sweepStale(info, name);
|
||||
}
|
||||
|
||||
const sessions = [];
|
||||
for (const name of names) {
|
||||
for (const name of names.slice(0, MAX_SESSIONS)) {
|
||||
const session = await this._readOne(name, cancellable);
|
||||
if (session)
|
||||
sessions.push(session);
|
||||
@@ -169,8 +198,9 @@ export const SessionStore = GObject.registerClass({
|
||||
return sessions;
|
||||
}
|
||||
|
||||
/** Delete an abandoned temporary file, once it is old enough to be sure. */
|
||||
_sweepTemp(info, name) {
|
||||
/** Delete an abandoned file, once it is old enough to be sure nobody is
|
||||
* part-way through writing it. */
|
||||
_sweepStale(info, name) {
|
||||
const modified = info.get_modification_date_time?.();
|
||||
if (!modified)
|
||||
return;
|
||||
@@ -235,12 +265,10 @@ export const SessionStore = GObject.registerClass({
|
||||
state,
|
||||
cwd: String(raw.cwd ?? ''),
|
||||
since: Number(raw.since) || 0,
|
||||
started: Number(raw.started) || 0,
|
||||
pid,
|
||||
message: String(raw.message ?? ''),
|
||||
agents: Math.max(0, Number(raw.agents) || 0),
|
||||
notificationType: String(raw.notification_type ?? ''),
|
||||
zellijSession: String(raw.zellij_session ?? ''),
|
||||
zellijPane: String(raw.zellij_pane ?? ''),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user