dotnet/BenchmarkDotNet · error · InvalidOperationException

The process instance is not set.

Error message

The process instance is not set.

What it means

DiagnoserActionParameters.ProcessId is a computed getter: it returns Process?.Id and throws InvalidOperationException when the Process field is null. The constructor accepts a nullable Process because some diagnosers can legitimately run without a live process (e.g. dry runs, or diagnosers that attach lazily); only the ProcessId accessor demands one.

Source

Thrown at src/BenchmarkDotNet/Diagnosers/DiagnoserActionParameters.cs:18

using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using System.Diagnostics;

namespace BenchmarkDotNet.Diagnosers
{
    public class DiagnoserActionParameters
    {
        public DiagnoserActionParameters(Process? process, BenchmarkCase benchmarkCase, BenchmarkId benchmarkId)
        {
            Process = process;
            BenchmarkCase = benchmarkCase;
            BenchmarkId = benchmarkId;
        }

        public Process? Process { get; }

        public int ProcessId => Process?.Id ?? throw new InvalidOperationException("The process instance is not set.");

        public BenchmarkCase BenchmarkCase { get; }

        public BenchmarkId BenchmarkId { get; }

        public ImmutableConfig Config => BenchmarkCase.Config;
    }
}

View on GitHub (pinned to b515068b61)

Solutions

  1. Null-check params.Process before reading ProcessId: if (params.Process is { } p) { use p.Id }.
  2. Ensure the process is started/attached before constructing DiagnoserActionParameters, passing a non-null Process.
  3. For diagnosers that can run without a process, avoid reading ProcessId on that code path.
  4. Log/warn when Process is null rather than dereferencing ProcessId unconditionally.

Example fix

// before
var id = parameters.ProcessId; // throws if Process is null

// after
if (parameters.Process is Process p)
    var id = p.Id;
else
    return; // or handle the no-process case
Defensive patterns

Strategy: validation

Validate before calling

if (parameters.Process is not Process p)
    throw new InvalidOperationException("A live process is required to read the process id.");
var id = p.Id;

Type guard

static bool HasProcess(DiagnoserActionParameters p) => p.Process is not null;

Try / catch

try { var id = parameters.ProcessId; }
catch (InvalidOperationException ex) when (ex.Message.Contains("process instance is not set"))
{
    // skip the diagnoser step that needs the id, or wait for attach
}

Prevention

When it happens

Trigger: A diagnoser constructed DiagnoserActionParameters with a null Process (process not yet started, failed to attach, or not captured) and then code read .ProcessId.

Common situations: Diagnoser code paths that assume a process exists when the benchmark process failed to launch, in-process diagnoser scenarios, or custom diagnosers that do not guard ProcessId.

Related errors


AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13). Data as JSON: /api/errors/95d4d28f5dd505d0. Report an issue: GitHub.