{"record":{"id":"89c9472cdfc4a355","repo":"mastra-ai/mastra","slug":"inspector-session-is-unavailable","errorCode":null,"errorMessage":"Inspector session is unavailable.","messagePattern":"Inspector session is unavailable\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/process-memory-diagnostics.ts","lineNumber":522,"sourceCode":"          timestamp,\n          sequence: this.gcEventCount,\n          name: entry.name,\n          startTime: entry.startTime,\n          duration: entry.duration,\n          kind: gcEntry.detail?.kind ?? gcEntry.kind ?? null,\n          flags: gcEntry.detail?.flags ?? gcEntry.flags ?? null,\n          before,\n          after,\n          latestSampleSequence: this.latestSample?.sequence ?? null,\n        });\n      }\n    });\n    observer.observe({ entryTypes: ['gc'] });\n    this.observer = observer;\n  }\n\n  private async startSampling(): Promise<void> {\n    if (!this.inspector) throw new Error('Inspector session is unavailable.');\n    await this.inspector.post('HeapProfiler.startSampling', {\n      samplingInterval: this.config.allocationIntervalBytes,\n      includeObjectsCollectedByMajorGC: true,\n      includeObjectsCollectedByMinorGC: true,\n    });\n    this.samplingActive = true;\n  }\n\n  private async takeSample(): Promise<ProcessMemoryDiagnosticsMemorySample> {\n    if (!this.outputDirectory || !this.startedAt) throw new Error('Diagnostics output directory is unavailable.');\n    const sample: ProcessMemoryDiagnosticsMemorySample = {\n      timestamp: this.now().toISOString(),\n      sequence: this.sampleCount + 1,\n      elapsedMs: Math.max(0, this.now().getTime() - this.startedAt.getTime()),\n      memory: process.memoryUsage(),\n      resourceUsage: process.resourceUsage(),\n      heap: getHeapStatistics(),\n      heapSpaces: getHeapSpaceStatistics(),","sourceCodeStart":504,"sourceCodeEnd":540,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/process-memory-diagnostics.ts#L504-L540","documentation":"startSampling throws this when this.inspector is null, i.e. the V8 inspector session has not been created or has been disconnected, so HeapProfiler.startSampling cannot be posted. It is an internal-state guard protecting the allocation-sampling setup path.","triggerScenarios":"startSampling is invoked from startRun before/without a connected inspector, or from captureEpoch/restartSamplingAfterFailure after disconnectInspector() (post-stop or post-start-failure) set inspector to null.","commonSituations":"Concurrent start()/stop() races, a failed start whose cleanup ran while a queued capture still restarts sampling, or a custom createInspectorSession dependency returning null-like sessions that were disconnected.","solutions":["Ensure start() completed (state 'active') before capturing; capture() already rejects when not active","Check for overlapping start/stop calls — stop() disconnects the inspector; await the stop promise before restarting","If injecting a custom createInspectorSession, verify it returns a connected-capable session and does not disconnect early"],"exampleFix":"// before\nconst setup = createProcessMemoryDiagnosticsFromEnvironment(env);\nsetup.diagnostics.capture(); // may race stop()\nawait stopProcessMemoryDiagnosticsWithTimeout(setup.diagnostics, warn);\nsetup.diagnostics.capture();\n// after\nconst setup = createProcessMemoryDiagnosticsFromEnvironment(env);\nawait stopProcessMemoryDiagnosticsWithTimeout(setup.diagnostics, warn);\n// no capture after stop; restart with start() first if needed","handlingStrategy":"try-catch","validationCode":"const status = diagnostics.getStatus();\nif (status.state !== 'active') throw new Error(`Diagnostics not active (state=${status.state}); inspector may be disconnected`);","typeGuard":null,"tryCatchPattern":"try {\n  await startConfiguredProcessMemoryDiagnostics(setup, warn);\n} catch (error) {\n  if (error.message === 'Inspector session is unavailable.') {\n    console.error('Inspector was disconnected (previous stop/failure). Create a new diagnostics instance.');\n  } else throw error;\n}","preventionTips":["Never call start()/stop() concurrently on the same instance; await each transition","After stop(), create a fresh ProcessMemoryDiagnostics instead of relying on restart internals","If injecting createInspectorSession, return a live node:inspector Session each call","Inspect getStatus().state before any manual capture or sampling-dependent work"],"tags":["inspector","state-error","allocation-sampling"],"backgroundTag":"inspector-session-unavailable","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}