Bound memory on clone/fetch/push/fsck/LFS/size paths (anvil#422) #59

open colechristensen cole.christensen@gmail.com wants to merge fix/object-resolver-bounded-walk into main

Part of anvil#422 (bounded memory). Context: on 2026-11-25 prod Anvil was OOM-killed 29 times. A hephaestus clone held +1,294 MB in the request process because ObjectResolver cached whole packfiles (anvil#421 / anvil PR #284 fixed the HTTP buffering on top of that).

Bug class

A process holds memory proportional to a whole artifact (pack, push, response, blob) when a bounded or streaming alternative exists. This PR removes every such site in the library on the clone, fetch, push, fsck, LFS, size and diff paths.

Changes (one commit each)

commit what was unbounded now
object_resolver escalated to get_pack (whole file, cached for the process lifetime) after 64 reads block-aligned LRU over get_pack_range, default 1 MiB blocks / 32 MiB budget; huge windows bypass the cache
storage/s3 stream_pack GET whole pack, wrapped in a list lazy ranged chunks
upload_pack_v2 reuse get_pack whole pack → one sideband payload streamed through stream_pack
upload_pack_v2 feed/2 separate buffered implementation, about 4× the pack thin wrapper over feed/3; 183 lines of duplicate code deleted
upload_pack v1 + walks v1 had no streaming; walk read every blob’s content; walks kept commit/tree/tag content v1 feed/3 streams; walk entries carry no content; shared Protocol.PackStream
receive-pack / reader delta-base LRU bounded by entry count (each entry up to 128 MB); telemetry closure kept the push iolist alive; iolist→binary 2× byte-bounded LRU (32 MiB); push spooled to an unlinked raw temp file and read back once
fsck get_pack whole pack to hash Reader.verify_pack_checksum_stream/1
protocol buffers v2/v1/receive-pack command buffers grew until a flush arrived PktLine.max_request_bytes/0 (8 MiB) aborts with an error
lfs/s3 get/3 whole GET, stream: [body] lazy ranged chunks
blob_size/2 loaded the whole blob (callers’ size gates were fake; blob_sizes/3 did this ×16) header-only ObjectResolver.read_info/2 (pack entry header / delta size varints / loose header prefix)
Diff.diff_blobs max_bytes, filter blob:limit loaded content and then compared its size header sizes first
lfs/filesystem put/4 a raising input stream removed the temp file but leaked its fd (one per failed upload) fd closed and the failure logged
mix.lock hex.audit failed on main (bandit, ex_aws, ex_aws_s3, plug advisories), so CI audit, dialyzer and test were cancelled for every branch bumped; audit exits 0

Evidence (measured)

Full 15-want v2 clone of hephaestus (41,140 objects, 522 MB pack):

backend before (main) after
Filesystem +1,294 MB peak binary, 3 whole packs, 34.8 s +161 MB, 0 whole packs, ~35 s
S3 (MinIO) +1,820 MB, 3 whole-pack GETs, 371 ranged, 31.3 s +194 MB, 0 whole, 395 ranged, 31.7 s
single-pack reuse (213 MB pack) +612 MB, 2.9 s +72 MB, 3.3 s

The remainder is the 32 MiB block cache plus the largest in-flight objects (about 25 MB PDFs). Correctness: the streamed clone was verified with git index-pack --strict (41,140 objects) and a connectivity fsck --full --strict from all 15 tips, with exactly 41,140 objects reachable.

Each fix has a test that was red before the fix, most of them peak-sampling global binary memory in async: false modules:

case before after
bulk reader held the whole 12.4 MB pack ≤ budget + one block
receive-pack, 91 MB push +257 MB ≤ 1× pack + 32 MiB
delta-base cache, 194 KB pack held 191 MB within budget
walk over 49 MB of trees +54 MB < ¼ of that
blob_size of a 20 MB blob +15.9 MB packed / +40 MB loose < 4 MB
diff_blobs stub of 2× 20 MB +57 MB < 4 MB
blob:limit excluding a 20 MB blob +19 MB < 4 MB

Behaviour stays locked by byte-identical feed/2 vs feed/3 characterization tests across clone, incremental, shallow, filtered, wait-for-done, bad-filter, multi-round and multi-frame fetches, plus real-git interop tests.

Full suite: 1197/1197 including :s3 against MinIO; dialyzer 0 errors. compile --warnings-as-errors and mix format are clean, and every touched file is credo --strict clean. That includes the pre-existing findings in those files, which are fixed.

Not in this PR (tracked on anvil#422)

  • Repo-wide credo --strict still reports about 40 pre-existing findings, mostly in diff/myers.ex (arity-17 functions). Refactoring the tuned Myers diff is its own change.
  • The fetch walkers are quadratic (objs ++ acc through parents). This is CPU, not memory: ~2.6 s of the hephaestus clone.
  • Count-bounded caches: Graph.Cache persistent_term (no eviction), the ETS cache by entries, and Walk path-filter commit caching. These are O(metadata), not O(artifact).
  • The upload-pack walk silently omits unreadable objects (anvil#423, data integrity). Blobs now fail loudly via PackStream, but commits and trees can still be skipped.
  • The Reader’s ×4 window growth over-reads up to 4× the largest object.

Merge note

anvil PR #285 pins ex_git_objectstore to 3e75f5d on this branch, so its CI exercises the combination. The later commit here (4dbc52b) only changes this repo’s mix.lock. After this merges, that ref: will be repointed to the resulting main commit.

🤖 Generated with Claude Code

Created Nov 26, 2026 at 00:10 UTC