skip to content

A C# upload service pools 64 KB buffers with ArrayPool<byte>.Shared, and forwarded chunks sometimes carry another upload's bytes; which rental bugs cause this?

level: seniorimportance: should knowfreq 32%

answer

  1. two users of one array
  2. a second Return
  3. a slice that outlives the rental
  4. the pool trusts its callers

basics

~20 s

Two code paths share one array: a double Return let the pool hand it to two renters, or a stored Memory<byte> or unawaited task still used it after Return. ArrayPool<T>.Shared detects neither; return exactly once, after the last use.

solid answer

~50 s

The `ArrayPool<T>.Return` contract says the caller gives up all ownership on return and must return a rental only once. The shared pool checks neither: it validates null and the array's length, nothing more. In the .NET 10 source, a second `Return` on the same thread, with no rental in between, stores the array again and pushes the earlier copy into the shared stack, so the next two `Rent` calls can get the same array. The other cause is a view that outlives the rental: a `buffer.AsMemory(0, read)` queued for a forwarder, an unawaited `WriteAsync`, or a stored `ArraySegment<byte>`, all still pointing at an array that the `finally` already returned and someone else re-rented. Fix it with one owner per rental, `Return` in a `finally` after the last awaited use, and explicit ownership transfer (an `IMemoryOwner<byte>`) when a chunk must outlive the method. `clearArray: true` only changes what the bug reads.

code

csharp · 24 lines
csharp
using System.Buffers;

byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
ArrayPool<byte>.Shared.Return(buffer);
ArrayPool<byte>.Shared.Return(buffer); // bug: second Return of the same rental

byte[] requestA = ArrayPool<byte>.Shared.Rent(4096);
byte[] requestB = ArrayPool<byte>.Shared.Rent(4096);
Console.WriteLine($"A and B share one array: {ReferenceEquals(requestA, requestB)}");

requestA[0] = 1;
requestB[0] = 2;
Console.WriteLine($"requestA[0] = {requestA[0]}");

try
{
    ArrayPool<byte>.Shared.Return(new byte[1000]);
}
catch (ArgumentException e)
{
    Console.WriteLine($"{e.GetType().Name}: {e.Message}");
}
ArrayPool<byte>.Shared.Return(new byte[1024]);
Console.WriteLine("a foreign 1024-byte array was accepted");

go deeper

for a junior

Remember the rule printed on the API: return a rented array exactly once, and never touch it after returning it.

for a middle

Explain why the pool cannot help: it checks only null and the array length, so a double return or a stale reference goes unnoticed.

for a senior

Diagnose the race from symptoms, trace every view of the buffer to its last use, and fix it with one owner, awaited uses and explicit ownership transfer.

for a principal

Weigh where pooled buffers may cross component boundaries at all, and require a checking pool in tests wherever they do.

## The contract the pool states The remarks on `ArrayPool<T>.Return` in the .NET source are blunt: "Once a buffer has been returned to the pool, the caller gives up all ownership of the buffer and must not use it. The reference returned from a given call to `Rent` must only be returned via `Return` once." When an upload's chunk carries bytes from a different upload, one of those two rules has been broken, because the only way two uploads can see each other's data is for two pieces of code to be using the same array at the same time. ## What the shared pool actually checks The pool trusts its callers. In the .NET 10 source, `SharedArrayPool<T>.Return` checks only that the array is not null and, for lengths inside its bucket range, that the length is exactly a bucket size; it keeps no record of which arrays are out on loan. | Misuse | What `ArrayPool<byte>.Shared` does | |---|---| | `Return(null)` | throws `ArgumentNullException` | | Returning a foreign array whose length is not a bucket size (`new byte[1000]`) | throws `ArgumentException`: "The buffer is not associated with this pool and may not be returned to it." | | Returning a foreign array of a bucket size (`new byte[1024]`) | accepted silently | | Returning the same rental twice | accepted silently; the array can be stored twice | | Using the array after `Return` | nothing; it is an ordinary array | | Never returning it | not fatal; the garbage collector reclaims it and the pool allocates a replacement | So the two bugs that leak data between requests are invisible to the pool itself. ## Bug 1: returning twice A double `Return` usually comes from two cleanup paths: a `finally` plus an error handler, a `Dispose` that can run twice, or a helper that returns the buffer while its caller also does. In the .NET 10 shared pool, when nothing has rented the array in between, the second `Return` on the same thread puts it into the per-thread slot again and pushes the earlier copy into the shared per-core stack, so the pool now holds one array twice. The next two `Rent` calls of that size can both receive it, and two concurrent uploads write into the same 64 KB. ## Bug 2: a view outlives the rental Every `Memory<byte>`, `ArraySegment<byte>` or captured `byte[]` that still points at the array after `Return` is a reader of whatever the next renter writes. Common shapes: - A chunk is enqueued as `buffer.AsMemory(0, read)` for a background forwarder, and the reading method returns the buffer in `finally` before the forwarder runs. - A `WriteAsync` or hashing task is started but not awaited before the `finally` returns the buffer. - A parser keeps an `ArraySegment<byte>` of the last message in a field. The compiler cannot catch these, because `Memory<T>` and arrays are allowed to be stored; only `Span<T>` gets compile-time lifetime checks, and even those know nothing about a pool's `Return`. ## Fixing it 1. **One owner per rental.** The method that calls `Rent` calls `Return`, exactly once, in a `finally` that runs after the last awaited use. 2. **Make the last use awaited.** Await every write, hash or forward that touches the buffer before the `finally` runs. 3. **Drop the reference.** Set the local to `null` (or `Array.Empty<byte>()`) right after `Return` so a later line cannot touch it. 4. **Transfer ownership explicitly.** When a chunk must outlive the reading method, wrap it in an owner, such as an `IMemoryOwner<byte>` from `MemoryPool<byte>.Shared.Rent`, or your own disposable type, hand that to the consumer, and let the consumer release it after its last read. 5. **Copy when in doubt.** If a consumer's lifetime is unknown, give it a copy (`ToArray()`) and keep the pooled buffer local. `clearArray: true` is not a fix: it clears the array at `Return`, but the stale reader then sees zeros or the next renter's bytes instead. ## Finding it in a running service These bugs are races, so they appear under load and vanish in a debugger. In tests, route rentals through an injected `ArrayPool<byte>` subclass that remembers outstanding arrays, throws on a second `Return`, and fills returned arrays with a poison byte so a stale reader produces obviously wrong output. The runtime's `System.Buffers.ArrayPoolEventSource` reports rentals, allocations and drops, which helps size a pool but cannot reveal a double return; event tracing tools themselves belong to the diagnostics material.

  • Why did adding clearArray: true not stop the leak?
    Because the bug is sharing, not leftovers. `clearArray: true` clears the array at `Return`; the second renter then writes its own data into the same array, and the stale reader sees zeros or that data. It can hide the symptom in a quiet test while the race stays in production.
  • How do you let a pooled chunk safely outlive the method that rented it?
    Transfer ownership explicitly. Rent an `IMemoryOwner<byte>` (or wrap the array in your own disposable type), pass the owner to the consumer, and stop using it in the producer. The consumer disposes the owner after its last read, which returns the array exactly once.
  • Can a private pool from ArrayPool<T>.Create() catch a double Return instead?
    No. Its `Return` checks that the length matches a bucket and whether the bucket has room; it does not track outstanding arrays either. If the bucket has room, the duplicate is stored like any other array.

  • ArrayPool throws if the same array is returned twice.
  • clearArray: true stops data leaking between requests.
  • A Memory<byte> slice keeps its rented array out of the pool until collected.
  • Returning in finally is safe even while a started, unawaited write still uses the buffer.
  • A rented array that is never returned is leaked for the life of the process.

Assumes .NET 10; the return-once rule is the documented contract, while what a double Return does inside the shared pool was read from the .NET 10 source.