Skip to content

Perf: Watch the systemd-coredump directory with inotify instead of following the journal - #7155

Open
felixzsh wants to merge 12 commits into
omacom:quattrofrom
felixzsh:perf-crash-watch-memory
Open

felixzsh wants to merge 12 commits into
omacom:quattrofrom
felixzsh:perf-crash-watch-memory

Conversation

@felixzsh

@felixzsh felixzsh commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

tl;dr:
The crash watcher follows the system journal with journalctl -f, which maps journal files and grows with the journal. This PR watches the systemd-coredump directory with inotify instead, resolving crash details from one coredumpctl lookup per crash.

Problem. journalctl -f opens and maps every journal file it can find, so the watcher's footprint grows with the journal. I once observed the watcher/journalctl reach roughly 400 MB, but I could not reproduce it, isolate a cause, or measure it methodically, so I cannot stand behind that number. The "1.3 GB journal" and "close to a gigabyte of page cache" figures I originally gave were worst-case estimates, not measurements, and are removed here.

Change. systemd-coredump already writes one core file per crash, named core.comm.uid.bootid.pid.timestamp.zst. The watcher now watches that folder with inotify instead: a few MB idle, and nothing retained between crashes. The program name, executable, and signal shown in the notification come from a coredumpctl lookup per crash, matched by pid and boot id so a reused pid can never point at the wrong crash. systemd-coredump writes the core file before it journals the entry, so the lookup retries briefly (up to two seconds) to let the entry land; the toast only falls back to the truncated comm and an unknown signal when the entry truly never appears, and clicking it always runs a full diagnosis from coredumpctl regardless.

No behavior changes. The same critical notification with the click-to-diagnose action, the per-minute dedupe for crash loops, the uid filter, ignore patterns, and self-exclusion all work exactly as before. Clicking the notification still opens the AI diagnosis, which reads the crash details itself. A restart only misses crashes that happened while it was down, just like before. inotify-tools was already an Omarchy dependency.

Testing. A new crash-watch-test.sh covers the notification contents, the uid and ignore filters, self-exclusion, dedupe, and the lookup-failure fallback, using mocked inotifywait, coredumpctl, and notification scripts.

Copilot AI balanced review requested due to automatic review settings August 16, 2026 16:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR rewrites omarchy-crash-watch to watch the systemd-coredump directory via inotify (instead of following the journal), aiming to reduce idle memory usage while preserving crash notifications and dedupe behavior.

Changes:

  • Switch crash detection from journalctl -f to inotifywait on /var/lib/systemd/coredump with filename parsing + coredumpctl lookup.
  • Add a shell-based test harness that stubs inotifywait, coredumpctl, and notification commands to validate notification contents + dedupe/ignore behavior.
  • Add a migration to restart the crash watcher so the rewrite takes effect immediately after update; update docs accordingly.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/shell.d/crash-watch-test.sh Adds end-to-end-ish shell tests for the rewritten crash watcher using stubbed helpers.
migrations/1786894219.sh Restarts the user crash-watch service so the new inotify-based watcher is used right after upgrade.
docs/notifications.md Updates documentation to reflect the new inotify-based implementation and memory characteristics.
bin/omarchy-crash-watch Replaces journal tailing with inotify directory watching and transient coredumpctl lookups.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread test/shell.d/crash-watch-test.sh Resolved
Copilot AI review requested due to automatic review settings August 16, 2026 16:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (2)

test/shell.d/crash-watch-test.sh:21

  • This stub expands $WATCH_ITEMS unquoted, so it performs word-splitting and glob expansion. That can make the test diverge from real inotifywait behavior (one filename per line) and can break if an item contains glob characters or spaces. Prefer passing items newline-separated and printing with printf '%s\n' \"$WATCH_ITEMS\" (or otherwise ensure the stub emits exactly one item per line without word-splitting).
cat >"$TMPDIR/bin/inotifywait" <<'SH'
#!/bin/bash
printf '%s\n' $WATCH_ITEMS
SH

migrations/1786894219.sh:17

  • This migration script uses bash-specific [[ ... ]] syntax but has no shebang. If the migration runner executes scripts via /bin/sh (or a non-bash shell), this can fail. Add an explicit #!/bin/bash (and, if consistent with other migrations in this repo, consider set -euo pipefail) to make the interpreter and error-handling behavior unambiguous.
echo "Restart the crash watcher so it picks up the inotify rewrite"

# omarchy-crash-watch followed the journal with journalctl -f, mapping the
# whole journal into memory; the rewrite watches the systemd-coredump
# directory with inotify instead, at a few MB. pacman replaces the script on
# update, but the running service keeps the old one in memory until it
# restarts, so restart it here for the memory win to land during the update
# instead of at the next login.

# The unit's ConditionPathExists re-checks the same toggle flag at every
# start, so a watcher the user disabled stays off: honoring the flag here
# makes this migration a no-op for them. Only nudge a watcher that is
# actually running; a stopped one stays stopped.
[[ -f "$HOME/.local/state/omarchy/toggles/crash-capture-off" ]] && exit 0

systemctl --user is-active omarchy-crash-watch.service >/dev/null 2>&1 &&
  systemctl --user restart omarchy-crash-watch.service >/dev/null 2>&1 || true

Copilot AI review requested due to automatic review settings August 16, 2026 16:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 16, 2026 16:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

Comment thread migrations/1786894219.sh Resolved
Comment thread migrations/1786894219.sh Resolved
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at cf518e1, on a disposable Omarchy VM, with real crashes and the suites run there. This needs a few changes before it can land, and the memory numbers need another look.

Tests. test/shell.d/crash-capture-test.sh passes 33/33 on quattro but hangs on this branch, alone and merged into quattro. It still drives the watcher through a stubbed journalctl, so the watcher now blocks forever on a real inotifywait -m. Its per-program mute, hostile-name and empty-comm coverage needs porting to the inotifywait/coredumpctl stubs rather than dropping. The new crash-watch-test.sh fails too (1 passed, 1 failed): the self-exclusion case at line 109 gives omarchy-agent-foo pid 1002, which the coredumpctl stub answers with /usr/bin/other-real. The crash is announced as other-real, so it gets two toasts. The fixture needs its own pid.

Crashes dropped or misread:

  • A process whose comm is empty dumps as core..1000.<boot>.<pid>.<ts>.zst. The (.+) in the filename regex (bin/omarchy-crash-watch:64) rejects it, so the crash is skipped silently. I crashed one on the VM: quattro announced it, this branch posted nothing. (.*) keeps it.
  • When coredumpctl has a signal but no executable, awk prints \tABRT. Tab is IFS whitespace, so read -r exe signal sets exe=ABRT and leaves the signal empty. The diagnosis then gets a binary called ABRT, and the retry stops early. This is the trap the removed comment in the old watcher describes; a placeholder for empty fields avoids it.
  • The signal now reaches omarchy-agent-crash as ABRT where quattro passes SIGABRT. It only appears in the diagnosis prompt, but it is a behaviour change.
  • When the lookup falls back, the name is the comm as systemd escaped it for the filename (.hidden becomes \x2ehidden, foo bar becomes foo\x20bar), truncated to 15 characters. A mute or OMARCHY_CRASH_IGNORE pattern keyed on the real name won't match it.

The memory claim didn't reproduce at the stated size. With a 1 GB journal on the VM, the current watcher's service used 24 MB, and its journalctl held 10 MB RSS. That was 14 MB after following another 400 MB of writes. A desktop that has run it for a week, with a 452 MB journal, shows 40 MB RSS, 39 MB of it file-backed and reclaimable. This branch idles at about 1 MB, a real gain. But each crash's coredumpctl lookups charged 25–94 MB of page cache to the service. Could you say how you measured the 200–400 MB and the ~1 GB of cache: journal size, how many files are in /var/log/journal/*/, and whether that was VmRSS, RssAnon or the unit's MemoryCurrent? That decides whether the trade is worth it.

What still works: Storage=none crashes are announced; large (1.4 GB) cores are announced within a second, with full details; foreign-uid crashes are skipped; and dedupe holds. ./test/cli passes.

Codex Medium reviewed it as a second opinion. It agreed on the test failures and the empty-comm drop, and found the executable/signal field shift, which I then checked in the source. Its independence from this review is not guaranteed.

Also for the maintainer: #11569, #11782 and #13161 edit the same watcher, and #11782 adds a different test/shell.d/crash-watch-test.sh, so whichever lands second will conflict.

This is waiting on the author for the fixes above and the measurement details.

@felixzsh
felixzsh force-pushed the perf-crash-watch-memory branch from cf518e1 to bb49ebd Compare October 10, 2026 17:26
@greptile-apps

greptile-apps Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; both numbered findings are fixed.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; both numbered findings are fixed.

Summary

The PR replaces crash journal events with short-lived markers from systemd-coredump. The watcher reads each marker with inotify, then looks up the matching journal entry.

  • The crash watcher gets coredump events from temporary markers and looks up their details.

Both numbered findings are fixed. No new actionable issues were found.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[systemd-coredump finishes] --> B[Create and remove invocation marker]
  B --> C[inotify event]
  C --> D[Look up matching journal entry]
  D --> E[Check user, mute, and duplicate rules]
  F[OOM journal stream] --> G[Unbuffered event reader]
  G --> H[Update OOM grace window]
  H --> I[OOM notification]
  E --> J[Crash notification]
Loading

Reviews (7) · Last reviewed commit: "fix(migration): wait for the package's f..." · Reviewed by Greptile

Comment thread bin/omarchy-crash-watch Resolved
Comment thread test/shell.d/crash-watch-test.sh Resolved
Comment thread bin/omarchy-crash-watch Resolved
Comment thread bin/omarchy-crash-watch Resolved
Comment thread bin/omarchy-crash-watch Resolved
Comment thread migrations/1786894219.sh Resolved
Comment thread test/shell.d/crash-watch-test.sh Resolved
@felixzsh felixzsh changed the title Perf: Reduce the crash watcher's memory footprint by ~90% Perf: Watch the systemd-coredump directory with inotify instead of following the journal Oct 10, 2026
@felixzsh

Copy link
Copy Markdown
Contributor Author

On the measurement questions: the figures were not measured.

The ~400 MB was a single observation of the running watcher/journalctl, with no root cause identified and never reproduced since. The "1.3 GB journal" and "close to a gigabyte of page cache" were worst-case estimates, not measurements, and I have no data behind them.

I have just updated the PR title and description to drop both claims and to state only what is verifiable, so the memory numbers no longer overstate the case.

If settling the trade needs real numbers, I am happy to measure with your approach (RssAnon/RssFile, or the unit's memory.stat) rather than a total that folds reclaimable page cache into one figure.

Comment thread test/shell.d/crash-watch-test.sh Resolved
@omarchybot omarchybot added enhancement New feature or request and removed bug Something isn't working labels Oct 10, 2026
@felixzsh
felixzsh force-pushed the perf-crash-watch-memory branch from 900443b to 84878e4 Compare November 1, 2026 16:50
Comment thread test/shell.d/crash-watch-test.sh Resolved
Comment thread test/shell.d/crash-watch-test.sh Resolved
Comment thread docs/notifications.md Resolved
journalctl -f mapped the whole journal into memory (~200MB RSS with a 1.3GB
journal): it opens every journal file, archived or active. The crash watcher
only needs the four fields systemd-coredump already bakes into each core
file's name (comm, uid, boot id, pid), so watch /var/lib/systemd/coredump
with inotify instead — a few MB idle, and nothing retained between crashes.

The name, executable, and signal for the toast come from one transient
coredumpctl lookup per crash, matched by pid and boot id so a reused pid
cannot resolve to the wrong crash. A failed lookup still announces, just
with the truncated kernel comm and an unknown signal, and the diagnosis
path is untouched: clicking the toast still runs omarchy-agent-crash, which
re-derives everything from coredumpctl at click time.

Behavior is preserved: the uid filter, ignore pattern, self-exclusion, and
the per-minute dedupe window all work as before, and a restart misses only
the crashes that happened while it was down, exactly like journalctl -n 0
never replayed old entries. inotify-tools was already a base dependency.
felixzsh and others added 9 commits November 28, 2026 11:20
pacman replaces /usr/bin/omarchy-crash-watch on update, but the running
service keeps the old script in memory until it restarts, so the memory win
from the inotify rewrite would otherwise wait for the next login. A migration
restarts the watcher during omarchy update instead.

It only nudges a watcher that is actually running, and skips one the user
disabled: the unit re-checks the same toggle flag at every start, so honoring
it here makes the migration a no-op for them.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The stub expanded $WATCH_ITEMS unquoted to turn the space-separated list
into one filename per line, which also glob-expands any glob character in
a name. Split on spaces with tr instead so the mock only ever emits the
names it was given.
systemd-coredump writes the core file before it journals the COREDUMP_*
entry, so the watcher could fire on the file and miss the entry by a beat,
falling back to the truncated comm and an unknown signal. Retry the lookup
for up to two seconds before falling back, and only notify with less detail
when the entry truly never lands.

The test now covers a crash whose first two lookups fail and whose details
arrive on the third.
Match an empty comm in the core filename so a crash whose process never
set a name is still announced, and read the executable and signal on
separate lines so a missing executable cannot shift the signal into its
place. Give the self-exclusion fixture its own pid and cover both cases.
A requested restart is no longer masked with `|| true`, so a failure
leaves the migration unmarked and omarchy-migrate retries it instead of
recording a success the watcher did not get.
Emit ephemeral root-owned events from systemd-coredump ExecStopPost so
notifications do not depend on retaining an external core. Resolve each
event with a short-lived JSON journal query matched by InvocationID,
avoiding reused PIDs, escaped filenames, and shifted empty fields.

Port the existing crash capture fixtures to the event source, preserve
mute and hostile-name coverage, and verify bounded retries, real inotify
marker delivery, unit syntax, and migration failure/retry behavior.
Keep mocked and static checks unconditional. Skip native inotify and
systemd verification independently when their host dependencies are
missing, while preserving failures when available checks are broken.
Fold the crash-watch-test.sh assertions into crash-capture-test.sh, the
existing suite for bin/omarchy-crash-watch, and drop the greps that restated
the drop-in's contents instead of exercising it.

Delete crash-watch-migration-test.sh: a migration is frozen once it ships, so
its behaviour is tested where it lives, not in a suite of its own.

Unwrap the crash capture paragraph in docs/notifications.md onto one line.
@felixzsh
felixzsh force-pushed the perf-crash-watch-memory branch from 24d1d97 to 380889c Compare November 28, 2026 17:22
Comment thread bin/omarchy-crash-watch Resolved
Comment thread migrations/1786894219.sh Resolved
jq buffers its output into a pipe, and the OOM stream never ends, so a
single kill stayed in the buffer until it filled or the watcher died: the
user got no toast, and the OOM grace window never opened to suppress the
crash that follows.

--unbuffered makes jq write each event as it arrives. The test that comes
with it holds the journal stream open until the toast has been seen, which
the buffered version never manages.
…pdate

A dev-linked checkout runs ahead of its packages, where neither the
tmpfiles rule nor the coredump drop-in is installed under /etc yet, and an
unguarded systemd-tmpfiles call then failed the whole update.

Exit 75 with the paths it waits for, as the migration guide asks: the
migration stays pending and the next update applies it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants