sveltejs/kit · error

Cannot add tasks to a queue that has ended

Error message

Cannot add tasks to a queue that has ended

What it means

SvelteKit's internal task queue (used during prerendering/build) exposes add() for scheduling work. Once q.end() is called the queue is closed; adding more tasks afterwards throws this error. It indicates scheduling work after the build phase has been finalized.

Source

Thrown at packages/kit/src/core/postbuild/queue.js:52

					.then(task.fulfil, (err) => {
						task.reject(err);
						reject(err);
					})
					.then(() => {
						current -= 1;
						dequeue();
					});
			} else if (current === 0) {
				closed = true;
				resolve();
			}
		}
	}

	return {
		/** @param {() => any} fn */
		add: (fn) => {
			if (closed) throw new Error('Cannot add tasks to a queue that has ended');

			const promise = new Promise((fulfil, reject) => {
				tasks.push({ fn, fulfil, reject });
			});
			promise.catch(() => {});

			dequeue();
			return promise;
		},

		done: () => {
			if (current === 0) {
				closed = true;
				resolve();
			}

			return promise;
		}

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Ensure all q.add() calls complete before invoking q.end() — await outstanding tasks first
  2. Reorder code so end() is only called after every producer has finished enqueueing
  3. Track closed state in your wrapper and skip/queue-later instead of calling add after end
  4. If triggered by a plugin, update the plugin to a version compatible with your @sveltejs/kit build pipeline

Example fix

// before
await Promise.all(items.map((i) => q.add(() => render(i))));
q.end();
stragglers.forEach((i) => q.add(() => render(i))); // throws

// after
for (const i of items) await q.add(() => render(i));
for (const i of stragglers) await q.add(() => render(i));
q.end();
Defensive patterns

Strategy: try-catch

Validate before calling

function safeAdd(q, closed, fn) {
  if (closed) return Promise.resolve();
  return q.add(fn);
}

Try / catch

try {
  await q.add(task);
} catch (e) {
  if (String(e.message).includes('queue that has ended')) {
    console.warn('Queue closed; run task directly or reschedule');
    return task();
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling q.add(fn) after q.end() on the queue returned by create_queue — in practice, code or a plugin hook that enqueues prerender/render tasks after the prerender step completes.

Common situations: Custom build tooling integrating with Kit internals; async callbacks resolving after q.end() and then trying to add work; misordered lifecycle code in postbuild scripts.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/5926a814924eb314. Report an issue: GitHub.