mastra-ai/mastra · error · Error

AgentScheduleWorker: call init() before start()

Error message

AgentScheduleWorker: call init() before start()

What it means

AgentScheduleWorker.start() was called before init(), so this.deps is unset. The worker needs initialized dependencies (including the Mastra instance) before it can subscribe and process schedule events.

Source

Thrown at packages/core/src/schedules/worker.ts:72

  #pushCb?: EventCallback;
  #running = false;

  constructor(config: AgentScheduleWorkerConfig = {}) {
    super();
    this.#config = config;
  }

  async init(deps: WorkerDeps): Promise<void> {
    await super.init(deps);

    if (!deps.mastra) {
      throw new Error('AgentScheduleWorker requires Mastra instance');
    }
  }

  async start(): Promise<void> {
    if (this.#running) return;
    if (!this.deps) throw new Error('AgentScheduleWorker: call init() before start()');

    // Push-only pubsubs (EventEmitter, UnixSocketPubSub) don't support the
    // grouped pull subscription a PullTransport requires. They deliver every
    // event to every in-process subscriber, so subscribe directly without a
    // group — mirroring how Mastra.startWorkers handles workflow events for
    // push-only transports instead of running the pull-based worker.
    const modes = this.deps.pubsub.supportedModes ?? ['pull'];
    if (!modes.includes('pull')) {
      const cb: EventCallback = (event, ack, nack) => {
        void this.#handleEvent(event, ack, nack);
      };
      this.#pushCb = cb;
      await this.deps.pubsub.subscribe(TOPIC_AGENT_SCHEDULES, cb);
      this.#running = true;
      return;
    }

    const group = this.#config.group ?? DEFAULT_GROUP;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Call worker.init(deps) and await it before calling worker.start().
  2. Await init so any init error surfaces instead of being followed by start().
  3. Guard startup: only call start() when init completed successfully (e.g. after a successful await).

Example fix

// before
await worker.start();
// after
await worker.init({ pubsub, storage, mastra });
await worker.start();
Defensive patterns

Strategy: validation

Validate before calling

if (!worker.isInitialized?.()) await worker.init(deps);
await worker.start();

Try / catch

try {
  await worker.start();
} catch (e) {
  if (String((e as Error).message).includes('call init()')) {
    await worker.init(deps);
    await worker.start();
  } else throw e;
}

Prevention

When it happens

Trigger: Calling worker.start() on a fresh AgentScheduleWorker without a prior successful worker.init(deps) call.

Common situations: Custom bootstrap code that starts workers before initializing them; error-swallowed init failure followed by start; reordering of statements after refactor.

Related errors


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