{"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":"exception","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/60108ba66eb7d1d12f595480091b4ad80a24b172/src/native/libs/System.Native.Browser/diagnostics/dotnet-cpu-profiler.ts#L1-L32","documentation":"collectCpuSamples() has the identical guard as collectMetrics: it throws when startup=false and serverSession is undefined. CPU sampling needs an active diagnostic session to deliver SampleProfiler commands. The check fires before the WasmPerformanceInstrumentation check (error 119), so sessions must be active first.","triggerScenarios":"Calling collectCpuSamples() before runtime ready, before any advert, or after session close. Same as 117 but for the cpu profiler entry point.","commonSituations":"Profiling from a UI button before runtime finished booting. Diagnostics module not included in the build. Runtime crashed and session was torn down.","solutions":["Await runtime creation: const rt = await dotnet.create(); await collectCpuSamples();","Use startup=true to register before boot: collectCpuSamples({}, true) — note you still need WasmPerformanceInstrumentation enabled (else 119).","Confirm resources.jsModuleDiagnostics is present in the boot config.","If runtime exited, restart it before sampling."],"exampleFix":"// before\ncollectCpuSamples(); // serverSession undefined\n\n// after\nawait dotnet.create();\nawait collectCpuSamples({ durationSeconds: 30 });","handlingStrategy":"validation","validationCode":"function canCollectCpuSamples(): boolean {\n  return !!serverSession\n      && !!dotnetApi.getConfig().environmentVariables?.['DOTNET_WasmPerformanceInstrumentation'];\n}","typeGuard":"function isSessionReady(): boolean { return !!serverSession; }","tryCatchPattern":"try { await collectCpuSamples(); } catch (e) {\n  if (/No active JS diagnostic session/.test((e as Error).message)) {\n    await dotnet.create();\n    await collectCpuSamples();\n  } else throw e;\n}","preventionTips":["Await dotnet.create() before invoking collectCpuSamples().","Ensure the diagnostics JS module is bundled (resources.jsModuleDiagnostics non-empty).","Use startup=true when profiling from boot.","Detect a dead runtime and stop offering the profiling action."],"tags":["diagnostics","profiler","session","lifecycle"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}