Skip to content

fix(recording): stop and save a macOS recording when its capture source goes away - #4240

Open
richiemcilroy wants to merge 17 commits into
mainfrom
fix/macos-capture-target-loss
Open

richiemcilroy wants to merge 17 commits into
mainfrom
fix/macos-capture-target-loss

Conversation

@richiemcilroy

@richiemcilroy richiemcilroy commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

On macOS, a recording whose capture source goes away (the display sleeps or is unplugged, the lid closes on the recorded display, the recorded window closes, sharing is stopped from the menu bar) now stops on its own and is saved like a normal Stop. Before, a Studio recording kept counting while capturing nothing, and a stream that stopped with any other error could leave Stop, Pause and Quit stuck until a force quit (#3578).

Root cause

flowchart LR
    A[SCK didStopWithError -3815] --> B[screen pipeline ends cleanly]
    B --> C[mic, camera, system audio cancelled]
    B -.Studio: Ok never reported.-> D[session still Recording, timer counts]
    B -.Instant: Ok reported.-> E[session: 'Recording ended unexpectedly', upload aborted]
    F[SCK didStopWithError, other code] --> G[capturer still marked started]
    G --> H[Stop calls stop on a dead stream, fails]
    H --> I[Stop unconfirmed, Pause and Quit refused]
Loading
  • fix(recording): end macOS capture cleanly when its source disappears #3903 made SCStreamErrorNoCaptureSource (-3815) end the screen pipeline cleanly. On macOS the Studio track watcher (spawn_watcher, studio_recording.rs) only forwards errors, so a screen track that ends with Ok before Stop never resolves the recording's done_fut. The session never learns that capture ended and keeps showing a live recording.
  • Instant does resolve done_fut with Ok, but the session treats any completion before Stop as a failure (monitor_recording_failure, session.rs), so the recording is kept as failed files and its upload is aborted.
  • For every other didStopWithError code the monitors in screen_capture/macos.rs return an error without marking the capturer stopped. Stop then calls stopStream on a stream that is already gone, gets Failed to stop a stream that is already stopped, and reports cleanup as unconfirmed: the stuck Stop/Pause/Quit state in macOS: sleep during Studio recording leaves controls in zombie state — pause/stop rejected, app refuses to quit #3578.

What changed

  • studio_recording.rs: on macOS, when the required screen track ends cleanly before Stop, the watcher resolves the recording's completion with Ok. The pipeline's stopping flag (already used on Windows and Linux) now exists on macOS too, so a Pause or Stop, which set it first, never counts as the source going away.
  • session.rs: on macOS a completion that is Ok before Stop means the source went away. The session runs the normal Stop (capture_source_lost), so Studio finalizes into an openable project and Instant finalizes output.mp4 and queues its upload. Errors still take the existing failed-recording path.
  • main_window.rs + locales: after such a stop the main window shows the same warning prompt the low-storage stop uses, titled "Recording stopped": "Cap stopped recording because what it was capturing went away, for example the display went to sleep or the window closed. Everything recorded up to then was saved." Translated in all seven catalogs.
  • screen_capture/macos.rs, closed window: a focused-window capture uses a window filter, and ScreenCaptureKit keeps that stream running after the window closes or its app quits; it just stops sending frames. The capture monitor now checks once a second whether the window server still lists the recorded window (a minimized, hidden or off-Space window is still listed). After two missed checks it reports CaptureTargetLost and ends the pipeline cleanly, so the recording is stopped and saved as above. The watcher only arms when the stream really uses a window filter (the same capturable_window check start and restart use), which only happens in the Focused window capture style and never for a window excluded from recordings, which is recorded as a display crop; the default Screen region style records a display crop that keeps capturing real pixels after the window closes, so nothing is lost there.
  • screen_capture/macos.rs: any didStopWithError marks the capturer stopped before the monitor exits (video and system audio), because the delegate is only called once the stream has stopped. A real failure is still reported as a failure, but Stop now confirms and keeps the media.

Sleep vs stop

System sleep already pauses a running Studio recording before the Mac sleeps, and stops and saves an Instant one (#3702, power.rs). That stays. Display sleep, display unplug, lid close with an external display and window close do not go through the system power notification, and afterwards the target may not exist, so the recording is stopped and saved rather than paused.

Callers checked

  • ActorHandle::done_fut (Studio): the session.rs failure monitor and apps/cli/src/record.rs finalize. The CLI now finalizes a Studio recording when its display sleeps instead of waiting for the stop trigger, as it already did for Instant.
  • Pipeline::stopping: set in Pipeline::stop before any track stops, on every platform. The Windows and Linux watcher branch is untouched.
  • Capturer::mark_stopped: only makes Capturer::stop a no-op for a stream that has already stopped.
  • sleep_action / prepare_for_sleep: unchanged.

Tests

  • macos_screen_capture_ending_before_stop_completes_the_recording, macos_pause_or_stop_does_not_complete_the_recording (studio_recording.rs).
  • a_capture_that_ends_cleanly_before_stop_is_saved_not_failed (session.rs), new strings in dialogs_surface_tests.rs.
  • a_window_the_window_server_does_not_list_counts_as_closed, only_a_window_filter_is_watched_for_its_window_closing (screen_capture/macos.rs).
  • real_app_recorded_window_closing_ends_the_recording_with_its_media (Instant reliability workflow, real app): records a TextEdit document window in Focused window style in both modes, opens it in a TextEdit instance of its own and ends only that instance (a scripted close needs Automation consent, which CI cannot give), and requires the recording to stop and save on its own with sealed media.
  • real_app_display_sleep_ends_the_recording_with_its_media (Instant reliability workflow, real app, pmset displaysleepnow) now also requires both modes to stop and save on their own (recording finished, no recording pipeline failed).

Proof

All on GitHub-hosted macOS 15 runners (Apple silicon), debug build of this branch, real ScreenCaptureKit with BlackHole as the audio device.

  • Display sleep and window close: the "Instant crash, cancel and recovery (real app)" job passes both real-app tests above in Instant and Studio mode, and the existing crash, pipeline-failure (Stop, Pause and Quit confirm) and recovery cases still pass.
  • A/V sync after a target loss vs main: a probe (not merged) records a window that flashes and clicks once a second, with system audio, in Focused window style, and runs the repo's av-sync-check on the finalized file. "Lost" closes the window mid-recording; "Stop" is a normal Stop. Offset is video minus audio, mean and standard deviation over 22 flash/click pairs; drift is the slope across the recording.
Mode Case Build Offset Drift
Instant Lost this PR -41.9 ms ± 10.1 +12.9 ms/min (r² 0.07, no trend)
Instant Stop this PR -43.5 ms ± 11.9 +1.2 ms/min
Instant Stop main -43.1 ms ± 13.3 -2.3 ms/min
Studio Lost this PR -76.6 ms ± 6.6 -10.5 ms/min (r² 0.12, no trend)
Studio Stop this PR -77.5 ms ± 7.3 +0.3 ms/min
Studio Stop main -77.4 ms ± 7.9 +4.6 ms/min

A recording finalized after its window closed is within 2 ms of main's offset in both modes, and the logs show the source-lost stop with no pipeline failure.

  • System sleep: Studio pauses before the Mac sleeps, so the part this PR touches is that a Pause must never count as the source going away. A probe pauses and resumes mid-recording in both modes on real ScreenCaptureKit: the recording is never stopped as source-lost, Studio keeps two segments, and offsets match main over two runs each (Instant -48.8 ms vs -47.2 ms on main; Studio before the pause -75.8 ms vs -77.2 ms; after the resume -105.6 ms vs -97.5 ms, which also shifts on main). A real system sleep was not run: the hosted runner refuses pmset sleepnow (error 0xe00002e2). The sleep handling itself is unchanged and covered by sleep_pauses_running_studio_and_stops_every_live_instant_recording.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the latest change fixes the early stop for excluded windows.

Summary

macOS recordings now stop and save when their capture source disappears. The latest change keeps the window-close watcher off display crops.

  • Mac recordings stop and save when their capture source goes away.
  • ScreenCaptureKit errors no longer leave Stop waiting on an ended stream.

All three previous threads are addressed. None supplied a comment number, so previousFindings is empty.

Reviews (8) · Last reviewed commit: "fix(recording): only watch for a closed ..." · Reviewed by Greptile

@sandchest-com

sandchest-com Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Sandchest

Sandchest reviewed 09bbfed: nothing exploitable found in this change.

Sandchest reviews every change for vulnerabilities an attacker could use · 2 credits · Review details

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found 2 potential issues (2 medium).

View 1 other finding in Capy.

Open in Capy

Comment thread apps/desktop-gpui/src/main_window.rs Resolved
@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
cap-status Ignored Ignored Preview Nov 16, 2026 8:52pm UTC

Comment thread apps/desktop-gpui/src/main_window.rs Resolved

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 05:45 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 05:47 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

Open in Capy

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 14, 2026 23:12 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 2 other findings in Capy.

Open in Capy

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 3 other findings in Capy.

Open in Capy

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 15, 2026 15:43 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 16, 2026 10:24 Inactive
@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai review

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

This branch was successfully deployed

1 active and 1 inactive (outdated) deployments
Preview – cap-web — 09bbfed1 Deployed Nov 16, 2026 by vercel[bot]
Preview – cap-status — 2a48b761 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant