Skip to content

PriorityQueue<TElement, TPriority> edge case Count out of sync #77212

@theodorzoulias

Description

@theodorzoulias

Description

I found an edge case in the PriorityQueue<TElement, TPriority> collection, where enqueuing an element
might fail, but nonetheless the Count increases.

Reproduction Steps

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        PriorityQueue<int, int> queue = new(new (int, int)[300_000_000]);
        Console.WriteLine($"Before Enqueue, Count: {queue.Count:#,0}");
        try
        {
            queue.Enqueue(1, 1);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.WriteLine($"After Enqueue, Count: {queue.Count:#,0}");
    }
}

In my PC I get this output, after around 30 seconds of execution time:

Before Enqueue, Count: 300,000,000
Exception of type 'System.OutOfMemoryException' was thrown.
After Enqueue, Count: 300,000,001

Expected behavior

After failing to Enqueue, the Count should preserve its previous value (300,000,000).

Actual behavior

The Count increases to 300,000,001.

Regression?

No response

Known Workarounds

No response

Configuration

  • .NET 6.0
  • Console application
  • 64-bit operating system, x64-based processor

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions