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)
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)
- 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>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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 singletoWebStreamhelper pauses the source whendesiredSizehits 0 and destroys it (or SIGKILLs ffmpeg) on cancel. The@ts-expect-erroris gone.server-ffmpeg.ts(new):spawnFfmpegcaps 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/otunesand the 30 minuteos.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 callmusic.parseFile. The codecconsole.logis removed.Verification
I used a throwaway harness against local fixtures:
ensureHlsJobcalls: 1 job, 1 ffmpeg, 40 segments. No segment changed size after it was served, and no.tmpfiles were left.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