Skip to content

Commit 2bf74ee

Browse files
committed
Fix: High memory usage on Linux due to a compatibility issue between Sharp and Electron
1 parent 9521df2 commit 2bf74ee

13 files changed

Lines changed: 352 additions & 152 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1616
- Prevent concurrent reads of the same file [`cefa1c5`](https://github.com/ollm/OpenComic/commit/cefa1c552309a27e04ec6c6c5b7d2549028eeed5)
1717
- Memory leak in blob files when browsing extremely quickly while reading [`ccdcdbc`](https://github.com/ollm/OpenComic/commit/ccdcdbc7194e3703c16f0ae5c979f1947d3b7517)
1818
- TypeError by ensuring countries is an array [`4fb987d`](https://github.com/ollm/OpenComic/commit/4fb987d50d299d8b5c58a979f35eaef11e4c3be0)
19+
- High memory usage on Linux due to a compatibility issue between Sharp and Electron
1920

2021
## [v1.6.2](https://github.com/ollm/OpenComic/releases/tag/v1.6.2) (08-09-2025)
2122

‎scripts/cache.js‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ async function processTheImageQueue(img = false)
4242
const ratios = getRatios();
4343
const forceSize = img.forceSize || 150;
4444

45-
let blob = false;
46-
47-
if(compatible.image.blob(realPath)) // Convert unsupported images to blob
48-
blob = await workers.convertImageToBlob(realPath, {useThreads: 0.3/*0.4*/});
45+
if(compatible.image.convert(img.file)) // Convert unsupported images
46+
await workers.convertImage(img.file, {useThreads: 0.3/*0.4*/});
4947

5048
return new Promise(function(resolve, reject) {
5149

52-
image.resize(blob || realPath, toImage, {blob: blob ? true : false, width: img.size, height: Math.round(img.size * (img.type ? ratios[img.type][forceSize] : 1.5)), quality: 95, fit: img.type ? fit[img.type] : 'inside'}).then(function(){
53-
54-
//if(blob) fileManager.revokeObjectURL(path);
50+
image.resize(realPath, toImage, {width: img.size, height: Math.round(img.size * (img.type ? ratios[img.type][forceSize] : 1.5)), quality: 95, fit: img.type ? fit[img.type] : 'inside'}).then(function(){
5551

5652
if(typeof data[sha] === 'undefined') data[sha] = {lastAccess: app.time()};
5753
data[sha].size = img.size;
@@ -63,8 +59,6 @@ async function processTheImageQueue(img = false)
6359

6460
}).catch(function(){
6561

66-
//if(blob) fileManager.revokeObjectURL(path);
67-
6862
img.callback({cache: true, path: escapeBackSlash(realPath), sha: sha}, img.vars);
6963

7064
resolve();
@@ -76,9 +70,6 @@ async function processTheImageQueue(img = false)
7670

7771
async function addImageToQueue(file, size, sha, callback, vars, type, forceSize)
7872
{
79-
if(compatible.image.convert(file)) // Convert unsupported images
80-
await workers.convertImage(file);
81-
8273
threads.job('cache', {useThreads: 1}, processTheImageQueue, {file: file, size: size, sha: sha, callback: callback, vars: vars, type: type, forceSize: forceSize});
8374
}
8475

‎scripts/child-fork.js‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
const {fork} = require('node:child_process');
2+
3+
class ChildFork
4+
{
5+
fork;
6+
#index = 0;
7+
#idleTimeout;
8+
#key = crypto.randomUUID();
9+
onCloseCallback = false;
10+
promisses = [];
11+
shouldCloseOnFinish = false;
12+
13+
constructor(options = {}) {
14+
15+
this.fork = fork(p.join(appDir, 'scripts/fork.js'));
16+
this.fork.on('message', (event) => this.message(event));
17+
18+
this.#idleTimeout = options.idleTimeout || false;
19+
20+
if(this.#idleTimeout && options.idleTimeoutInit)
21+
app.setDebounce(this.#key, () => this.close(), this.#idleTimeout);
22+
23+
}
24+
25+
message(data) {
26+
27+
const arrayIndex = this.promisses.findIndex(item => item.index === data.index);
28+
const promise = this.promisses[arrayIndex];
29+
this.promisses.splice(arrayIndex, 1);
30+
31+
if(data.error)
32+
promise.reject(data.error);
33+
else
34+
promise.resolve(data.result);
35+
36+
if(this.shouldCloseOnFinish)
37+
this.closeOnFinish();
38+
39+
}
40+
41+
work(options = {}) {
42+
43+
if(this.#idleTimeout)
44+
app.setDebounce(this.#key, () => this.close(), this.#idleTimeout);
45+
46+
const index = this.#index++;
47+
48+
this.fork.send({
49+
index: index,
50+
...options,
51+
});
52+
53+
let resolve = false, reject = false;
54+
55+
const promise = new Promise(function(_resolve, _reject){
56+
57+
resolve = _resolve;
58+
reject = _reject;
59+
60+
});
61+
62+
this.promisses.push({
63+
index: index,
64+
resolve: resolve,
65+
reject: reject,
66+
});
67+
68+
return promise;
69+
70+
}
71+
72+
onClose(callback) {
73+
this.onCloseCallback = callback;
74+
}
75+
76+
close() {
77+
78+
if(!this.fork)
79+
return;
80+
81+
this.fork.kill();
82+
delete this.fork;
83+
84+
if(this.onCloseCallback)
85+
this.onCloseCallback();
86+
}
87+
88+
closeOnFinish() {
89+
90+
if(!this.promisses.length)
91+
this.close();
92+
else
93+
this.shouldCloseOnFinish = true;
94+
95+
}
96+
}
97+
98+
let IDLE_TIMEOUT = 5000;
99+
let JOBS_PER_CYCLE = 30;
100+
101+
function config(options)
102+
{
103+
if(options.idleTimeout !== undefined)
104+
IDLE_TIMEOUT = options.idleTimeout;
105+
106+
if(options.jobsPerCycle !== undefined)
107+
JOBS_PER_CYCLE = options.jobsPerCycle;
108+
}
109+
110+
const forks = [];
111+
let index = 0;
112+
113+
function pushFork()
114+
{
115+
const _index = index++;
116+
const fork = new ChildFork({idleTimeout: IDLE_TIMEOUT, idleTimeoutInit: false});
117+
118+
forks.push({
119+
index: _index,
120+
count: 0,
121+
fork: fork,
122+
});
123+
124+
fork.onClose(function(){
125+
closeFork(_index);
126+
});
127+
}
128+
129+
function closeFork(index)
130+
{
131+
const fork = forks.find(item => item.index === index);
132+
if(!fork) return;
133+
134+
forks.splice(forks.indexOf(fork), 1);
135+
fork.fork.closeOnFinish();
136+
}
137+
138+
// Keep 2 forks alive to avoid delays
139+
function createForks()
140+
{
141+
const fork = forks[0] || false;
142+
143+
if(fork?.count > JOBS_PER_CYCLE)
144+
closeFork(fork.index);
145+
146+
if(!forks[0]) pushFork();
147+
if(!forks[1]) pushFork();
148+
}
149+
150+
function getFork(count = true)
151+
{
152+
createForks();
153+
154+
const fork = forks[0];
155+
if(count) fork.count++;
156+
157+
return fork.fork;
158+
}
159+
160+
async function _resize() {
161+
162+
const fork = getFork();
163+
164+
const result = await fork.work({
165+
job: '_resize',
166+
args: [...arguments],
167+
});
168+
169+
return;
170+
}
171+
172+
async function resizeToBlob() {
173+
174+
const fork = getFork();
175+
176+
return fork.work({
177+
job: 'resizeToBlob',
178+
args: [...arguments],
179+
});
180+
181+
}
182+
183+
module.exports = {
184+
ChildFork,
185+
config,
186+
_resize,
187+
resizeToBlob,
188+
};

‎scripts/compatible.js‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,14 @@ compatible.image.all = [
315315
...compatible.image.heic,
316316
];
317317
compatible.image.blob = [ // This image formats requires conversion to Blob to be displayed
318+
319+
];
320+
compatible.image.convert = [ // This image formats requires conversion to PNG to be displayed
321+
...compatible.image.jxr,
318322
...compatible.image.jp2,
319323
...compatible.image.jxl,
320324
...compatible.image.heic,
321325
];
322-
compatible.image.convert = [ // This image formats requires conversion to PNG to be displayed
323-
...compatible.image.jxr,
324-
];
325326

326327
// Join compressed
327328
compatible.compressed.all = [
@@ -346,12 +347,13 @@ compatibleMime.image.all = [
346347
...compatibleMime.image.heic,
347348
];
348349
compatibleMime.image.blob = [ // This image formats requires conversion to Blob to be displayed
349-
...compatibleMime.image.jp2,
350-
...compatibleMime.image.jxl,
351-
...compatibleMime.image.heic,
350+
352351
];
353352
compatibleMime.image.convert = [ // This image formats requires conversion to PNG to be displayed
354-
...compatibleMime.image.jxr,
353+
...compatibleMime.image.jxr,
354+
...compatibleMime.image.jp2,
355+
...compatibleMime.image.jxl,
356+
...compatibleMime.image.heic,
355357
];
356358

357359
// Join mime compressed

‎scripts/dom.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ async function loadIndexPage(animation = true, path = false, content = false, ke
567567

568568
fileManager.revokeAllObjectURL();
569569
reading.render.revokeAllObjectURL();
570-
workers.clean('convertImageToBlob');
570+
workers.clean('convertImage');
571571

572572
scroll.reset();
573573
reading.hideContent();
@@ -2670,9 +2670,10 @@ var readingActive = false, skipNextComic = false, skipPreviousComic = false;
26702670

26712671
async function openComic(animation = true, path = true, mainPath = true, end = false, fromGoBack = false, fromNextAndPrev = false)
26722672
{
2673+
settings.purgeTemporaryFilesEveryTimes(10);
26732674
fileManager.revokeAllObjectURL();
26742675
reading.render.revokeAllObjectURL();
2675-
workers.clean('convertImageToBlob');
2676+
workers.clean('convertImage');
26762677

26772678
dom.setCurrentPageVars('reading');
26782679

@@ -2769,8 +2770,6 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
27692770
await file.makeAvailable(files, false, true);
27702771
}
27712772

2772-
await fileManager.convertUnsupportedImages(files);
2773-
27742773
if(hasMusic) files.pop(); // Remove now
27752774

27762775
file.destroy();

‎scripts/dom/file-info.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,8 @@ async function show(path, opds = false)
234234
height: Math.round(600 * window.devicePixelRatio),
235235
};
236236

237-
if(compatible.image.blob(src)) // Convert unsupported images to blob
238-
{
239-
src = await workers.convertImageToBlob(src, {priorize: true});
240-
options.blob = true;
241-
}
237+
if(compatible.image.convert(images.path)) // Convert unsupported images
238+
src = await workers.convertImage(images.path, {priorize: true});
242239

243240
let data = await image.resizeToBlob(src, options);
244241
images.realPath = data.blob;

‎scripts/fork.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const p = require('path'),
2+
fs = require('fs');
3+
4+
global.inChildFork = true;
5+
global.app = require(p.join(__dirname, '../scripts/app.js'));
6+
7+
const image = require(p.join(__dirname, '../scripts/image.js'));
8+
9+
process.on('message', async function(data) {
10+
11+
const job = data.job;
12+
let message = {};
13+
14+
switch (job)
15+
{
16+
case '_resize':
17+
18+
message = {
19+
index: data.index,
20+
result: false,
21+
error: false,
22+
};
23+
24+
try
25+
{
26+
const result = await image._resize(...data.args);
27+
message.result = result;
28+
}
29+
catch(error)
30+
{
31+
message.error = error.message;
32+
}
33+
34+
process.send(message);
35+
36+
break;
37+
38+
case 'resizeToBlob':
39+
40+
message = {
41+
index: data.index,
42+
result: false,
43+
error: false,
44+
};
45+
46+
try
47+
{
48+
const result = await image.resizeToBlob(...data.args);
49+
message.result = result;
50+
}
51+
catch(error)
52+
{
53+
console.log(error);
54+
message.error = error.message;
55+
}
56+
57+
process.send(message);
58+
59+
break;
60+
}
61+
62+
});

0 commit comments

Comments
 (0)