Cysharp/UniTask · error · ArgumentOutOfRangeException
minimumLength
Error message
minimumLength
What it means
Thrown by ArrayPool<T>.Rent(minimumLength) when minimumLength is negative. The pool sizes internal buckets by the requested length; a negative length is a programming error since no array can have negative size. This is UniTask's internal ArrayPool (mirrors System.Buffers.ArrayPool<T>) used for zero-allocation pooling.
Source
Thrown at src/UniTask/Assets/Plugins/UniTask/Runtime/Internal/ArrayPool.cs:38
readonly SpinLock[] locks;
ArrayPool()
{
// see: GetQueueIndex
buckets = new MinimumQueue<T[]>[18];
locks = new SpinLock[18];
for (int i = 0; i < buckets.Length; i++)
{
buckets[i] = new MinimumQueue<T[]>(4);
locks[i] = new SpinLock(false);
}
}
public T[] Rent(int minimumLength)
{
if (minimumLength < 0)
{
throw new ArgumentOutOfRangeException("minimumLength");
}
else if (minimumLength == 0)
{
return EmptyArray;
}
var size = CalculateSize(minimumLength);
var index = GetQueueIndex(size);
if (index != -1)
{
var q = buckets[index];
var lockTaken = false;
try
{
locks[index].Enter(ref lockTaken);
if (q.Count != 0)
{View on GitHub (pinned to ceac8d6946)
Solutions
- Clamp the value to zero before calling Rent: var len = Math.Max(0, computedLength);
- Trace the upstream calculation that produced the negative value and fix the root cause.
- Add a debug assertion or precondition check before the Rent call.
Example fix
// before var buf = ArrayPool<byte>.Shared.Rent(estimatedSize - headerSize); // throws if headerSize > estimatedSize // after var len = Math.Max(0, estimatedSize - headerSize); var buf = ArrayPool<byte>.Shared.Rent(len);
Defensive patterns
Strategy: validation
Validate before calling
var safeLength = Math.Max(0, minimumLength); var buf = ArrayPool<T>.Shared.Rent(safeLength);
Prevention
- Always clamp computed sizes with Math.Max(0, value) before passing to Rent.
- Validate parsed/serialized size fields against negative values at the trust boundary.
- Use debug assertions to catch negative sizes early during development.
When it happens
Trigger: Passing a computed negative value to ArrayPool<T>.Shared.Rent(). The pool is internal to UniTask but the guard is standard argument validation.
Common situations: A size calculation that subtracts from an uninitialized or incorrectly computed length, producing a negative result. An off-by-one in buffer sizing logic. Parsing external data that yields a negative count.
Related errors
- capacity
- EmptyQueue
- Delay does not allow minus delayFrameCount. dueTimeFrameCoun
- Delay does not allow minus periodFrameCount. periodFrameCoun
- Delay does not allow minus intervalFrameCount. intervalFrame
AI-assisted analysis of Cysharp/UniTask@ceac8d6946 (2026-08-13).
Data as JSON: /api/errors/37f5c08579c3053c.
Report an issue: GitHub.