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

Source

Thrown at src/native/libs/System.Native.Browser/diagnostics/dotnet-counters.ts:14

// 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 { commandResumeRuntime, commandStopTracing, commandCounters } from "./client-commands";
import { dotnetLoaderExports, Module } from "./cross-module";
import { serverSession, setupJsClient } from "./diagnostic-server-js";
import { IDiagnosticSession } from "./types";

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

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

View on GitHub (pinned to 290d5ab72c)

Solutions

  1. Ensure the runtime is running and DOTNET_DiagnosticPorts is set so a JS session is created.
  2. Use collectMetrics(opts, true) to capture metrics from startup instead of at runtime.
  3. Call collectMetrics only after the diagnostic session has been established.

Example fix

// before: runtime call with no session yet
Module.onRuntimeInitialized = () => collectMetrics(); // throws
// after: capture at startup so the session is guaranteed
collectMetrics({ durationSeconds: 30 }, true);
Defensive patterns

Strategy: validation

Validate before calling

// Verify a session exists (or use startup) before collecting metrics
import { serverSession } from './diagnostic-server-js';
if (!serverSession) {
  // prefer the startup hook for boot-to-boot capture
  collectMetrics({ durationSeconds: 30 }, true);
} else {
  collectMetrics({ durationSeconds: 30 });
}

Prevention

When it happens

Trigger: collectMetrics() invoked after the runtime started but before the diagnostic server completed a session handshake (serverSession still undefined).

Common situations: Calling collect before the runtime is ready; diagnostics disabled (no DOTNET_DiagnosticPorts); calling right at boot instead of via the startup hook.

Related errors


AI-assisted analysis of dotnet/runtime@290d5ab72c (2026-08-06). Data as JSON: /api/errors/5d4703442ac1e2e4. Report an issue: GitHub.