mastra-ai/mastra · error

BackgroundTaskWorker: failed to initialize background task m

Error message

BackgroundTaskWorker: failed to initialize background task manager

What it means

After ensuring dependencies exist and creating the owned BackgroundTaskManager if needed, start() verifies that a manager instance is actually available. If this.#manager is still falsy, manager creation silently failed and the worker cannot run, so it throws this explicit initialization-failure error.

Source

Thrown at packages/core/src/worker/workers/background-task-worker.ts:118

        },
      });
    }
  }

  async start(): Promise<void> {
    if (this.#running) return;
    if (!this.deps) {
      throw new Error('BackgroundTaskWorker: call init() before start()');
    }
    // An owned manager has a terminal shutdown lifecycle. Recreate it on a
    // direct stop → start cycle instead of attempting to reinitialize a
    // manager whose subscriptions and executor registry were released.
    if (!this.#manager) {
      this.#createOwnedManager(this.deps);
    }
    const manager = this.#manager;
    if (!manager) {
      throw new Error('BackgroundTaskWorker: failed to initialize background task manager');
    }
    // When sharing Mastra's manager, Mastra has already fired off init() in
    // its constructor as fire-and-forget. Don't re-await it here — that would
    // surface init errors twice (the constructor's `.catch` already reports
    // them) and serialize startWorkers() behind the manager's full bootstrap.
    if (this.#ownsManager) {
      await manager.init(this.deps.pubsub);
    }
    this.#running = true;
  }

  async stop(): Promise<void> {
    if (!this.#running) return;
    // Only tear down the manager if this worker owns it. When sharing Mastra's
    // manager, Mastra's stopWorkers() / shutdown is responsible.
    if (this.#manager && this.#ownsManager) {
      try {
        await this.#manager.shutdown();

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Inspect your WorkerDeps for missing/invalid fields and supply a complete deps object in init()
  2. Check earlier logs for errors thrown inside createOwnedManager/manager init (the constructor's catch handler reports them)
  3. Upgrade @mastra/core to a version where owned-manager recreation on stop→start is present
  4. As a workaround, construct and inject your own BackgroundTaskManager via deps instead of relying on the owned manager

Example fix

// before
await worker.init({ storage, logger }); // manager factory gets nothing it can use
// after
await worker.init({ mastra, storage, logger, pubsub });
await worker.start();
Defensive patterns

Strategy: try-catch

Validate before calling

await worker.init(deps);
if (!worker.deps) throw new Error('deps missing');

Try / catch

try {
  await worker.start();
} catch (e) {
  if (e.message === 'BackgroundTaskWorker: failed to initialize background task manager') {
    logger.error('manager init failed — check deps and prior init logs', { deps: Object.keys(deps) });
  } else throw e;
}

Prevention

When it happens

Trigger: this.#createOwnedManager(this.deps) not producing a manager (e.g. misconfigured worker deps so manager construction short-circuits), or an external code path clearing #manager between creation and use.

Common situations: Custom or partial WorkerDeps missing fields the manager factory expects; subclass overriding manager creation; running an older worker build where owned-manager creation is conditional and skipped.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/bceb163bb1acd55b. Report an issue: GitHub.