Skip to content

fix(capture): single-flight screen recording stop and skip empty (#11508) - #11520

Open
ansonboby wants to merge 6 commits into
omacom:quattrofrom
ansonboby:fix/11508-screenrecording-double-stop
Open

ansonboby wants to merge 6 commits into
omacom:quattrofrom
ansonboby:fix/11508-screenrecording-double-stop

Conversation

@ansonboby

Copy link
Copy Markdown

Fixes #11508

Two concurrent stop_screenrecording calls (slow encoder drain 2-3s at 4K) both read /tmp/omarchy-screenrecord-filename before either removes it, posting duplicate "Screen recording saved" toasts and racing over -preview.png" (broken thumbnail). If second starts after first removed file, it reads empty name and posts -preview.png".

  • bin/omarchy-capture-screenrecording:204 — add flock -n /tmp/omarchy-screenrecord.lock at top of stop_screenrecording to make it single-flight (suggested exec 9>...; flock -n 9 || return 0)
  • Check for empty filename after reading RECORDING_FILE and skip duplicate finalize/notification
  • Ensure lock fd is closed on all exit paths

Repro: omarchy-capture-screenrecording --fullscreen --resolution=3840x2160; sleep 5; stop & sleep 0.4; stop no longer posts two toasts.

…com#11508)

Two concurrent stops (double Alt+Print) both read /tmp/omarchy-screenrecord-filename
before either removes it, posting duplicate toasts and racing over the preview.
If second starts after first removes file, it reads empty name and posts
broken thumbnail "-preview.png".

- Add flock -n on /tmp/omarchy-screenrecord.lock at top of stop_screenrecording
- Check for empty filename after reading RECORDING_FILE and skip duplicate
  finalize/notification
- Ensure lock fd is closed on all exit paths
…com#11508)

Static regression suite for the stop race: the flock guard runs before the
recorder is signaled, losing the race returns quietly, an empty recording
filename skips finalize and the saved toast, and the lock releases on every
exit path including the RETURN trap. Asserted via source pins the way the
other capture tests do, since the race depends on SIGKILL timing between
the panel teardown and the EXIT trap that a harness cannot reproduce
meaningfully. Verified the suite fails against packaged 4.0.3, which lacks
the guard.
@ansonboby

Copy link
Copy Markdown
Author

Added a regression suite pinning the stop mechanics (test/shell.d/screenrecording-stop-test.sh): flock guard precedes the recorder signal, losing the lock race returns quietly, empty recording filename skips finalize + toast, and the lock releases on every exit path (explicit releases + RETURN trap). Static source pins in the style of the existing capture tests, since the race itself depends on SIGKILL timing a harness can't reproduce. Verified the suite fails against packaged 4.0.3, which lacks the guard.

@ansonboby

Copy link
Copy Markdown
Author

Status check — this one has no review yet, so flagging that it's waiting rather than stuck.

Two files: bin/omarchy-capture-screenrecording and test/shell.d/screenrecording-stop-test.sh. Re-verified against current quattro just now — screenrecording-stop-test.sh 6/6, PR MERGEABLE/CLEAN, no failing checks.

What the suite pins: the stop is single-flight via flock and returns quietly when the lock is held; a consumed recording skips the finalize and toast; the lock is released on every exit path including early return, and it's taken before the recorder is signalled — which is the ordering the double-stop report needed.

Happy to split it, reshape it, or add anything if the approach isn't what you'd want.

omarchybot and others added 4 commits October 1, 2026 01:27
A fixed name in /tmp is one any other local account can create first, and a lock file this user cannot open makes flock fail, so every stop returns quietly and the recording cannot be stopped. The per-user runtime directory is where omacom#8374 moved the rest of the recorder's state for the same reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only start creates RUNTIME_DIR. If stop runs where it is missing, the redirect fails, flock fails on the closed descriptor, and stop returns as if it had lost the race, so the recorder is never signalled and the recording cannot be stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Codex Medium <noreply@openai.com>
The lock was held through finalize_recording, which re-encodes or normalizes audio and can run for a long time after the recorder has exited and the indicator has cleared. A recording started in that window could not be stopped: its stop lost the lock and returned quietly. The final rm could also delete that new recording's state file.

Stop now reads and removes the recording file, releases the lock, and finalizes the name it read, so finalize_recording takes the path as an argument instead of re-reading shared state. Releasing before the preview cleanup is forked also keeps that background sleep from inheriting the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Codex Medium <noreply@openai.com>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Sep 30, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and verified on a disposable Omarchy worker, with a second opinion from Codex Medium. The race in #11508 is real and taking a lock before signalling the recorder is the right fix. I pushed four commits to this branch to fix three problems with the lock itself, and brought the branch up to date with quattro.

Reproduction. On the worker I stubbed a recorder that takes 1.5s to drain after SIGINT and ran two --stop-recording calls 0.4s apart. Current quattro posts two "Screen recording saved" toasts for one file, and this branch posts one. Stopping a second recording while the first is still being finalized makes quattro post a toast whose image is -preview.png, which is the broken thumbnail from the issue. This branch posts both toasts with the right images.

What I pushed

  • A merge of quattro. [Security] Keep screen-recording state out of world-writable /tmp #8374 moved the recorder's state from /tmp into $RUNTIME_DIR, and the fix below needs that change.
  • 80c6e00 moves the lock from /tmp/omarchy-screenrecord.lock to $RUNTIME_DIR. Any local account can create a fixed name in /tmp first. When it does, exec 9> fails, flock fails, and every stop returns quietly, so the recording can't be stopped. The worker showed exactly that until the lock moved.
  • f112b4c creates $RUNTIME_DIR before opening the lock, for the same reason. Only start creates that directory, and without it the lock fails and stop becomes a silent no-op.
  • 35a0ba6 holds the lock only until the recording is claimed. Stop now reads and removes the recording file, releases the lock, and finalizes the name it read, so finalize_recording takes the path as an argument. Before this, the lock was held through the re-encode and loudness pass, after the indicator had already cleared. A recording started in that window couldn't be stopped, and the final rm could delete the new recording's state file. This also stops the backgrounded preview cleanup from inheriting the lock for its two-second sleep.

Codex Medium found the last two problems. Its final round on 35a0ba6 found nothing left open.

Tests on the worker. screenrecording-stop-test.sh 6/6 and screenrecording-test.sh 26/26. ./test/cli stops at vscode generated theme references current theme file, which fails the same way on current quattro and is unrelated to this change.

Notes for the maintainer.

Waiting on the maintainer. ready is held back only because ./test/cli isn't green on quattro.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Screen recording stop logic adds concurrency control and file handling.

The PR should not merge until a failure to open the new lock is handled without silently leaving a recording active.

Findings

  1. P1 Lock failure prevents stopping ▶
  2. P2 Concurrent stops remain untested ▶

Summary

The PR adds a nonblocking stop lock, consumes the recording filename before finalization, skips empty filenames, and adds a source-level regression test.

  • The stop path needs explicit handling when its new lock file cannot be opened.
  • The test does not exercise the concurrent behavior it is intended to protect.

Reviews (1) · Last reviewed commit: "Hold the stop lock only until the record..."

Comment on lines +221 to +223
mkdir -p "$RUNTIME_DIR"
exec 9>"$RUNTIME_DIR/omarchy-screenrecord.lock"
if ! flock -n 9; then

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.

P1 Lock failure prevents stopping If the runtime directory cannot be created or the lock file cannot be opened—for example, if its permissions change while a recording is active—exec 9> exits the shell before pkill -SIGINT runs. The recording keeps running instead of stopping. Handle directory creation and lock-open failures explicitly.

Comment on lines +9 to +12
# broken "-preview.png" thumbnail. The race involves SIGKILL timing between the
# panel's Process teardown and the script's EXIT trap, which cannot be asserted
# meaningfully from a test harness, so this suite pins the mechanics statically
# the way the other capture tests assert source contents: the flock guard, the

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.

P2 Concurrent stops remain untested The new test checks the script’s text but never runs two overlapping stops. It can pass even if the lock no longer prevents duplicate finalization and notifications. A test with a controllable recorder and competing stop calls would protect the behavior this PR is meant to fix.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 10, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed now that this branch conflicts with quattro. Current quattro already fixes #11508, so this branch shouldn't be ported. I recommend closing it. Codex Medium reviewed the same question and agreed. Independence isn't guaranteed, because the reviewer can read this session.

Why it conflicts. e1b0e5e (#14431) rewrote bin/omarchy-capture-screenrecording. The recorder is now found through omarchy-capture-screenrecording-process rather than pgrep/pkill, and the whole script runs under flock on $RUNTIME_DIR/omarchy-screenrecord.lock. That lock is held from the activity check through the stop, the finalize, the toast and removing the recording file. A second stop now waits its turn, finds no recorder and exits. The lines this pull request changes are gone.

Reproduction, on a disposable Omarchy worker. I used the same stub as last time: a recorder that keeps draining after SIGINT, then two invocations 0.4s apart. I ran it with drains of 1.5s and 0.5s, both on each side of the new flock -w 1, and with a plain toggle as the second press:

  • the base this branch was built on (8b4eae6) posts two "Screen recording saved" toasts every time, one of them with the -preview.png image from the issue
  • current quattro (04b374a) posts one toast in every case, with the right image, and never force-kills the recorder
  • this branch's head (35a0ba6) also posts one

Don't port it by hand either. On quattro, fd 9 already holds the script's lock. This branch's exec 9>… inside stop_screenrecording would replace that descriptor and release the lock before taking it again, so another invocation could get in. Codex Medium raised this.

A smaller case is still open on quattro. Codex Medium also found this one. It only arises when a recorder is running that this script didn't start, so there's no saved pid and no recording file. Then select_recorder matches any of the user's recorders. Stop stops it and still posts "Screen recording saved" with an empty filename, at bin/omarchy-capture-screenrecording:428-439. I confirmed this by reading the code, not on a worker. This branch's empty-filename guard would cover it, but as its own small change against the current script.

Waiting on the maintainer: close this pull request and #11508 as fixed by #14431, or ask for the empty-filename guard separately.

@omarchybot

Copy link
Copy Markdown
Collaborator

#15073 has merged, and it covers the case my last comment left open. When there is no saved recording, for example after stopping a recorder this script didn't start, stop no longer posts "Screen recording saved" with an empty name and a -preview.png image. Combined with the script-wide flock from #14431, current quattro (56a910b) now covers what this pull request fixes for #11508.

I compared this branch with bin/omarchy-capture-screenrecording on current quattro:

  • Empty filename guard. Announce no saved recording when stop knows of none #15073 already covers it, at bin/omarchy-capture-screenrecording:428-430.
  • Lock in stop_screenrecording. Stops are already serialised by the outer lock, at lines 561-617. On quattro, this branch's exec 9>… would replace the descriptor that holds that lock, so it can't be carried over as written.
  • Releasing the lock before finalize, and passing the filename to finalize_recording. These only support this branch's own lock.
  • The new test. It checks this branch's source text.

One behaviour is still different. When a second --stop-recording overlaps the first, this branch returns success immediately. quattro waits until the first stop has finalized and posted its toast, then exits 1 because nothing is left to stop. Codex Medium found this, and I confirmed it at lines 576-592. It doesn't bring back the duplicate toast. Keeping that behaviour would mean changing the outer lock policy, not merging this branch, which now conflicts with quattro.

Opus 5.5 and Codex Medium reviewed this against current quattro. Codex Medium agreed with the rest, and its independence isn't guaranteed because it can read this session. The close-as-obsolete check, which compares only the two diffs, did not agree that this pull request is obsolete, so it stays open.

Waiting on the maintainer: close this as fixed by #14431 and #15073, or ask for an immediate, successful return from an overlapping stop as a separate change to the outer lock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Screen recording: stopping twice posts two "saved" toasts and a broken thumbnail

2 participants