{"record":{"id":"2c59cd9af0f0a8c1","repo":"dotnet/runtime","slug":"no-active-js-diagnostic-session","errorCode":null,"errorMessage":"No active JS diagnostic session","messagePattern":"No active JS diagnostic session","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mono/browser/runtime/diagnostics/dotnet-counters.ts","lineNumber":15,"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 { commandStopTracing, commandCounters } from \"./client-commands\";\nimport { IDiagnosticSession } from \"./common\";\nimport { Module } from \"./globals\";\nimport { serverSession, setupJsClient } from \"./diagnostics-js\";\nimport { loaderHelpers } from \"./globals\";\n\nexport function collectMetrics (options?:DiagnosticCommandOptions):Promise<Uint8Array[]> {\n    if (!options) options = {};\n    if (!serverSession) {\n        throw new Error(\"No active JS diagnostic session\");\n    }\n\n    const onClosePromise = loaderHelpers.createPromiseController<Uint8Array[]>();\n    function onSessionStart (session: IDiagnosticSession): void {\n        // stop tracing after period of monitoring\n        Module.safeSetTimeout(() => {\n            session.sendCommand(commandStopTracing(session.session_id));\n        }, 1000 * (options?.durationSeconds ?? 60));\n    }\n    setupJsClient({\n        onClosePromise:onClosePromise.promise_control,\n        skipDownload:options.skipDownload,\n        commandOnAdvertise:() => commandCounters(options),\n        onSessionStart,\n    });\n    return onClosePromise.promise;\n}\n","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/runtime/blob/60108ba66eb7d1d12f595480091b4ad80a24b172/src/mono/browser/runtime/diagnostics/dotnet-counters.ts#L1-L33","documentation":"Thrown by `collectMetrics` (dotnet-counters) when there is no active JS diagnostic session. The function relies on `serverSession` (the singleton session established when the runtime's diagnostic server sent an advert message); without it, there is no transport to send the counters command and the call cannot proceed.","triggerScenarios":"Calling `collectMetrics(options)` (dotnet-counters.ts:12) before the runtime has advertised a diagnostic session — i.e. before `serverSession` is set in diagnostics-js.ts:88. Also thrown if the runtime was built without diagnostics enabled, or if the runtime hasn't finished starting up.","commonSituations":"Calling `collectMetrics` at app startup before the runtime is ready; runtime build with diagnostics disabled; the diagnostic server in the runtime never advertised (e.g. DOTNET_DiagnosticPorts misconfigured).","solutions":["Wait for the runtime to be ready and the diagnostic advert to arrive before calling `collectMetrics`.","Ensure the runtime was published with diagnostics support enabled.","Verify `DOTNET_DiagnosticPorts` configuration points to the JS transport so an advert is produced."],"exampleFix":"// before: called before runtime ready\nconst m = collectMetrics({ durationSeconds: 5 }); // throws\n\n// after: wait for runtime + advert, then collect\nawait dotnet.ready;\nawait waitForDiagAdvert();\nconst m = collectMetrics({ durationSeconds: 5 });","handlingStrategy":"validation","validationCode":"function canCollectMetrics () {\n  return !!serverSession;\n}\nif (canCollectMetrics()) collectMetrics({ durationSeconds: 5 });","typeGuard":"function hasActiveDiagSession (): boolean {\n  return typeof serverSession !== 'undefined' && serverSession !== null;\n}","tryCatchPattern":null,"preventionTips":["Wait for the diagnostic advert before offering counters in the UI.","Publish the runtime with diagnostics enabled.","Configure `DOTNET_DiagnosticPorts` so the runtime advertises a JS session."],"tags":["diagnostics","dotnet-counters","session-management","startup"],"backgroundTag":null,"analyzedSha":"60108ba66eb7d1d12f595480091b4ad80a24b172","analyzedAt":"2026-08-10T18:54:11.478Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}