fix(web): close leaked audio streams and ffmpeg jobs #98

Merged
joshuafhiggins merged 1 commits from t3/fix-streaming-leaks into main 2026-11-16 21:51:44 -05:00
Owner

Every seek or track change aborted a range request, but the file stream behind it stayed open until GC. ffmpeg transcodes ignored backpressure, so a slow client could buffer an entire transcode in memory. Overlapping HLS requests started duplicate ffmpeg processes writing into the same directory. Segments could be served (and cached as immutable) while ffmpeg was still writing them. Evicted jobs left ffmpeg running, and nothing capped how many ffmpeg processes could run at once.

Changes

  • server-audio-stream.ts: a single toWebStream helper pauses the source when desiredSize hits 0 and destroys it (or SIGKILLs ffmpeg) on cancel. The @ts-expect-error is gone.
  • server-ffmpeg.ts (new): spawnFfmpeg caps concurrency (OTUNES_MAX_FFMPEG, defaults to the CPU count), drops a queued spawn if its request aborts, and keeps only the last 4KB of stderr to log when ffmpeg fails.
  • server-hls.ts: the in-flight job promise goes into the map, so concurrent requests share one ffmpeg. Segments are written with -hls_flags temp_file. Eviction kills ffmpeg and removes only incomplete output. Completed playlists are reused across restarts.
  • server-cache.ts (new): a persistent, LRU size-capped cache (OTUNES_CACHE_DIR, OTUNES_CACHE_MAX_MB, default 2GB) replaces the hard-coded /tmp/otunes and the 30 minute os.tmpdir() sweep. Entries in use are pinned, and each lossless range request refreshes the file's mtime.
  • server-audio-info.ts (new): codec info is cached per file version, so Range requests no longer call music.parseFile. The codec console.log is removed.

Verification

I used a throwaway harness against local fixtures:

  • 200 aborted range reads: fd count unchanged (24 → 24)
  • 3s stalled consumer: ffmpeg stays paused, RSS delta 0MB. Cancel SIGKILLs it.
  • Cap of 2 with 6 jobs: peak of 2 concurrent. A queued spawn rejects when aborted.
  • 4 concurrent ensureHlsJob calls: 1 job, 1 ffmpeg, 40 segments. No segment changed size after it was served, and no .tmp files were left.
  • Lossless cache hit refreshes mtime.

Typecheck and lint pass. I did not test in a real browser or on mobile.

Changes made by Claude Opus 5.5 in Claude Code (T3 Code).

🤖 Generated with Claude Code

Every seek or track change aborted a range request, but the file stream behind it stayed open until GC. ffmpeg transcodes ignored backpressure, so a slow client could buffer an entire transcode in memory. Overlapping HLS requests started duplicate ffmpeg processes writing into the same directory. Segments could be served (and cached as `immutable`) while ffmpeg was still writing them. Evicted jobs left ffmpeg running, and nothing capped how many ffmpeg processes could run at once. ## Changes - **`server-audio-stream.ts`**: a single `toWebStream` helper pauses the source when `desiredSize` hits 0 and destroys it (or SIGKILLs ffmpeg) on cancel. The `@ts-expect-error` is gone. - **`server-ffmpeg.ts`** (new): `spawnFfmpeg` caps concurrency (`OTUNES_MAX_FFMPEG`, defaults to the CPU count), drops a queued spawn if its request aborts, and keeps only the last 4KB of stderr to log when ffmpeg fails. - **`server-hls.ts`**: the in-flight job promise goes into the map, so concurrent requests share one ffmpeg. Segments are written with `-hls_flags temp_file`. Eviction kills ffmpeg and removes only incomplete output. Completed playlists are reused across restarts. - **`server-cache.ts`** (new): a persistent, LRU size-capped cache (`OTUNES_CACHE_DIR`, `OTUNES_CACHE_MAX_MB`, default 2GB) replaces the hard-coded `/tmp/otunes` and the 30 minute `os.tmpdir()` sweep. Entries in use are pinned, and each lossless range request refreshes the file's mtime. - **`server-audio-info.ts`** (new): codec info is cached per file version, so Range requests no longer call `music.parseFile`. The codec `console.log` is removed. ## Verification I used a throwaway harness against local fixtures: - 200 aborted range reads: fd count unchanged (24 → 24) - 3s stalled consumer: ffmpeg stays paused, RSS delta 0MB. Cancel SIGKILLs it. - Cap of 2 with 6 jobs: peak of 2 concurrent. A queued spawn rejects when aborted. - 4 concurrent `ensureHlsJob` calls: 1 job, 1 ffmpeg, 40 segments. No segment changed size after it was served, and no `.tmp` files were left. - Lossless cache hit refreshes mtime. Typecheck and lint pass. I did not test in a real browser or on mobile. Changes made by Claude Opus 5.5 in Claude Code (T3 Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
joshuafhiggins added 1 commit 2026-11-16 14:04:56 -05:00
- File and ffmpeg streams cancel when the client aborts (seek, track change)
- ffmpeg transcodes respect backpressure and are killed on cancel
- HLS jobs dedupe in-flight creation; segments use -hls_flags temp_file
- Evicted HLS jobs kill ffmpeg; concurrent ffmpeg is capped
- Persistent, size-capped transcode cache (OTUNES_CACHE_DIR, OTUNES_CACHE_MAX_MB)
- Codec info cached per file; drop per-chunk ffmpeg and codec logging

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshuafhiggins merged commit 777713baf7 into main 2026-11-16 21:51:44 -05:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: joshuafhiggins/otunes#98