{"record":{"id":"830d58b060637d62","repo":"dotnet/runtime","slug":"no-active-js-diagnostic-session-830d58","errorCode":null,"errorMessage":"No active JS diagnostic session","messagePattern":"No active JS diagnostic session","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/native/libs/System.Native.Browser/diagnostics/dotnet-cpu-profiler.ts","lineNumber":14,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport type { DiagnosticCommandOptions } from \"../types\";\n\nimport { commandResumeRuntime, commandStopTracing, commandSampleProfiler } from \"./client-commands\";\nimport { dotnetApi, dotnetLoaderExports, Module } from \"./cross-module\";\nimport { serverSession, setupJsClient } from \"./diagnostic-server-js\";\nimport { IDiagnosticSession } from \"./types\";\n\nexport function collectCpuSamples(options?: DiagnosticCommandOptions, startup?: boolean): Promise<Uint8Array[]> {\n    if (!options) options = {};\n    if (!startup && !serverSession) {\n        throw new Error(\"No active JS diagnostic session\");\n    }\n    if (!dotnetApi.getConfig().environmentVariables![\"DOTNET_WasmPerformanceInstrumentation\"]) {\n        throw new Error(\"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property\");\n    }\n\n    const onClosePromise = dotnetLoaderExports.createPromiseCompletionSource<Uint8Array[]>();\n    function onSessionStart(session: IDiagnosticSession): void {\n        session.sendCommand(commandResumeRuntime());\n        // stop tracing after period of monitoring\n        Module.safeSetTimeout(() => {\n            session.sendCommand(commandStopTracing(session.sessionId));\n        }, 1000 * (options?.durationSeconds ?? 60));\n    }\n\n    setupJsClient({\n        onClosePromise: onClosePromise,\n        skipDownload: options.skipDownload,\n        commandOnAdvertise: () => commandSampleProfiler(options),","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/runtime/blob/290d5ab72cc1102813fbc0406fb186ceadabc340/src/native/libs/System.Native.Browser/diagnostics/dotnet-cpu-profiler.ts#L1-L32","documentation":"Thrown by collectCpuSamples (dotnet-cpu-profiler) when called at runtime (startup=false) but serverSession is undefined: no JS diagnostic session exists to attach the CPU-sampling client to. Identical precondition to the counters variant, scoped to the CPU profiler.","triggerScenarios":"collectCpuSamples() invoked after runtime start but before the diagnostic server completed a session handshake (serverSession undefined).","commonSituations":"Calling the profiler before the runtime/session is ready; diagnostics disabled; missing DOTNET_DiagnosticPorts.","solutions":["Ensure the runtime is running and DOTNET_DiagnosticPorts is configured so a JS session exists.","Use collectCpuSamples(opts, true) to sample from startup.","Call collectCpuSamples only after a diagnostic session is established."],"exampleFix":"// before: runtime CPU profiling with no session\ncollectCpuSamples(); // throws\n// after: register at startup\ncollectCpuSamples({ durationSeconds: 30 }, true);","handlingStrategy":"validation","validationCode":"// Verify a session exists (or use startup) before CPU sampling\nimport { serverSession } from './diagnostic-server-js';\nimport { dotnetApi } from './cross-module';\nif (!dotnetApi.getConfig().environmentVariables?.DOTNET_WasmPerformanceInstrumentation) {\n  throw new Error('Enable WasmPerformanceInstrumentation before profiling.');\n}\nif (!serverSession) collectCpuSamples({ durationSeconds: 30 }, true);\nelse collectCpuSamples({ durationSeconds: 30 });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Enable <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> before relying on CPU sampling.","Use the startup=true path for from-boot CPU samples.","Confirm DOTNET_DiagnosticPorts creates a session before runtime collection."],"tags":["diagnostics","profiling","session","ordering"],"analyzedSha":"290d5ab72cc1102813fbc0406fb186ceadabc340","analyzedAt":"2026-08-06T19:57:01.276Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}