dotnet/runtime · error · Error

No active JS diagnostic session

Error message

No active JS diagnostic session

What it means

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.

Solutions

  1. Wait for the runtime to be ready and the diagnostic advert to arrive before calling `collectMetrics`.
  2. Ensure the runtime was published with diagnostics support enabled.
  3. Verify `DOTNET_DiagnosticPorts` configuration points to the JS transport so an advert is produced.

Example fix

// before: called before runtime ready
const m = collectMetrics({ durationSeconds: 5 }); // throws

// after: wait for runtime + advert, then collect
await dotnet.ready;
await waitForDiagAdvert();
const m = collectMetrics({ durationSeconds: 5 });
Defensive patterns

Strategy: validation

Validate before calling

function canCollectMetrics () {
  return !!serverSession;
}
if (canCollectMetrics()) collectMetrics({ durationSeconds: 5 });

Type guard

function hasActiveDiagSession (): boolean {
  return typeof serverSession !== 'undefined' && serverSession !== null;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of dotnet/runtime@60108ba66e (2026-08-10). Data as JSON: /api/errors/2c59cd9af0f0a8c1. Report an issue: GitHub.

Appendix: source

Thrown at src/mono/browser/runtime/diagnostics/dotnet-counters.ts:15

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { DiagnosticCommandOptions } from "../types";

import { commandStopTracing, commandCounters } from "./client-commands";
import { IDiagnosticSession } from "./common";
import { Module } from "./globals";
import { serverSession, setupJsClient } from "./diagnostics-js";
import { loaderHelpers } from "./globals";

export function collectMetrics (options?:DiagnosticCommandOptions):Promise<Uint8Array[]> {
    if (!options) options = {};
    if (!serverSession) {
        throw new Error("No active JS diagnostic session");
    }

    const onClosePromise = loaderHelpers.createPromiseController<Uint8Array[]>();
    function onSessionStart (session: IDiagnosticSession): void {
        // stop tracing after period of monitoring
        Module.safeSetTimeout(() => {
            session.sendCommand(commandStopTracing(session.session_id));
        }, 1000 * (options?.durationSeconds ?? 60));
    }
    setupJsClient({
        onClosePromise:onClosePromise.promise_control,
        skipDownload:options.skipDownload,
        commandOnAdvertise:() => commandCounters(options),
        onSessionStart,
    });
    return onClosePromise.promise;
}

View on GitHub (pinned to 60108ba66e)