Skip to content

Prevent display sleep from killing SCStream during recording#27

Merged
paberr merged 1 commit into
paberr:mainfrom
what-name:fix/prevent-display-sleep
Jun 2, 2026
Merged

Prevent display sleep from killing SCStream during recording#27
paberr merged 1 commit into
paberr:mainfrom
what-name:fix/prevent-display-sleep

Conversation

@what-name

Copy link
Copy Markdown
Contributor

Summary

  • Hold an IOPMAssertion (PreventUserIdleDisplaySleep) while the ScreenCaptureKit stream is active
  • Releases the assertion on stop, and auto-releases if the process crashes

Problem

macOS tears down SCStream with error -3821 ("Stream was stopped by the system") when the display idles to sleep. This is especially aggressive on battery power — recordings can be killed after just a few minutes of no keyboard/trackpad input, even while the meeting is still happening.

Fix

Same approach used by OBS, QuickTime, and Zoom: create a power assertion when capture starts, release it when capture stops. This tells macOS "someone is actively using this capture session, don't idle-sleep the display."

The assertion is scoped to the process lifetime — if ownscribe-audio crashes or is killed, macOS automatically releases it.

Test plan

  • Start a recording on battery, leave the computer idle for 5+ minutes — recording should survive
  • Verify assertion appears in pmset -g assertions during recording
  • Verify assertion disappears after Ctrl+C stop
  • Verify normal display sleep resumes after recording ends

🤖 Generated with Claude Code

macOS tears down ScreenCaptureKit streams (error -3821) when the display
sleeps due to user idle timeout. This is especially aggressive on battery.
Hold an IOPMAssertion while capturing to prevent idle display sleep,
matching what OBS/QuickTime/Zoom do. The assertion auto-releases if the
process crashes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

Adds an IOPMAssertion to prevent macOS from idle-sleeping the display during ScreenCaptureKit recording, which otherwise causes SCStream to be torn down with error -3821.

Changes:

  • Import IOKit.pwr_mgt and add a powerAssertionID property.
  • Create a PreventUserIdleDisplaySleep assertion after stream.startCapture().
  • Release the assertion during stop/cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@paberr paberr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM

@paberr paberr merged commit f887671 into paberr:main Jun 2, 2026
2 checks passed
@what-name what-name deleted the fix/prevent-display-sleep branch June 4, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants