mastra-ai/mastra · info · DOMException

AbortError

AbortError

Error message

Aborted

What it means

runExperiment throws a DOMException('Aborted','AbortError') when the caller-supplied executionSignal is aborted. This is the library's cooperative cancellation mechanism: before executing each dataset item, it checks executionSignal.aborted and raises AbortError so an in-flight experiment run can be stopped by the caller. It surfaces as a rejected promise from runExperiment and its wrappers (executeRun, startExperiment, startExperimentAsync).

Source

Thrown at packages/core/src/datasets/experiment/index.ts:497

  try {
    if (beforeAll) {
      // Runs inside the execution try/catch so a failing setup produces the
      // same `failed` summary shape as any other run-level failure, with every
      // item counted as skipped.
      await beforeAll(hookArgs);
    }

    const pMap = (await import('p-map')).default;

    await pMap(
      items.map((item, idx) => ({ item, idx })),
      async ({ item, idx }) => {
        if (eventDispatcher?.failure) return;

        try {
          // Check for cancellation
          if (executionSignal?.aborted) {
            throw new DOMException('Aborted', 'AbortError');
          }

          const itemStartedAt = new Date();
          const metadataSnapshot = item.metadata === undefined ? undefined : structuredClone(item.metadata);
          const cloneMetadataSnapshot = () =>
            metadataSnapshot === undefined ? undefined : structuredClone(metadataSnapshot);
          const itemWithMetadataSnapshot = (): ExperimentItem => ({
            ...item,
            metadata: cloneMetadataSnapshot(),
          });
          let itemScorers: MastraScorer<any, any, any, any>[];
          let itemStepScorers = {} as ReturnType<typeof resolveStepScorers>;
          let scorerConfigError: ExecutionResult['error'] = null;

          if (hasRunLevelScorers) {
            itemScorers = runLevelScorers;
            itemStepScorers = runLevelStepScorers;
          } else if (item.scorerIds !== undefined) {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Treat AbortError as an expected control-flow outcome, not a bug: catch it and mark the experiment run as cancelled/partial rather than failed.
  2. If aborts are unexpected, audit what holds a reference to the AbortController and remove stray abort() calls or short timeouts.
  3. Ensure per-item results already completed are persisted; runExperiment only checks the signal between items, so completed items remain valid.
  4. If you need mid-item cancellation, pass a per-item signal (execFn receives itemSignal) and handle AbortError inside the executor.

Example fix

// before
await mastra.getExperiment(experimentId).start({ signal: controller.signal });
// after
try {
  await mastra.getExperiment(experimentId).start({ signal: controller.signal });
} catch (err) {
  if (err instanceof DOMException && err.name === 'AbortError') {
    logger.info('Experiment run cancelled by caller');
    return;
  }
  throw err;
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (controller.signal.aborted) { /* don't start the run at all */ return; }

Type guard

function isAbortError(e: unknown): e is DOMException {
  return e instanceof DOMException && e.name === 'AbortError' || (e instanceof Error && e.name === 'AbortError');
}

Try / catch

try {
  await experiment.start({ signal: controller.signal });
} catch (err) {
  if (isAbortError(err)) return markRunCancelled(runId);
  throw err;
}

Prevention

When it happens

Trigger: Calling startExperiment/executeRun with an AbortController signal and calling controller.abort() while the run loop is between item executions; aborting while items are queued in the concurrency pool before each item's try block runs.

Common situations: A user cancels an experiment run from the UI or CLI; a host times out a long-running evaluation and aborts the signal; a server shutdown handler aborts in-flight experiment runs during deployment.

Related errors


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