Repository navigation
Grow QPackDecoder's Huffman buffers using ArrayPool - #138229
asylkhan-azat wants to merge 2 commits into
Conversation
|
Azure Pipelines: Successfully started running 4 pipeline(s). 11 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
|
I don't know if you're aware, but this code is also used in ASP.NET (we share it as sources, not binaries). |
Yeah, that's why the parameter is nullable and optional. If you mean that for the shared code, tehre is a different process for the PR's, then I didn't knew and sorry :) |
|
AFAIK, the shared code is propagated back and forth automatically. I'm more interested if you've looked at Kestrel side and decided it's not valuable there, or you plan to follow up there, or you weren't aware of the code sharing. |
| return j; | ||
| } | ||
|
|
||
| private static void Grow(ref byte[] dstArray, ArrayPool<byte>? pool) |
There was a problem hiding this comment.
IMO a bool to pick whether to pool or not would be better than a pool arg here, the only pool type that's ever used in practice here is ArrayPool.Shared
There was a problem hiding this comment.
The reason on why I went with pool arg was that the caller code on the decoder side held the pool as static field, which I'm passing here
There was a problem hiding this comment.
So, should I go with boolean or the pool itself?
I was aware that this is shared, but just went with the minimal amount of code in 1 repo to see if it wins anything. Not sure if the Kestrel rents from the pools too |
|
|
||
| byte[] newArray = pool.Rent(dstArray.Length * 2); | ||
| dstArray.AsSpan().CopyTo(newArray); | ||
| pool.Return(dstArray); |
There was a problem hiding this comment.
Concurrent calls from multiple threads could result in a double-free, leading to a memory safety violation. Does the calling code guarantee the necessary synchronization to prevent this? Could a threading bug in user code trigger a double-free here?
There was a problem hiding this comment.
The QPackDecoder does sync the rented array returning. I'll see if aspnetcore call sites do sync those
There was a problem hiding this comment.
Ok, I'm back from digging into ASP.NET Core and HttpClient.
This doesn't add "new" risk.
Only QPackDecoder passes the pool, and it already rents and returns these same buffers in EnsureStringCapacity. Kestrel uses the same decoder (though it never disposes it, so its last buffers never go back to the pool), but it calls it from a single read loop per stream, so concurrent calls can't happen there.
On the client side, a double return is possible if user code reads the response stream from two threads, and that could corrupt data. But that risk was already there. _recvBuffer grows the same way. It rents a bigger array and returns old one. So the same concurrent reads could already return an array twice. This change uses the same pattern that's already in place. That's why it's not "new" risk
I tried to reproduce it with Claude, with two threads reading one HTTP/3 response stream that has huffman coded trailers. The process crashed first with InvalidOperationException from the QUIC stream, because it doesn't allow two pending reads. A double return would need both threads to work on data that's already buffered at the same moment, which is a very small window.
And honestly, I'm not sure what we'll should do. AFAIK, the Stream contract says it isn't safe to access it concurrently, so code like that already breaks the contract. If we need to prevent data corruption even when user code misuses the Stream and reads it from two threads, we would need to change ArrayBuffer too or ditch the pooling approach.
There was a problem hiding this comment.
The problem is that array pool issues like this turn a local problem (e.g. single request fails with an exception) into a global process corruption (unrelated components that use array pool start misbehaving).
I agree that the existing array pool use looks problematic. The question is whether fixing this code can turn a minor problem into a serious problem. It is very hard to reason about it.
cc @EgorBo
This PR builds on this PR. Without it the decoder's buffers are never returned to the pool, so only part of the saving shows. The most wins come from dispose returning arrays to pool + renting when doubling the arrays of the decoder.
When a Huffman-coded header value doesn't fit the rented buffer,
Huffman.Decodegrows it withArray.Resize. The rented array is dropped and the new one is not from the pool. The QPackDecoder already rents from the pool, so it can return old used pool and grow from itBenchmarks and results are here:
Details
I've run this benchmark:
On this simple loopback server:
Results:
There is slight reduction o nthe execution time, but across several runs the ratio was changing between 0.98-1.03, so I suspect that it's a noise