mastra-ai/mastra · error

Allocation sampling is not active.

Error message

Allocation sampling is not active.

What it means

captureEpoch() is the internal routine that stops V8 heap allocation sampling and persists the resulting .heapprofile. Before stopping sampling it asserts that an inspector session exists, sampling is flagged active, and an output directory is set; if any is missing it throws this error (preferring a previously recorded diagnostic message). The library throws it because capturing a profile is meaningless when HeapProfiler.startSampling never succeeded or state was torn down.

Source

Thrown at mastracode/sdk/src/process-memory-diagnostics.ts:580

        throw error;
      }
    });
    this.writeQueue = operation.catch(() => undefined);
    return operation;
  }

  private enqueueCapture<T>(operation: () => Promise<T>): Promise<T> {
    const result = this.captureQueue.then(operation);
    this.captureQueue = result.catch(() => undefined);
    return result;
  }

  private async captureEpoch(
    reason: ProcessMemoryDiagnosticsCapture['reason'],
    final: boolean,
  ): Promise<ProcessMemoryDiagnosticsCapture> {
    if (!this.inspector || !this.samplingActive || !this.outputDirectory) {
      throw new Error(this.latestError ?? 'Allocation sampling is not active.');
    }

    let response: Record<string, unknown>;
    try {
      response = await this.inspector.post('HeapProfiler.stopSampling');
      this.samplingActive = false;
    } catch (error) {
      this.latestError = `Allocation capture failed: ${errorMessage(error)}`;
      if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
      throw new Error(this.latestError, { cause: error });
    }

    const profile = response.profile;
    if (!profile || typeof profile !== 'object') {
      this.latestError = 'Allocation capture failed: inspector returned no profile.';
      if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();
      throw new Error(this.latestError);
    }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check diagnostics.getStatus() state === 'active' before calling capture(); if state is 'error' or 'inactive', create and start() a new instance instead of reusing it.
  2. Read the latestError field on the instance — the thrown message is usually the earlier underlying failure (e.g. 'Allocation capture failed: ...') and points at the real cause.
  3. Verify start() actually succeeded and that the inspector session stayed connected (same process, no forced disconnect); enable logging around startSampling.
  4. If this happens on stop(), it is often benign (sampling already ended); tolerate or guard the call.

Example fix

// before
const status = await diagnostics.stop();
await diagnostics.capture('manual'); // throws: sampling not active after stop
// after
const status = await diagnostics.stop();
if (status.state === 'active') {
  await diagnostics.capture('manual');
}
Defensive patterns

Strategy: validation

Validate before calling

const status = diagnostics.getStatus();
if (status.state !== 'active') throw new Error(`Cannot capture: diagnostics state is ${status.state}`);

Try / catch

try {
  await diagnostics.capture('manual');
} catch (e) {
  if ((e as Error).message.includes('Allocation sampling is not active')) {
    // instance is stale/error: restart a fresh session
    await restartDiagnostics();
  } else throw e;
}

Prevention

When it happens

Trigger: Calling capture('manual') or stop() when this.inspector is null, this.samplingActive is false (e.g. a previous capture failed and restartSamplingAfterFailure could not re-arm sampling, or disconnectInspector ran), or this.outputDirectory is null (createArtifacts never ran or was cleaned up).

Common situations: Reusing a stopped ProcessMemoryDiagnostics instance; a prior 'Allocation capture failed' left samplingActive=false and the instance in 'error' state; disk/permission failure during start prevented the output directory from being created; Node version without inspector HeapProfiler support so startSampling failed; calling capture() concurrently with stop() after stopRequested reset sampling.

Related errors


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