mastra-ai/mastra · error
Allocation sampling did not restart.
Error message
Allocation sampling did not restart.
What it means
For non-final captures, captureEpoch() restarts allocation sampling in its finally block so the next epoch can be recorded. If sampling is still not active afterwards (startSampling threw and the failure was swallowed into latestError / state 'error'), the library throws 'Allocation sampling did not restart.' (or the recorded latestError). The capture itself succeeded, but the continuous sampling loop is broken.
Source
Thrown at mastracode/sdk/src/process-memory-diagnostics.ts:630
this.latestCapturePath = finalPath;
this.latestError = null;
} catch (error) {
this.artifactWriteFailed = true;
this.latestError = `Unable to persist allocation capture: ${errorMessage(error)}`;
throw new Error(this.latestError, { cause: error });
} finally {
if (!final && !this.stopRequested && this.state === 'active') {
try {
await this.startSampling();
} catch (error) {
this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
this.state = 'error';
}
}
}
if (!final && !this.stopRequested && !this.samplingActive) {
throw new Error(this.latestError ?? 'Allocation sampling did not restart.');
}
return { path: finalPath, sequence, timestamp, reason };
}
private async restartSamplingAfterFailure(): Promise<void> {
try {
await this.startSampling();
} catch (error) {
this.latestError = `Unable to restart allocation sampling: ${errorMessage(error)}`;
this.state = 'error';
}
}
private recordError(prefix: string, error: unknown): void {
this.latestError = `${prefix}: ${errorMessage(error)}`;
}
View on GitHub (pinned to 75dd419e61)
Solutions
- Check getStatus(): if state is 'error', stop() and start() a fresh diagnostics session — the current sampling loop cannot resume.
- Read latestError for the 'Unable to restart allocation sampling: ...' message recorded by the finally block; fix the underlying inspector issue.
- Verify no external debugger (DevTools, --inspect client) is contending for the session; use a dedicated inspector port.
- If captures are triggered while stopRequested is set, stop mixing manual capture() with stop(); finalize with stop() instead.
- Consider lowering capture frequency or upgrading Node if the inspector repeatedly fails under load.
Example fix
// before
await diagnostics.capture('manual'); // throws: sampling did not restart
// keep using same instance
// after
try {
await diagnostics.capture('manual');
} catch {
const s = await diagnostics.getStatus();
if (s.state === 'error') {
await diagnostics.stop();
await diagnostics.start();
}
} Defensive patterns
Strategy: fallback
Validate before calling
const s = diagnostics.getStatus();
if (s.state !== 'active') throw new Error('Diagnostics not active; start a fresh session before capturing.'); Try / catch
try {
await diagnostics.capture('manual');
} catch (e) {
if ((e as Error).message.includes('Allocation sampling did not restart')) {
// profile was saved, but the sampling loop is broken: recycle the session
await diagnostics.stop();
diagnostics = createDiagnostics(config);
await diagnostics.start();
} else throw e;
} Prevention
- Treat any capture error as a signal to check state/latestError and recycle the session if not 'active'.
- Don't interleave manual capture() calls with stop(); let stop() perform the final capture.
- Keep the inspector session exclusive to diagnostics (dedicated --inspect port).
- Periodically verify sampling continues: check that new allocation-*.heapprofile files appear at the capture interval.
When it happens
Trigger: After a successful profile write, startSampling() (HeapProfiler.startSampling) fails in the finally block — inspector session degraded or closed — leaving samplingActive false; also reachable when state is not 'active' so the finally skips restart entirely.
Common situations: Inspector session dying mid-run (external debugger interference, worker teardown); HeapProfiler domain errors after heavy load; capture() racing with stop() so stopRequested/state prevents restart; Node inspector instability on very long-running processes.
Related errors
- Allocation sampling is not active.
- Inspector session is unavailable.
- ${this.latestError}
- Diagnostics output directory is unavailable.
- restart() is not supported on ${this.workflowEngineType} wor
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/3ec581f2be362f3e.
Report an issue: GitHub.