Recordly icon indicating copy to clipboard operation
Recordly copied to clipboard

[Bug]: macOS window recording can leak stalled recordly-window-list helpers and exhaust system memory

Open borkasm opened this issue 4 months ago • 2 comments

Summary

On macOS, Recordly can launch many overlapping recordly-window-list helper processes while stopping a native window recording. If SCShareableContent stalls during ScreenCaptureKit teardown, those helpers can remain alive and consume enough memory/CPU to make the entire machine unresponsive.

This report is based on a macOS force-reset stackshot correlated with the v1.3.3 source. Raw diagnostics are intentionally not attached because they contain private system data.

Environment

  • Recordly 1.3.3
  • macOS 26.5.1
  • Apple Silicon (M1 Pro), 16 GB RAM
  • Native ScreenCaptureKit recording path

Observed behavior

  • Recording stopped and the output was saved.
  • During a single 20.3-second interval around recording finalization, 26 recordly-window-list processes entered the same wait state.
  • The captured main-thread frame symbolicated to main + 432 in recordly-window-list, consistent with the helper's DispatchGroup.wait().
  • Every helper was waiting on a spinlock. The helpers remained alive after Recordly was no longer recording.
  • Each helper showed approximately 2.6 GB of resident mappings. This likely includes shared ScreenCaptureKit/window-surface mappings, so the sum is not necessarily unique physical memory.
  • The kernel later reported 100% of compressed pages limit (BAD) and 31 swapfiles. The machine became unresponsive and required a forced power-button reset.
  • There was no preceding spontaneous kernel panic or sleep/wake failure report.

Source-level cause

The behavior appears to come from several lifecycle and concurrency issues acting together:

  1. electron/ipc/cursor/bounds.ts starts a fire-and-forget bounds refresh every 250 ms:

    void refreshSelectedWindowBounds();
    setWindowBoundsCaptureInterval(setInterval(() => {
      void refreshSelectedWindowBounds();
    }, 250));
    

    There is no in-flight guard, so a slow or stalled refresh does not prevent another refresh from launching.

  2. getNativeMacWindowSources() starts a new recordly-window-list process for every cache miss. The cache is only populated after the helper completes, and there is no shared in-flight promise.

  3. electron/native/ScreenCaptureKitWindowList.swift calls:

    try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)
    

    and the main thread waits on a DispatchGroup without an internal timeout. The source itself notes that this helper can stall sporadically when run as a standalone process.

  4. In src/hooks/useScreenRecorder.ts, the renderer waits for native recording shutdown before disabling recording state:

    const result = await window.electronAPI.stopNativeScreenRecording();
    await window.electronAPI?.setRecordingState(false);
    

    Consequently, the 250 ms bounds poll continues during SCStream.stopCapture() and asset-writer finalization, when ScreenCaptureKit is already tearing down the active stream.

  5. stopWindowBoundsCapture() only clears future interval callbacks. It cannot cancel helpers that are already running.

  6. The Node execFile timeout has no explicit SIGKILL escalation or child-process registry, and the app's before-quit cleanup does not terminate window-list helpers.

  7. The native recorder separately performs another full SCShareableContent enumeration every 500 ms to validate that the selected window still exists, adding more ScreenCaptureKit contention.

The critical bounds/helper code is unchanged on current main, so the issue does not appear fixed after v1.3.3.

Likely failure sequence

  1. A window recording is active and bounds polling runs every 250 ms.
  2. The user stops the recording.
  3. Native ScreenCaptureKit teardown begins, but bounds polling remains active.
  4. One window-list helper stalls in SCShareableContent.
  5. Additional interval ticks launch more helpers because there is no single-flight guard.
  6. Recording shutdown eventually clears the interval, but already-running helpers remain.
  7. The leaked helpers retain substantial ScreenCaptureKit/window resources and eventually exhaust compressed memory and swap.

Suggested fixes

  • Call setRecordingState(false) before awaiting stopNativeScreenRecording().
  • Replace the 250 ms setInterval with a serialized/recursive refresh that cannot overlap.
  • Add a shared in-flight promise to getNativeMacWindowSources() and set the cache timestamp after successful completion.
  • Track all window-list child processes and terminate them on recording stop and app quit.
  • Escalate timed-out helpers to SIGKILL after a short grace period.
  • Add an internal watchdog to the Swift helper so DispatchGroup.wait() cannot block indefinitely.
  • Consider a persistent or window-specific bounds API instead of enumerating all shareable content four times per second.
  • Add a regression test with a never-resolving helper and assert that at most one helper exists and that it is cleaned up on stop/quit.

Related symptoms may overlap with #700 and #673, but neither issue documents the orphaned helper/process-exhaustion mechanism.

borkasm avatar Jul 10 '26 18:07 borkasm

我也遇到了同样的问题,CPU 占用高达 150%。

以下是 Agent 的调研结果:


Additional confirmation on macOS 27 / Apple Silicon — sustained replayd / WindowServer CPU exhaustion from orphaned helpers (not only memory)

I hit the exact same mechanism reported here, and I can add a distinct, easily reproducible symptom: sustained replayd / WindowServer CPU burn, not just memory pressure.

Environment

  • Recordly 1.3.3 (/Applications/Recordly.app, bundle dev.recordly.app)
  • macOS 27.0 (26A428), Apple Silicon
  • Native ScreenCaptureKit window recording path
  • The Recordly main app was not running at the time of observation — only the leaked helpers remained

What I observed

  • After stopping a window recording, 31 orphaned recordly-window-list processes were left behind, all reparented to launchd (PPID = 1) because their parent had already exited.

  • They stayed alive for 16+ hours, each spinning ~1.5–6% CPU, repeatedly re-enumerating shareable content.

  • replayd was pinned at ~147–150% CPU (≈1547 minutes of accumulated CPU time), and WindowServer at ~90% CPU. The constant XPC traffic from the helpers is the trigger.

  • sample replayd showed the hot path was: com.apple.NSXPCListener.service.com.apple.replayd → +[NSXPCInterface interfaceWithProtocol:] → setProtocolMetadata → +[NSMethodSignature signatureWithObjCTypes:]

    i.e. replayd was rebuilding XPC interface metadata for a never-ending stream of new connections. The sample contains ~30 distinct com.apple.NSXPCConnection.user.com.apple.replayd.<pid> serial queues, each PID matching a leaked recordly-window-list.

  • log show --predicate 'process == "replayd"' was flooded with RPConnectionManager ... accepted client connection PID: <pid> and -[SLContentFilter initWithDisplay:...includedWindows:] entries.

Workaround that immediately fixes it

Killing the orphaned helpers drops replayd back to 0.0% CPU instantly:

ps -Ao pid=,command= | grep "app.asar.unpacked/electron/native/bin/darwin-arm64/recordly-window-list" | grep -v grep | awk '{print $1}' | xargs -r kill -9

Still present on the latest release

I checked the v1.4.0 tag and the critical code is unchanged, so this is not fixed:

  • electron/ipc/cursor/bounds.ts still fires an unguarded 250 ms interval with no in-flight guard.
  • getNativeMacWindowSources() still spawns a new recordly-window-list per cache miss with no shared in-flight promise.
  • execFileAsync(..., { timeout: 30000 }) still has no SIGKILL escalation and no child-process registry, and before-quit still does not reap window-list helpers.

None of the suggested fixes in this report appear to be implemented, and I could not find any PR addressing it either.

Suggested additions

  • Please also track the CPU side (replayd / WindowServer), not just compressed-memory exhaustion — it makes the machine hot and drains battery even on 16 GB+ machines.
  • A regression test with a never-resolving helper that asserts zero leftover helpers after stop/quit would cover this well.

中文摘要

我遇到了完全相同的机制,并补充一个很直观的独立症状:replayd / WindowServer 持续高 CPU,不只是内存压力。

  • 环境:Recordly 1.3.3,macOS 27.0 (26A428),Apple Silicon,原生 ScreenCaptureKit 窗口录制。
  • 现象:停止窗口录制后残留 31 个孤儿 recordly-window-list 进程,父进程已退出、全部被 launchd 接管(PPID = 1),存活 16 小时以上,每个占用约 1.5–6% CPU;replayd 被顶到 约 147–150% CPU,WindowServer 约 90% CPU。
  • 证据:sample replayd 热点栈为 com.apple.NSXPCListener.service.com.apple.replayd → +[NSXPCInterface interfaceWithProtocol:] → +[NSMethodSignature signatureWithObjCTypes:],即 replayd 在为不断新建的 XPC 连接反复重建接口元数据;采样中约 30 个 com.apple.NSXPCConnection.user.com.apple.replayd.<pid> 串行队列与泄漏的 helper 一一对应。日志被 RPConnectionManager ... accepted client connection PID 和 -[SLContentFilter initWithDisplay:...includedWindows:] 刷屏。
  • 临时解法:kill -9 掉全部孤儿 helper 后,replayd 立刻回落到 0.0% CPU。
  • 仍未修复:检查 v1.4.0 tag,electron/ipc/cursor/bounds.ts 仍是 250 ms 无防重入 interval,getNativeMacWindowSources() 仍无共享 in-flight promise,execFileAsync 仍无 SIGKILL 升级与子进程登记,before-quit 仍不回收 helper;本报告建议的修复未见落地,也未找到相关 PR。
  • 建议:请把 CPU 侧(replayd / WindowServer)一并纳入跟踪;并增加「helper 永不返回」的回归测试,断言 stop/quit 后残留 helper 数为 0。

ohmyangboy avatar Sep 14 '26 06:09 ohmyangboy

I just experienced this same issue even after updating to the most recent version which is Version 1.4.0 (1.4.0). The problem still persist

Screenshot: Image

System specs: Apple M4 Pro MacOs Tahoe 26.6.2

Summary

On macOS, Recordly can launch many overlapping recordly-window-list helper processes while stopping a native window recording. If SCShareableContent stalls during ScreenCaptureKit teardown, those helpers can remain alive and consume enough memory/CPU to make the entire machine unresponsive.

This report is based on a macOS force-reset stackshot correlated with the v1.3.3 source. Raw diagnostics are intentionally not attached because they contain private system data.

Environment

  • Recordly 1.3.3
  • macOS 26.5.1
  • Apple Silicon (M1 Pro), 16 GB RAM
  • Native ScreenCaptureKit recording path

Observed behavior

  • Recording stopped and the output was saved.
  • During a single 20.3-second interval around recording finalization, 26 recordly-window-list processes entered the same wait state.
  • The captured main-thread frame symbolicated to main + 432 in recordly-window-list, consistent with the helper's DispatchGroup.wait().
  • Every helper was waiting on a spinlock. The helpers remained alive after Recordly was no longer recording.
  • Each helper showed approximately 2.6 GB of resident mappings. This likely includes shared ScreenCaptureKit/window-surface mappings, so the sum is not necessarily unique physical memory.
  • The kernel later reported 100% of compressed pages limit (BAD) and 31 swapfiles. The machine became unresponsive and required a forced power-button reset.
  • There was no preceding spontaneous kernel panic or sleep/wake failure report.

Source-level cause

The behavior appears to come from several lifecycle and concurrency issues acting together:

  1. electron/ipc/cursor/bounds.ts starts a fire-and-forget bounds refresh every 250 ms: void refreshSelectedWindowBounds(); setWindowBoundsCaptureInterval(setInterval(() => { void refreshSelectedWindowBounds(); }, 250));

    There is no in-flight guard, so a slow or stalled refresh does not prevent another refresh from launching.

  2. getNativeMacWindowSources() starts a new recordly-window-list process for every cache miss. The cache is only populated after the helper completes, and there is no shared in-flight promise.

  3. electron/native/ScreenCaptureKitWindowList.swift calls: try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)

    and the main thread waits on a DispatchGroup without an internal timeout. The source itself notes that this helper can stall sporadically when run as a standalone process.

  4. In src/hooks/useScreenRecorder.ts, the renderer waits for native recording shutdown before disabling recording state: const result = await window.electronAPI.stopNativeScreenRecording(); await window.electronAPI?.setRecordingState(false);

    Consequently, the 250 ms bounds poll continues during SCStream.stopCapture() and asset-writer finalization, when ScreenCaptureKit is already tearing down the active stream.

  5. stopWindowBoundsCapture() only clears future interval callbacks. It cannot cancel helpers that are already running.

  6. The Node execFile timeout has no explicit SIGKILL escalation or child-process registry, and the app's before-quit cleanup does not terminate window-list helpers.

  7. The native recorder separately performs another full SCShareableContent enumeration every 500 ms to validate that the selected window still exists, adding more ScreenCaptureKit contention.

The critical bounds/helper code is unchanged on current main, so the issue does not appear fixed after v1.3.3.

Likely failure sequence

  1. A window recording is active and bounds polling runs every 250 ms.
  2. The user stops the recording.
  3. Native ScreenCaptureKit teardown begins, but bounds polling remains active.
  4. One window-list helper stalls in SCShareableContent.
  5. Additional interval ticks launch more helpers because there is no single-flight guard.
  6. Recording shutdown eventually clears the interval, but already-running helpers remain.
  7. The leaked helpers retain substantial ScreenCaptureKit/window resources and eventually exhaust compressed memory and swap.

Suggested fixes

  • Call setRecordingState(false) before awaiting stopNativeScreenRecording().
  • Replace the 250 ms setInterval with a serialized/recursive refresh that cannot overlap.
  • Add a shared in-flight promise to getNativeMacWindowSources() and set the cache timestamp after successful completion.
  • Track all window-list child processes and terminate them on recording stop and app quit.
  • Escalate timed-out helpers to SIGKILL after a short grace period.
  • Add an internal watchdog to the Swift helper so DispatchGroup.wait() cannot block indefinitely.
  • Consider a persistent or window-specific bounds API instead of enumerating all shareable content four times per second.
  • Add a regression test with a never-resolving helper and assert that at most one helper exists and that it is cleaned up on stop/quit.

Related symptoms may overlap with #700 and #673, but neither issue documents the orphaned helper/process-exhaustion mechanism.

clinton9ice avatar Sep 23 '26 20:09 clinton9ice