From 21d224fa0c7b8ece81464873760ac163ea028bd7 Mon Sep 17 00:00:00 2001 From: voc0der Date: Tue, 24 Feb 2026 04:55:31 -0500 Subject: [PATCH] Fix -1 concurrent download limit handling --- backend/downloader.js | 10 +++++++++- backend/test/tests.js | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/backend/downloader.js b/backend/downloader.js index c256c5e..2166ef4 100644 --- a/backend/downloader.js +++ b/backend/downloader.js @@ -21,6 +21,14 @@ let should_check_downloads = true; const download_to_child_process = {}; +function hasReachedConcurrentDownloadLimit(maxConcurrentDownloads, runningDownloadsCount) { + const normalizedLimit = Number(maxConcurrentDownloads); + // `-1` (and other negative values) mean "no limit" in the UI/config. + if (!Number.isFinite(normalizedLimit) || normalizedLimit < 0) return false; + return runningDownloadsCount >= normalizedLimit; +} +exports.hasReachedConcurrentDownloadLimit = hasReachedConcurrentDownloadLimit; + if (db_api.database_initialized) { exports.setupDownloads(); } else { @@ -184,7 +192,7 @@ async function checkDownloads() { for (let i = 0; i < waiting_downloads.length; i++) { const waiting_download = waiting_downloads[i]; const max_concurrent_downloads = config_api.getConfigItem('ytdl_max_concurrent_downloads'); - if (max_concurrent_downloads < 0 || running_downloads_count >= max_concurrent_downloads) break; + if (hasReachedConcurrentDownloadLimit(max_concurrent_downloads, running_downloads_count)) break; if (waiting_download['finished_step'] && !waiting_download['finished']) { if (waiting_download['sub_id']) { diff --git a/backend/test/tests.js b/backend/test/tests.js index 081f09d..0accdd6 100644 --- a/backend/test/tests.js +++ b/backend/test/tests.js @@ -584,6 +584,14 @@ describe('Downloader', function() { assert(returned_download); }); + it('Respect max concurrent downloads sentinel -1', function() { + assert.strictEqual(downloader_api.hasReachedConcurrentDownloadLimit(-1, 0), false); + assert.strictEqual(downloader_api.hasReachedConcurrentDownloadLimit('-1', 5), false); + assert.strictEqual(downloader_api.hasReachedConcurrentDownloadLimit(0, 0), true); + assert.strictEqual(downloader_api.hasReachedConcurrentDownloadLimit(1, 0), false); + assert.strictEqual(downloader_api.hasReachedConcurrentDownloadLimit(1, 1), true); + }); + it('Pause file', async function() { const returned_download = await downloader_api.createDownload(url, 'video', options); await downloader_api.pauseDownload(returned_download['uid']); @@ -1142,4 +1150,4 @@ const generateEmptyVideoFile = async (file_path) => { const generateEmptyAudioFile = async (file_path) => { if (fs.existsSync(file_path)) fs.unlinkSync(file_path); return await exec(`ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 1 -q:a 9 -acodec libmp3lame ${file_path}`); -} \ No newline at end of file +}