Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: memory leak — cap audio segment cache + cleanup video preload elements

Louis Beaumontce79d9180c22
diff --git a/apps/screenpipe-app-tauri/components/rewind/hooks/use-frame-loading.ts b/apps/screenpipe-app-tauri/components/rewind/hooks/use-frame-loading.ts
index c1aac7368..029fcbb37 100644
--- a/apps/screenpipe-app-tauri/components/rewind/hooks/use-frame-loading.ts
+++ b/apps/screenpipe-app-tauri/components/rewind/hooks/use-frame-loading.ts
@@ -418,6 +418,8 @@ export function useFrameLoading(opts: {
// Preload adjacent video chunks so crossing chunk boundaries feels instant
useEffect(() => {
if (!adjacentFrames?.length) return;
+ let cancelled = false;
+ const preloadElements: HTMLVideoElement[] = [];
const seen = new Set<string>();
if (debouncedFrame?.filePath) seen.add(debouncedFrame.filePath);
for (const frame of adjacentFrames) {
@@ -428,17 +430,26 @@ export function useFrameLoading(opts: {
seen.add(path);
// Preload video chunk: create a hidden video element to trigger browser cache
getVideoUrl(path).then((url) => {
- if (!url) return;
+ if (!url || cancelled) return;
const v = document.createElement("video");
+ preloadElements.push(v);
v.preload = "auto";
v.muted = true;
v.src = url;
// Load just enough for metadata + first frame, then discard
- v.addEventListener("loadeddata", () => { v.src = ""; }, { once: true });
- v.addEventListener("error", () => { v.src = ""; }, { once: true });
+ v.addEventListener("loadeddata", () => { v.src = ""; v.removeAttribute("src"); v.load(); }, { once: true });
+ v.addEventListener("error", () => { v.src = ""; v.removeAttribute("src"); }, { once: true });
v.load();
});
}
+ return () => {
+ cancelled = true;
+ for (const v of preloadElements) {
+ v.src = "";
+ v.removeAttribute("src");
+ v.load();
+ }
+ };
}, [adjacentFrames, debouncedFrame?.filePath, getVideoUrl]);
// Update rendered dimensions on resize (needed for TextOverlay positioning)
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-audio-playback.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-audio-playback.tsx
index 71ab87067..6a6f45814 100644
--- a/apps/screenpipe-app-tauri/lib/hooks/use-audio-playback.tsx
+++ b/apps/screenpipe-app-tauri/lib/hooks/use-audio-playback.tsx
@@ -10,6 +10,7 @@ type PlaybackSpeed = 1 | 1.5 | 2;
const SPEED_CYCLE: PlaybackSpeed[] = [1, 1.5, 2];
const PRELOAD_AHEAD_MS = 60_000; // preload audio 60s ahead
const PRELOAD_BEHIND_MS = 5_000; // also preload 5s behind to cover current position
+const MAX_CACHED_SEGMENTS = 20; // evict oldest segments beyond this limit
interface AudioSegment {
filePath: string;
@@ -155,6 +156,29 @@ export function useAudioPlayback({
return false;
})();
+ /** Evict oldest non-playing segments when cache exceeds limit. */
+ const evictOldSegments = useCallback(() => {
+ const segments = segmentsRef.current;
+ if (segments.size <= MAX_CACHED_SEGMENTS) return;
+ // Evict non-playing segments furthest from current playback position
+ const toEvict: string[] = [];
+ for (const [path, seg] of segments) {
+ if (seg.playing) continue;
+ toEvict.push(path);
+ }
+ // Keep the most recent MAX_CACHED_SEGMENTS entries (Map preserves insertion order)
+ const evictCount = segments.size - MAX_CACHED_SEGMENTS;
+ for (let i = 0; i < Math.min(evictCount, toEvict.length); i++) {
+ const seg = segments.get(toEvict[i]);
+ if (seg) {
+ try { seg.audioElement.pause(); } catch { /* ignore */ }
+ seg.audioElement.src = "";
+ URL.revokeObjectURL(seg.blobUrl);
+ segments.delete(toEvict[i]);
+ }
+ }
+ }, []);
+
/** Load an audio file into an HTMLAudioElement with blob URL. */
const loadAudioSegment = useCallback(
async (filePath: string, chunkId: number, isInput: boolean, durationSecs: number, fallbackTsMs: number, deviceName: string) => {
@@ -213,13 +237,16 @@ export function useAudioPlayback({
"recordingStart:", new Date(recordingStartMs).toISOString(),
"duration:", actualDuration.toFixed(1) + "s",
);
+
+ // Evict old segments to cap memory usage
+ evictOldSegments();
} catch (err) {
console.warn("[audio-playback] Failed to load audio segment:", filePath, err);
} finally {
loadingPathsRef.current.delete(filePath);
}
},
- [],
+ [evictOldSegments],
);
/** Preload audio segments near the current playback position. */