{"record":{"id":"1c0f6cdd83cdd8eb","repo":"mastra-ai/mastra","slug":"allocation-sampling-is-not-active","errorCode":null,"errorMessage":"Allocation sampling is not active.","messagePattern":"Allocation sampling is not active\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/process-memory-diagnostics.ts","lineNumber":580,"sourceCode":"        throw error;\n      }\n    });\n    this.writeQueue = operation.catch(() => undefined);\n    return operation;\n  }\n\n  private enqueueCapture<T>(operation: () => Promise<T>): Promise<T> {\n    const result = this.captureQueue.then(operation);\n    this.captureQueue = result.catch(() => undefined);\n    return result;\n  }\n\n  private async captureEpoch(\n    reason: ProcessMemoryDiagnosticsCapture['reason'],\n    final: boolean,\n  ): Promise<ProcessMemoryDiagnosticsCapture> {\n    if (!this.inspector || !this.samplingActive || !this.outputDirectory) {\n      throw new Error(this.latestError ?? 'Allocation sampling is not active.');\n    }\n\n    let response: Record<string, unknown>;\n    try {\n      response = await this.inspector.post('HeapProfiler.stopSampling');\n      this.samplingActive = false;\n    } catch (error) {\n      this.latestError = `Allocation capture failed: ${errorMessage(error)}`;\n      if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();\n      throw new Error(this.latestError, { cause: error });\n    }\n\n    const profile = response.profile;\n    if (!profile || typeof profile !== 'object') {\n      this.latestError = 'Allocation capture failed: inspector returned no profile.';\n      if (!final && !this.stopRequested) await this.restartSamplingAfterFailure();\n      throw new Error(this.latestError);\n    }","sourceCodeStart":562,"sourceCodeEnd":598,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/process-memory-diagnostics.ts#L562-L598","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check diagnostics.getStatus() state === 'active' before calling capture(); if state is 'error' or 'inactive', create and start() a new instance instead of reusing it.","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.","Verify start() actually succeeded and that the inspector session stayed connected (same process, no forced disconnect); enable logging around startSampling.","If this happens on stop(), it is often benign (sampling already ended); tolerate or guard the call."],"exampleFix":"// before\nconst status = await diagnostics.stop();\nawait diagnostics.capture('manual'); // throws: sampling not active after stop\n// after\nconst status = await diagnostics.stop();\nif (status.state === 'active') {\n  await diagnostics.capture('manual');\n}","handlingStrategy":"validation","validationCode":"const status = diagnostics.getStatus();\nif (status.state !== 'active') throw new Error(`Cannot capture: diagnostics state is ${status.state}`);","typeGuard":null,"tryCatchPattern":"try {\n  await diagnostics.capture('manual');\n} catch (e) {\n  if ((e as Error).message.includes('Allocation sampling is not active')) {\n    // instance is stale/error: restart a fresh session\n    await restartDiagnostics();\n  } else throw e;\n}","preventionTips":["Always await start() and check getStatus().state === 'active' before capturing.","Never call capture() after stop() on the same instance; create a new instance per session.","After any capture error, inspect latestError and state instead of blindly retrying.","Ensure the runtime supports the inspector HeapProfiler domain (plain Node, not restricted serverless)."],"tags":["inspector","heap-profiler","state-error","diagnostics"],"backgroundTag":"profiler-not-active","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}