Merge fix/image-subtitle-memory-leaks: fix image subtitle memory and disk leaks
@@ -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
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
function getCachedValue (cache, key)169
function 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
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
try574
{575
fs.rmSync(SUBTITLE_CACHE_DIR, { recursive: true, force: true });576
} catch577
{578
// Best-effort; directory may not exist.579
}580
}581
551
function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)582
function 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
};@@ -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');5
const fs = require('fs');5
const fs = require('fs');6
const os = require('os');6
const os = require('os');7
const path = require('path');7
const path = require('path');8
const { clearCachesForFile, pruneOldImageSubtitleCaches } = require('./image-subtitles');8
const { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');9
9
10
const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');10
const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');11
const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;11
const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;@@ -25,6 +25,9 @@ const sessions = new Map();25
const leases = new Map();25
const leases = new Map();26
const reusableSessionIdBySource = new Map();26
const reusableSessionIdBySource = new Map();27
27
28
// Remove any .sup subtitle files left over from a previous server run.29
purgeSubtitleCacheDir();30
28
function parseSegmentRetentionCount (value)31
function 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
217
function clearImageSubtitleCachesForSession (session)218
{219
if (!session || !session.sourcePath) return;220
clearCachesForFile(session.sourcePath);221
}222
223
function pruneImageSubtitleCachesForSession (session)220
function pruneImageSubtitleCachesForSession (session)224
{221
{225
if (!session || !session.sourcePath) return;222
if (!session || !session.sourcePath) return;@@ -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
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
describe('image subtitle option parsing', () =>8
describe('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
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
});@@ -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', () => ({12
jest.mock('../server/image-subtitles', () => ({12
jest.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
17
function makeChildProcess ()18
function 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
});