|
| 1 | +# Nethermind PatriciaTree ArrayPool leak (leak on the exception path) |
| 2 | + |
| 3 | +**Source.** [NethermindEth/nethermind#9322](https://github.com/NethermindEth/nethermind/pull/9322), |
| 4 | +fixed in Nethermind **v1.35.0**. File `src/Nethermind/Nethermind.Trie/PatriciaTree.cs`, |
| 5 | +methods `Get` / `GetNodeByKey`. (A replay target named in |
| 6 | +[P-007](../../../docs/proposals/P-007-arraypool-span.md): *Nethermind ArrayPool leaks*.) |
| 7 | + |
| 8 | +**Pattern.** For keys longer than the 64-byte `stackalloc` minimum the methods |
| 9 | +`Rent` a `byte[]` from `ArrayPool<byte>.Shared`, then call `GetNew`, which can |
| 10 | +throw `TrieException`. The buffer was `Return`ed only on the success path (after |
| 11 | +the call), so a thrown `TrieException` skipped the `Return` and **leaked** the |
| 12 | +rented buffer — gradual memory pressure under sustained RPC load with long keys. |
| 13 | +The fix moves the `Return` into a `finally` block (`after.cs`). |
| 14 | + |
| 15 | +**What the checker says.** The OwnLang reduction trips **OWN001** (owned resource |
| 16 | +not released on all paths): `acquire` is `ArrayPool.Rent`, `release` is `.Return`, |
| 17 | +and the `throw` path is modeled as an early `return` arm that exits before the |
| 18 | +`release`. |
| 19 | + |
| 20 | +```text |
| 21 | +$ python -m ownlang check corpus/real-world/nethermind-patriciatree-arraypool-leak/case.own |
| 22 | +case.own:22: error: [OWN001] 'array' is owned but not released before return (leaks on at least one path) |
| 23 | +``` |
| 24 | + |
| 25 | +**Honesty / scope.** `case.own` is a *hand reduction* of the C# pattern, not direct |
| 26 | +extractor output. The uncaught C# `throw` on `GetNew`'s `TrieException` is modeled |
| 27 | +as an early `return` arm — a path that exits before cleanup, which is exactly what |
| 28 | +an uncaught throw is. |
| 29 | + |
| 30 | +End to end, `before.cs` keeps the `Return` **inside the `try`** after the may-throw |
| 31 | +`GetNew` (the real structure the PR fixed): that is what the default throw-edge |
| 32 | +model sees — `scripts/benchmark.py` runs own-check with `--flow-locals`, which |
| 33 | +injects a throw exit before each may-throw leaf **in a `try`** (a body-level |
| 34 | +may-throw call *outside* a `try` would need `--body-throw-edges`, which the |
| 35 | +benchmark does not pass). `after.cs` returns the buffer in a `finally`, so it is |
| 36 | +returned on every path and the fix is silent. Both are scored by the |
| 37 | +`corpus-benchmark` CI job (SDK-backed; not runnable in this Python-only checkout — |
| 38 | +recall there is a tracked floor, specificity is absolute). `before.cs` / `after.cs` |
| 39 | +are reduced (helpers stubbed), not a verbatim copy of the PR diff. |
0 commit comments