elsa-workflows/elsa-core · error · ArgumentOutOfRangeException

Capacity must be greater than zero.

Error message

Capacity must be greater than zero.

What it means

The RingBuffer<T> constructor requires a strictly positive capacity. A capacity of zero or a negative value cannot back a bounded ring, so the constructor throws ArgumentOutOfRangeException naming the capacity parameter before any state is stored.

Solutions

  1. Pass a positive capacity, e.g. new RingBuffer<LogRecord>(1000).
  2. Fix the bound configuration so the capacity option defaults to a positive value or is explicitly set.
  3. Clamp/validate configured values at options validation time so 0 never reaches the constructor.

Example fix

// before
var buffer = new RingBuffer<LogRecord>(config.BufferCapacity); // 0 when unset

// after
var capacity = Math.Max(1, config.BufferCapacity);
var buffer = new RingBuffer<LogRecord>(capacity);
Defensive patterns

Strategy: validation

Validate before calling

if (capacity <= 0)
    throw new InvalidOperationException($"Ring buffer capacity must be positive, got {capacity}.");
var buffer = new RingBuffer<T>(capacity);

Try / catch

try { buffer = new RingBuffer<T>(configuredCapacity); }
catch (ArgumentOutOfRangeException ex) { buffer = new RingBuffer<T>(DefaultCapacity); }

Prevention

When it happens

Trigger: Constructing new RingBuffer<T>(0), a negative size, or registering an OpenTelemetry in-memory provider whose configured buffer capacity option resolves to 0/negative (e.g. unset int config bound to default(int)).

Common situations: Appsettings binding where the capacity key is missing and the property defaults to 0; arithmetic computing capacity (e.g. size in MB * 1024) evaluating to 0; copying sample config with capacity commented out.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/f69eee6e2b09840f. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Diagnostics.OpenTelemetry/Providers/InMemory/RingBuffer.cs:13

namespace Elsa.Diagnostics.OpenTelemetry.Providers.InMemory;

public class RingBuffer<T>
{
    private readonly Queue<T> _items = new();
    private readonly object _lock = new();
    private readonly int _capacity;
    private long _droppedCount;

    public RingBuffer(int capacity)
    {
        if (capacity <= 0)
            throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");

        _capacity = capacity;
    }

    public long DroppedCount
    {
        get
        {
            lock (_lock)
                return _droppedCount;
        }
    }

    public void Add(T item)
    {
        lock (_lock)
        {
            if (_items.Count == _capacity)

View on GitHub (pinned to fe9217bdfa)