{"record":{"id":"5d4703442ac1e2e4","repo":"dotnet/runtime","slug":"no-active-js-diagnostic-session-5d4703","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-counters.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, commandCounters } from \"./client-commands\";\nimport { dotnetLoaderExports, Module } from \"./cross-module\";\nimport { serverSession, setupJsClient } from \"./diagnostic-server-js\";\nimport { IDiagnosticSession } from \"./types\";\n\nexport function collectMetrics(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\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    setupJsClient({\n        onClosePromise: onClosePromise,\n        skipDownload: options.skipDownload,\n        commandOnAdvertise: () => commandCounters(options),\n        onSessionStart,\n    }, startup);\n    return onClosePromise.promise;\n}","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/runtime/blob/290d5ab72cc1102813fbc0406fb186ceadabc340/src/native/libs/System.Native.Browser/diagnostics/dotnet-counters.ts#L1-L32","documentation":"Thrown by collectMetrics (dotnet-counters) when called at runtime (startup=false) but serverSession is undefined: no JS diagnostic session has been established yet, so there is nothing to attach the counters client to.","triggerScenarios":"collectMetrics() invoked after the runtime started but before the diagnostic server completed a session handshake (serverSession still undefined).","commonSituations":"Calling collect before the runtime is ready; diagnostics disabled (no DOTNET_DiagnosticPorts); calling right at boot instead of via the startup hook.","solutions":["Ensure the runtime is running and DOTNET_DiagnosticPorts is set so a JS session is created.","Use collectMetrics(opts, true) to capture metrics from startup instead of at runtime.","Call collectMetrics only after the diagnostic session has been established."],"exampleFix":"// before: runtime call with no session yet\nModule.onRuntimeInitialized = () => collectMetrics(); // throws\n// after: capture at startup so the session is guaranteed\ncollectMetrics({ durationSeconds: 30 }, true);","handlingStrategy":"validation","validationCode":"// Verify a session exists (or use startup) before collecting metrics\nimport { serverSession } from './diagnostic-server-js';\nif (!serverSession) {\n  // prefer the startup hook for boot-to-boot capture\n  collectMetrics({ durationSeconds: 30 }, true);\n} else {\n  collectMetrics({ durationSeconds: 30 });\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the startup=true path when you need metrics from the very beginning.","Ensure DOTNET_DiagnosticPorts is set so a JS session is created at boot.","Gate runtime collect calls on a known session state."],"tags":["diagnostics","counters","session","ordering"],"analyzedSha":"290d5ab72cc1102813fbc0406fb186ceadabc340","analyzedAt":"2026-08-06T19:57:01.276Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}