Skip to content

Commit

Permalink
Updated internal storing of promises to fix memory leak per #977 (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica authored Dec 24, 2024
1 parent 9827c2e commit 2f2b5e3
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/createWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
...defaultOptions,
..._options,
});
const resolves = {};
const rejects = {};
const promises = {};

// Current langs, oem, and config file.
// Used if the user ever re-initializes the worker using `worker.reinitialize`.
Expand All @@ -48,21 +47,12 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi

workerCounter += 1;

const setResolve = (promiseId, res) => {
resolves[promiseId] = res;
};

const setReject = (promiseId, rej) => {
rejects[promiseId] = rej;
};

const startJob = ({ id: jobId, action, payload }) => (
new Promise((resolve, reject) => {
log(`[${id}]: Start ${jobId}, action=${action}`);
// Using both `action` and `jobId` in case user provides non-unique `jobId`.
const promiseId = `${action}-${jobId}`;
setResolve(promiseId, resolve);
setReject(promiseId, reject);
promises[promiseId] = { resolve, reject };
send(worker, {
workerId: id,
jobId,
Expand Down Expand Up @@ -237,9 +227,11 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
} else if (action === 'getPDF') {
d = Array.from({ ...data, length: Object.keys(data).length });
}
resolves[promiseId]({ jobId, data: d });
promises[promiseId].resolve({ jobId, data: d });
delete promises[promiseId];
} else if (status === 'reject') {
rejects[promiseId](data);
promises[promiseId].reject(data);
delete promises[promiseId];
if (action === 'load') workerResReject(data);
if (errorHandler) {
errorHandler(data);
Expand All @@ -254,8 +246,6 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
const resolveObj = {
id,
worker,
setResolve,
setReject,
load,
writeText,
readText,
Expand Down

0 comments on commit 2f2b5e3

Please sign in to comment.