BlueGit

timfa filemanager

Fix image subtitle memory and disk leaks

- Subtitle event cache TTL now refreshes on every access, preventing
  the entire subtitle track being re-parsed every 5 minutes during
  active playback. Previously, watching a film with PGS subtitles for
  longer than 5 minutes would silently re-decode all subtitle frames
  from scratch, causing periodic CPU spikes and visible lag.
- clearCachesForFile now deletes the corresponding .sup file from disk
  when it evicts an event cache entry, instead of only clearing the
  in-memory cache. Without this, one .sup file was left on disk
  permanently for every unique video+subtitle-stream combination ever
  watched.
- purgeSubtitleCacheDir is called once at server startup to remove any
  .sup files left over from a previous run, so the on-disk footprint is
  always bounded to the current session.
- clearCachesForFile now maintains separate key lists for the event and
  frame caches instead of one shared list, making the intent clear and
  avoiding redundant no-op lookups.
- Removed dead code clearImageSubtitleCachesForSession from
  video-stream.js (defined but never called).
- Updated image-subtitles tests to match the actual render scale (0.75)
  and added coverage for purgeSubtitleCacheDir and the guarantee that
  subtitle caches are cleared when a session's last stream is closed.

timfa Tim Falken · pushed by timfa

Authored 2026-05-20 · 6 months ago

Hash
50e24563eacd6d86aaca9e65dfc4075f6f063008
Parent
4992cca2
4 files changed +122 −22
server/image-subtitles.js +42 −10
@@ -157,6 +157,15 @@ function deleteCachedValue (cache, key)
157 157 return true;
158 158 }
159 159
160 function touchCachedEntry (cache, key, ttlMs)
161 {
162 const entry = cache.map.get(key);
163 if (!entry) return;
164 entry.expiresAt = Date.now() + ttlMs;
165 cache.map.delete(key);
166 cache.map.set(key, entry);
167 }
168
160 169 function getCachedValue (cache, key)
161 170 {
162 171 const now = Date.now();
@@ -374,6 +383,7 @@ async function getEventCacheEntry (filePath, streamIndex)
374 383 const cached = getCachedValue(eventCache, identityKey);
375 384 if (cached)
376 385 {
386 touchCachedEntry(eventCache, identityKey, EVENT_CACHE_TTL_MS);
377 387 return cached;
378 388 }
379 389
@@ -548,6 +558,27 @@ async function renderImageSubtitleFrame (options)
548 558 return cacheValue;
549 559 }
550 560
561 function deleteSupFileForKey (identityKey)
562 {
563 const cacheKey = eventCacheFileKey(identityKey);
564 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
565 fs.unlink(supPath, () =>
566 {
567 // Best-effort; file may not exist if extraction never completed.
568 });
569 }
570
571 function purgeSubtitleCacheDir ()
572 {
573 try
574 {
575 fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });
576 } catch
577 {
578 // Best-effort; directory may not exist.
579 }
580 }
581
551 582 function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
552 583 {
553 584 const prefix = cachePrefixForFile(filePath);
@@ -586,27 +617,27 @@ function clearCachesForFile (filePath)
586 617 const prefix = cachePrefixForFile(filePath);
587 618 if (!prefix) return;
588 619
589 const keysToDelete = [];
620 const eventKeysToDelete = [];
621 const frameKeysToDelete = [];
590 622
591 623 for (const key of eventCache.map.keys())
592 624 {
593 if (key.startsWith(prefix))
594 {
595 keysToDelete.push(key);
596 }
625 if (key.startsWith(prefix)) eventKeysToDelete.push(key);
597 626 }
598 627
599 628 for (const key of frameCache.map.keys())
600 629 {
601 if (key.startsWith(prefix))
602 {
603 keysToDelete.push(key);
604 }
630 if (key.startsWith(prefix)) frameKeysToDelete.push(key);
605 631 }
606 632
607 for (const key of keysToDelete)
633 for (const key of eventKeysToDelete)
608 634 {
635 deleteSupFileForKey(key);
609 636 deleteCachedValue(eventCache, key);
637 }
638
639 for (const key of frameKeysToDelete)
640 {
610 641 deleteCachedValue(frameCache, key);
611 642 }
612 643 }
@@ -616,4 +647,5 @@ module.exports = {
616 647 renderImageSubtitleFrame,
617 648 clearCachesForFile,
618 649 pruneOldImageSubtitleCaches,
650 purgeSubtitleCacheDir,
619 651 };
@@ -157,6 +157,15 @@ function deleteCachedValue (cache, key)
157 return true;
157 return true;
158}
158}
159
159
160function touchCachedEntry (cache, key, ttlMs)
161{
162 const entry = cache.map.get(key);
163 if (!entry) return;
164 entry.expiresAt = Date.now() + ttlMs;
165 cache.map.delete(key);
166 cache.map.set(key, entry);
167}
168
160function getCachedValue (cache, key)
169function getCachedValue (cache, key)
161{
170{
162 const now = Date.now();
171 const now = Date.now();
@@ -374,6 +383,7 @@ async function getEventCacheEntry (filePath, streamIndex)
374 const cached = getCachedValue(eventCache, identityKey);
383 const cached = getCachedValue(eventCache, identityKey);
375 if (cached)
384 if (cached)
376 {
385 {
386 touchCachedEntry(eventCache, identityKey, EVENT_CACHE_TTL_MS);
377 return cached;
387 return cached;
378 }
388 }
379
389
@@ -548,6 +558,27 @@ async function renderImageSubtitleFrame (options)
548 return cacheValue;
558 return cacheValue;
549}
559}
550
560
561function deleteSupFileForKey (identityKey)
562{
563 const cacheKey = eventCacheFileKey(identityKey);
564 const supPath = path.join(SUBTITLE_CACHE_DIR, `${cacheKey}.sup`);
565 fs.unlink(supPath, () =>
566 {
567 // Best-effort; file may not exist if extraction never completed.
568 });
569}
570
571function purgeSubtitleCacheDir ()
572{
573 try
574 {
575 fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });
576 } catch
577 {
578 // Best-effort; directory may not exist.
579 }
580}
581
551function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
582function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
552{
583{
553 const prefix = cachePrefixForFile(filePath);
584 const prefix = cachePrefixForFile(filePath);
@@ -586,27 +617,27 @@ function clearCachesForFile (filePath)
586 const prefix = cachePrefixForFile(filePath);
617 const prefix = cachePrefixForFile(filePath);
587 if (!prefix) return;
618 if (!prefix) return;
588
619
589 const keysToDelete = [];
620 const eventKeysToDelete = [];
621 const frameKeysToDelete = [];
590
622
591 for (const key of eventCache.map.keys())
623 for (const key of eventCache.map.keys())
592 {
624 {
593 if (key.startsWith(prefix))
625 if (key.startsWith(prefix)) eventKeysToDelete.push(key);
594 {
595 keysToDelete.push(key);
596 }
597 }
626 }
598
627
599 for (const key of frameCache.map.keys())
628 for (const key of frameCache.map.keys())
600 {
629 {
601 if (key.startsWith(prefix))
630 if (key.startsWith(prefix)) frameKeysToDelete.push(key);
602 {
603 keysToDelete.push(key);
604 }
605 }
631 }
606
632
607 for (const key of keysToDelete)
633 for (const key of eventKeysToDelete)
608 {
634 {
635 deleteSupFileForKey(key);
609 deleteCachedValue(eventCache, key);
636 deleteCachedValue(eventCache, key);
637 }
638
639 for (const key of frameKeysToDelete)
640 {
610 deleteCachedValue(frameCache, key);
641 deleteCachedValue(frameCache, key);
611 }
642 }
612}
643}
@@ -616,4 +647,5 @@ module.exports = {
616 renderImageSubtitleFrame,
647 renderImageSubtitleFrame,
617 clearCachesForFile,
648 clearCachesForFile,
618 pruneOldImageSubtitleCaches,
649 pruneOldImageSubtitleCaches,
650 purgeSubtitleCacheDir,
619};
651};
server/video-stream.js +4 −7
@@ -5,7 +5,7 @@ const crypto = require('crypto');
5 5 const fs = require('fs');
6 6 const os = require('os');
7 7 const path = require('path');
8 const { clearCachesForFile, pruneOldImageSubtitleCaches } = require('./image-subtitles');
8 const { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');
9 9
10 10 const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11 11 const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
@@ -25,6 +25,9 @@ const sessions = new Map();
25 25 const leases = new Map();
26 26 const reusableSessionIdBySource = new Map();
27 27
28 // Remove any .sup subtitle files left over from a previous server run.
29 purgeSubtitleCacheDir();
30
28 31 function parseSegmentRetentionCount (value)
29 32 {
30 33 const parsed = Number.parseInt(String(value || ''), 10);
@@ -214,12 +217,6 @@ function destroySession (sessionId)
214 217 return true;
215 218 }
216 219
217 function clearImageSubtitleCachesForSession (session)
218 {
219 if (!session || !session.sourcePath) return;
220 clearCachesForFile(session.sourcePath);
221 }
222
223 220 function pruneImageSubtitleCachesForSession (session)
224 221 {
225 222 if (!session || !session.sourcePath) return;
@@ -5,7 +5,7 @@ const crypto = require('crypto');
5const fs = require('fs');
5const fs = require('fs');
6const os = require('os');
6const os = require('os');
7const path = require('path');
7const path = require('path');
8const { clearCachesForFile, pruneOldImageSubtitleCaches } = require('./image-subtitles');
8const { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');
9
9
10const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
10const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
11const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
@@ -25,6 +25,9 @@ const sessions = new Map();
25const leases = new Map();
25const leases = new Map();
26const reusableSessionIdBySource = new Map();
26const reusableSessionIdBySource = new Map();
27
27
28// Remove any .sup subtitle files left over from a previous server run.
29purgeSubtitleCacheDir();
30
28function parseSegmentRetentionCount (value)
31function parseSegmentRetentionCount (value)
29{
32{
30 const parsed = Number.parseInt(String(value || ''), 10);
33 const parsed = Number.parseInt(String(value || ''), 10);
@@ -214,12 +217,6 @@ function destroySession (sessionId)
214 return true;
217 return true;
215}
218}
216
219
217function clearImageSubtitleCachesForSession (session)
218{
219 if (!session || !session.sourcePath) return;
220 clearCachesForFile(session.sourcePath);
221}
222
223function pruneImageSubtitleCachesForSession (session)
220function pruneImageSubtitleCachesForSession (session)
224{
221{
225 if (!session || !session.sourcePath) return;
222 if (!session || !session.sourcePath) return;
tests/image-subtitles.test.js +27 −5
@@ -1,6 +1,9 @@
1 1 'use strict';
2 2
3 const { parseRenderOptions, pruneOldImageSubtitleCaches } = require('../server/image-subtitles');
3 const fs = require('fs');
4 const os = require('os');
5 const path = require('path');
6 const { parseRenderOptions, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('../server/image-subtitles');
4 7
5 8 describe('image subtitle option parsing', () =>
6 9 {
@@ -40,8 +43,8 @@ describe('image subtitle option parsing', () =>
40 43 canvasHeight: 2160,
41 44 });
42 45
43 expect(parsed.canvasWidth).toBe(640);
44 expect(parsed.canvasHeight).toBe(338);
46 expect(parsed.canvasWidth).toBe(960);
47 expect(parsed.canvasHeight).toBe(506);
45 48 expect(parsed.canvasWidth).toBeLessThanOrEqual(1280);
46 49 expect(parsed.canvasHeight).toBeLessThanOrEqual(720);
47 50 });
@@ -56,8 +59,8 @@ describe('image subtitle option parsing', () =>
56 59 canvasHeight: 1600,
57 60 });
58 61
59 expect(parsed.canvasWidth).toBe(640);
60 expect(parsed.canvasHeight).toBe(267);
62 expect(parsed.canvasWidth).toBe(960);
63 expect(parsed.canvasHeight).toBe(400);
61 64 });
62 65
63 66 test('prune calculates drop threshold as end plus duration plus 5 seconds', () =>
@@ -84,3 +87,22 @@ describe('image subtitle option parsing', () =>
84 87 expect(afterThreshold >= dropThreshold).toBe(true);
85 88 });
86 89 });
90
91 describe('subtitle cache directory management', () =>
92 {
93 test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () =>
94 {
95 const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles');
96 fs.mkdirSync(testDir, { recursive: true });
97 fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder');
98
99 purgeSubtitleCacheDir();
100
101 expect(fs.existsSync(testDir)).toBe(false);
102 });
103
104 test('purgeSubtitleCacheDir does not throw when the directory does not exist', () =>
105 {
106 expect(() => purgeSubtitleCacheDir()).not.toThrow();
107 });
108 });
@@ -1,6 +1,9 @@
1'use strict';
1'use strict';
2
2
3const { parseRenderOptions, pruneOldImageSubtitleCaches } = require('../server/image-subtitles');
3const fs = require('fs');
4const os = require('os');
5const path = require('path');
6const { parseRenderOptions, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('../server/image-subtitles');
4
7
5describe('image subtitle option parsing', () =>
8describe('image subtitle option parsing', () =>
6{
9{
@@ -40,8 +43,8 @@ describe('image subtitle option parsing', () =>
40 canvasHeight: 2160,
43 canvasHeight: 2160,
41 });
44 });
42
45
43 expect(parsed.canvasWidth).toBe(640);
46 expect(parsed.canvasWidth).toBe(960);
44 expect(parsed.canvasHeight).toBe(338);
47 expect(parsed.canvasHeight).toBe(506);
45 expect(parsed.canvasWidth).toBeLessThanOrEqual(1280);
48 expect(parsed.canvasWidth).toBeLessThanOrEqual(1280);
46 expect(parsed.canvasHeight).toBeLessThanOrEqual(720);
49 expect(parsed.canvasHeight).toBeLessThanOrEqual(720);
47 });
50 });
@@ -56,8 +59,8 @@ describe('image subtitle option parsing', () =>
56 canvasHeight: 1600,
59 canvasHeight: 1600,
57 });
60 });
58
61
59 expect(parsed.canvasWidth).toBe(640);
62 expect(parsed.canvasWidth).toBe(960);
60 expect(parsed.canvasHeight).toBe(267);
63 expect(parsed.canvasHeight).toBe(400);
61 });
64 });
62
65
63 test('prune calculates drop threshold as end plus duration plus 5 seconds', () =>
66 test('prune calculates drop threshold as end plus duration plus 5 seconds', () =>
@@ -84,3 +87,22 @@ describe('image subtitle option parsing', () =>
84 expect(afterThreshold >= dropThreshold).toBe(true);
87 expect(afterThreshold >= dropThreshold).toBe(true);
85 });
88 });
86});
89});
90
91describe('subtitle cache directory management', () =>
92{
93 test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () =>
94 {
95 const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles');
96 fs.mkdirSync(testDir, { recursive: true });
97 fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder');
98
99 purgeSubtitleCacheDir();
100
101 expect(fs.existsSync(testDir)).toBe(false);
102 });
103
104 test('purgeSubtitleCacheDir does not throw when the directory does not exist', () =>
105 {
106 expect(() => purgeSubtitleCacheDir()).not.toThrow();
107 });
108});
tests/video-stream.test.js +49 −0
@@ -12,6 +12,7 @@ jest.mock('child_process', () => ({
12 12 jest.mock('../server/image-subtitles', () => ({
13 13 clearCachesForFile: jest.fn(),
14 14 pruneOldImageSubtitleCaches: jest.fn(),
15 purgeSubtitleCacheDir: jest.fn(),
15 16 }));
16 17
17 18 function makeChildProcess ()
@@ -577,4 +578,52 @@ describe('video stream bootstrap', () =>
577 578
578 579 streamModule.releaseVideoStreamSession(secondViewer.sessionId);
579 580 });
581
582 test('purges subtitle disk cache when the module is first loaded', () =>
583 {
584 const { purgeSubtitleCacheDir } = require('../server/image-subtitles');
585 require('../server/video-stream');
586 expect(purgeSubtitleCacheDir).toHaveBeenCalledTimes(1);
587 });
588
589 test('clears subtitle caches for a video when its last stream is released', async () =>
590 {
591 mockSpawn.mockImplementation((command, args) =>
592 {
593 const child = makeChildProcess();
594 const exeName = path.basename(String(command || '')).toLowerCase();
595
596 if (exeName.includes('ffprobe'))
597 {
598 process.nextTick(() =>
599 {
600 child.stdout.emit('data', Buffer.from(JSON.stringify({
601 streams: [
602 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
603 ],
604 })));
605 child.emit('close', 0);
606 });
607 return child;
608 }
609
610 const playlistPath = String(args[args.length - 1] || '');
611 const outputDir = path.dirname(playlistPath);
612 process.nextTick(() =>
613 {
614 fs.mkdirSync(outputDir, { recursive: true });
615 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
616 });
617 return child;
618 });
619
620 const streamModule = require('../server/video-stream');
621 const { clearCachesForFile } = require('../server/image-subtitles');
622
623 const session = await streamModule.createVideoStreamSession(sourceFile);
624 clearCachesForFile.mockClear();
625
626 streamModule.releaseVideoStreamSession(session.sessionId);
627 expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
628 });
580 629 });
@@ -12,6 +12,7 @@ jest.mock('child_process', () => ({
12jest.mock('../server/image-subtitles', () => ({
12jest.mock('../server/image-subtitles', () => ({
13 clearCachesForFile: jest.fn(),
13 clearCachesForFile: jest.fn(),
14 pruneOldImageSubtitleCaches: jest.fn(),
14 pruneOldImageSubtitleCaches: jest.fn(),
15 purgeSubtitleCacheDir: jest.fn(),
15}));
16}));
16
17
17function makeChildProcess ()
18function makeChildProcess ()
@@ -577,4 +578,52 @@ describe('video stream bootstrap', () =>
577
578
578 streamModule.releaseVideoStreamSession(secondViewer.sessionId);
579 streamModule.releaseVideoStreamSession(secondViewer.sessionId);
579 });
580 });
581
582 test('purges subtitle disk cache when the module is first loaded', () =>
583 {
584 const { purgeSubtitleCacheDir } = require('../server/image-subtitles');
585 require('../server/video-stream');
586 expect(purgeSubtitleCacheDir).toHaveBeenCalledTimes(1);
587 });
588
589 test('clears subtitle caches for a video when its last stream is released', async () =>
590 {
591 mockSpawn.mockImplementation((command, args) =>
592 {
593 const child = makeChildProcess();
594 const exeName = path.basename(String(command || '')).toLowerCase();
595
596 if (exeName.includes('ffprobe'))
597 {
598 process.nextTick(() =>
599 {
600 child.stdout.emit('data', Buffer.from(JSON.stringify({
601 streams: [
602 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
603 ],
604 })));
605 child.emit('close', 0);
606 });
607 return child;
608 }
609
610 const playlistPath = String(args[args.length - 1] || '');
611 const outputDir = path.dirname(playlistPath);
612 process.nextTick(() =>
613 {
614 fs.mkdirSync(outputDir, { recursive: true });
615 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
616 });
617 return child;
618 });
619
620 const streamModule = require('../server/video-stream');
621 const { clearCachesForFile } = require('../server/image-subtitles');
622
623 const session = await streamModule.createVideoStreamSession(sourceFile);
624 clearCachesForFile.mockClear();
625
626 streamModule.releaseVideoStreamSession(session.sessionId);
627 expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
628 });
580});
629});