Prevent display sleep from killing SCStream during recording#27
Merged
Conversation
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>
There was a problem hiding this comment.
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_mgtand add apowerAssertionIDproperty. - Create a
PreventUserIdleDisplaySleepassertion afterstream.startCapture(). - Release the assertion during stop/cleanup.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IOPMAssertion(PreventUserIdleDisplaySleep) while the ScreenCaptureKit stream is activeProblem
macOS tears down
SCStreamwith 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-audiocrashes or is killed, macOS automatically releases it.Test plan
pmset -g assertionsduring recording🤖 Generated with Claude Code