stride3d/stride · error · InvalidOperationException

The queue is empty

Error message

The queue is empty

What it means

InsertionQueue<T>.Dequeue removes and returns the front item; when Count is 0 there is nothing to return, so it throws InvalidOperationException. This is a usage-contract violation: callers must only dequeue when the queue is non-empty.

Solutions

  1. Check queue.Count > 0 before calling Dequeue()
  2. Guard the drain loop with while (queue.Count > 0) instead of a fixed iteration count
  3. Wrap in try/catch (InvalidOperationException) if emptiness is an expected boundary condition

Example fix

// before
while (true) { var item = queue.Dequeue(); ... }
// after
while (queue.Count > 0) { var item = queue.Dequeue(); ... }
Defensive patterns

Strategy: validation

Validate before calling

if (queue.Count == 0) return default; // or skip
var item = queue.Dequeue();

Try / catch

try { var item = queue.Dequeue(); } catch (InvalidOperationException) { /* queue was empty */ }

Prevention

When it happens

Trigger: Calling Dequeue() on an InsertionQueue<T> with Count == 0 — e.g. dequeuing in a loop without checking Count, or dequeuing more times than items were enqueued.

Common situations: Event-queue draining loops in YAML processing that don't check emptiness, off-by-one in consumer/producer counts, re-entrancy draining the same queue twice.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/abcdae170c72939d. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Yaml/InsertionQueue.cs:82

        /// <summary>
        /// Enqueues the specified item.
        /// </summary>
        /// <param name="item">The item to be enqueued.</param>
        public void Enqueue(T item)
        {
            items.Add(item);
        }

        /// <summary>
        /// Dequeues an item.
        /// </summary>
        /// <returns>Returns the item that been dequeued.</returns>
        public T Dequeue()
        {
            if (Count == 0)
            {
                throw new InvalidOperationException("The queue is empty");
            }

            T item = items[0];
            items.RemoveAt(0);
            return item;
        }

        /// <summary>
        /// Inserts an item at the specified index.
        /// </summary>
        /// <param name="index">The index where to insert the item.</param>
        /// <param name="item">The item to be inserted.</param>
        public void Insert(int index, T item)
        {
            items.Insert(index, item);
        }
    }
}

View on GitHub (pinned to 96fad776d2)