Skip to content

Commit c54e924

Browse files
authored
Merge pull request #491 from thewtex/worker-pool-memory-leak
fix(WorkerPool): Address memory leak in Chrome from reject/resolve
2 parents c8bb77d + 376dbed commit c54e924

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/core/WorkerPool.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
13
import WorkerPoolFunction from './WorkerPoolFunction.js'
24
import WorkerPoolProgressCallback from './WorkerPoolProgressCallback.js'
35
import WorkerPoolRunTasksResult from './WorkerPoolRunTasksResult.js'
@@ -104,9 +106,8 @@ class WorkerPool {
104106
const info = this.runInfo[infoIndex]
105107

106108
if (info?.canceled === true) {
109+
info.reject!('Remaining tasks canceled')
107110
this.clearTask(info.index)
108-
// @ts-expect-error: error TS2722: Cannot invoke an object which is
109-
info?.reject('Remaining tasks canceled')
110111
return
111112
}
112113

@@ -129,17 +130,13 @@ class WorkerPool {
129130
this.addTask(infoIndex, reTask[0], reTask[1])
130131
} else if (!info.addingTasks && info.runningWorkers === 0) {
131132
const results = info.results
133+
info.resolve!(results)
132134
this.clearTask(info.index)
133-
// @ts-expect-error: error TS2722: Cannot invoke an object which is
134-
// possibly 'undefined'.
135-
info?.resolve(results)
136135
}
137136
}
138137
}).catch((error) => {
138+
info.reject!(error)
139139
this.clearTask(info.index)
140-
// @ts-expect-error: error TS2722: Cannot invoke an object which is
141-
// possibly 'undefined'.
142-
info?.reject(error)
143140
})
144141
} else {
145142
if (info.runningWorkers !== 0 || info.postponed) {
@@ -162,6 +159,8 @@ class WorkerPool {
162159
this.runInfo[clearIndex].taskQueue = []
163160
this.runInfo[clearIndex].progressCallback = null
164161
this.runInfo[clearIndex].canceled = null
162+
this.runInfo[clearIndex].reject = () => {}
163+
this.runInfo[clearIndex].resolve = () => {}
165164
}
166165
}
167166

0 commit comments

Comments
 (0)