Skip to content

fixed download manager memory leak caused by download queue #17396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions src/extensions/download_management/DownloadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ class DownloadWorker {
}
}

public isPending = () => {
return this.mEnded === false
&& this.mWriting === false
&& this.mJob.received === 0;
}

public ended = () => {
return this.mEnded;
}
Expand Down Expand Up @@ -391,7 +397,7 @@ class DownloadWorker {
this.mRedirected = false;
this.mEnded = false;
this.assignJob(this.mJob, this.mUrl);
}, 5000);
}, 1000);
} // the else case doesn't really make sense
}

Expand Down Expand Up @@ -592,7 +598,7 @@ class DownloadWorker {
}

private mergeBuffers = (): Buffer => {
const res = Buffer.concat(this.mBuffers);
const res = Buffer.concat(this.mBuffers.map(buffer => new Uint8Array(buffer)));
this.mBuffers = [];
return res;
}
Expand Down Expand Up @@ -726,7 +732,11 @@ class DownloadManager {
this.mMaxWorkers = maxWorkers;
this.mMaxChunks = maxChunks;
this.mUserAgent = userAgent;
this.mSpeedCalculator = new SpeedCalculator(5, speedCB);
const speedCalcCB = (speed: number) => {
this.tickQueue(false);
speedCB(speed);
}
this.mSpeedCalculator = new SpeedCalculator(5, speedCalcCB);
this.mProtocolHandlers = protocolHandlers;
this.mThrottle = () => makeThrottle(maxBandwidth);
}
Expand Down Expand Up @@ -1066,32 +1076,45 @@ class DownloadManager {
download.failedCB(err);
}

private tickQueue = async () => {
let freeSpots: number = this.mMaxWorkers - Object.keys(this.mBusyWorkers).length;
let idx = 0;
log('info', 'tick dl queue', { freeSpots, queue: this.mQueue.length });

while (freeSpots > 0 && idx < this.mQueue.length) {
private tickQueue(verbose: boolean = true) {
const totalBusyWorkers = Object.entries(this.mBusyWorkers)
.filter(([key, iter]) => this.mSlowWorkers[key] == null && !iter.isPending()).length;
let freeSpots = Math.max(this.mMaxWorkers - totalBusyWorkers, 0);
if (verbose)
log('info', 'tick dl queue', { freeSpots, queueLength: this.mQueue.length });

for (let idx = 0; idx < this.mQueue.length && freeSpots > 0; idx++) {
const queueItem = this.mQueue[idx];
const unstartedChunks = queueItem.chunks.filter(chunk => chunk.state === 'init');

for (const chunk of unstartedChunks) {
if (freeSpots <= 0) break;

try {
await this.startWorker(queueItem);
--freeSpots;
} catch (err) {
const nowIdx = this.mQueue.indexOf(queueItem);
if (nowIdx !== -1) {
this.mQueue[nowIdx].failedCB(err);
this.mQueue.splice(nowIdx, 1);
}
}

if (unstartedChunks.length === 0) continue;

let chunkIndex = 0;
while (freeSpots > 0 && chunkIndex < unstartedChunks.length) {
this.startWorker(queueItem)
.catch(err => {
const itemIdx = this.mQueue.indexOf(queueItem);
if (itemIdx !== -1) {
this.mQueue[itemIdx].failedCB(err);
this.mQueue.splice(itemIdx, 1);
}
});

freeSpots--;
chunkIndex++;
}
++idx;
}

// Remove already downloaded items from the queue
this.mQueue = this.mQueue.filter(download => {
const isCompleted = download.chunks.every(chunk => chunk.state === 'finished');
if (isCompleted && verbose) {
log('info', 'removing completed download from queue', { id: download.id });
}
return !isCompleted;
});
}

// private tickQueue() {
// let freeSpots: number = this.mMaxWorkers - Object.keys(this.mBusyWorkers).length;
// let idx = 0;
Expand Down