Repository navigation
provider: release each chunk of a chunked file read - #1836
Merged
Merged
Conversation
On Darwin, FileHandle.read(upToCount:) and readData(ofLength:) return autoreleased storage. A Swift concurrency thread drains no autorelease pool between jobs, and a Dispatch block drains none before it returns, so a loop that reads a file or a pipe in chunks kept every chunk resident until its thread went idle: memory grew with the bytes read. FileHandle.readDrained(upToCount:) (ProviderCoreFoundation) runs each read in its own pool and is otherwise read(upToCount:). The chunked loops now use it: ManifestBuilder.hashOne, SpecDecStore .digestRegularFile, the metallib snapshot copy, SSDBlockStore .readExactly (the streamed KV restore), LiveContext.workerSHA256, MTPBenchmarkDigest.sha256(file:), DesktopWorker and SandboxChildProcess. sha256Digest(of:) keeps readData(ofLength:) inside a pool, and WorkerCodeIdentity, which cannot depend on ProviderCoreFoundation, pools its reads inline. Digests, copied bytes, errors and cancellation are unchanged. Tests read 256 MiB sparse files (or child output) through each site and check the known digest and less than 64 MiB of footprint growth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
DesktopOutputBuffer kept its last 32 KiB as `data.suffix(_:)` and SandboxOutputTail as `removeFirst(_:)`. Both make a slice of the same storage, and each later append grows that storage, so the tail held every byte the child wrote. ChildOutputReadMemoryTests found it: 256 MiB of output left 258 MiB resident with drained reads. Each tail is now a copy of its last bytes; the text it gives is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
StderrTailBox trimmed with removeFirst(_:), which slices: each later append grew the shared storage, so the tail kept all of a child's stderr. It now keeps a copy of its last bytes; the reported text is unchanged. The readabilityHandler's availableData read runs in its own autorelease pool, as the other chunked reads do. BoundedProcessStderrMemoryTests runs a real child that writes 256 MiB to stderr and checks the footprint while it runs, then its exit status and tail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DesktopOutputBuffer, SandboxOutputTail and StderrTailBox each trimmed their tail with the same copy-not-slice step and the same comment. One ProviderCoreFoundation helper, Data.keepLast(_:), now holds that step and its reason; the bytes kept and the truncation flag do not change. Also: the inference doc names the worker digest key's modification time (st_mtimespec), not change time, and WorkerCodeIdentity says why its read pool is inline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
davidtai
added a commit
that referenced
this pull request
Nov 6, 2026
Brings #1838 (measured job sizes), #1839 (stream failure frame before the pair retires), #1836 (drained chunked reads) and #1848 (GLM video readiness test disabled). Clean merge; Package.swift and BuildEnvironmentTests.swift stay equal to master, MLX pins unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 6, 2026
This branch had an error being deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
On Darwin,
FileHandle.read(upToCount:)andreadData(ofLength:)return autoreleased storage. A Swift concurrency thread drains no autorelease pool between jobs, and a Dispatch block drains none before it returns. A loop that reads a file or a pipe in chunks therefore kept every chunk resident until its thread went idle, so memory grew with the bytes read. #1829 found this in the update hashes (one test process reached 14.96 GiB). This PR fixes the same pattern in the other sites. It does not touch #1829's files.Fix
FileHandle.readDrained(upToCount:)(ProviderCoreFoundation/FileHandle+DrainedRead.swift) isread(upToCount:)inside its ownautoreleasepool. It has the same errors, the same end of file (nilor empty) and the same bytes. On Linux (darkbloom-publish) it is a plain read. Each call site keeps its own error mapping and cancellation checks.The first CI run found a second cause in the two child-output readers.
DesktopOutputBufferkept its tail asdata.suffix(32_768), andSandboxOutputTailusedremoveFirst(_:). Both make a slice of the same storage, and each later append grows that storage, so the "32 KiB" tail held every byte the child wrote: 256 MiB of output left 258 MiB resident. Each tail is nowData(data.suffix(limit)), a copy. The text is unchanged.BoundedProcess'sStderrTailBoxhad the sameremoveFirst(_:)trimming and now copies the same way; itsreadabilityHandlerread (availableData) also runs in its own pool, because the handler's queue may run many callbacks before it drains.Sites
ManifestBuilder.hashOne(publish, 64 KiB reads)readDrainedSpecDecStore.digestRegularFile(1 MiB)readDrained; the cancellation check before each read is unchangedBinaryHasher.sha256Digest(of:)(64 KiBreadData(ofLength:);hashFile(atPath:),SecurityFoundation)readData(ofLength:)insideautoreleasepool(it keeps its non-throwing API)makeRuntimeMetallibSnapshot, the metallib copy loop (1 MiB)readDrained, and the write in its own poolSSDBlockStore.readExactly, called once per chunk by the streamed KV restore (SSDBlockChunkCursor,readStreaming)readDrained. A restore reads every chunk of a checkpoint file in one call, and the uncopied chunk it returns stayed resident after the caller dropped itLiveContext.workerSHA256(4 MiB reads, worker of tens of MB)readDrainedMTPBenchmarkDigest.sha256(file:)(weight files)readDrainedWorkerCodeIdentity.inspect, the executable hash (1 MiB)WorkerCodeIdentity.sha256Hex(of:)with an inlineautoreleasepool(ProviderAppAttestdoes not depend onProviderCoreFoundation)DesktopWorker.runpipe reader (4 KiB, one Dispatch block for the child's life; keeps a 32 KiB tail)readDrainedSandboxChildProcess.readpipe reader (16 KiB, same shape; tail or lines)readDrainedUpdateAtomicFilesystem.sha256(file:)/treeHash(root:)WeightHasher.hashSingleFileViaHandlewithAutoreleasePool)WeightHasherInputStream path,OverlappedFileDigestInputStream.read,pread); no Foundation object per chunkWorkerTransport.readExactlyDarwin.readinto oneDataClusterLogScan.lastLinelimitBytes(8 MiB), whatever the log sizeTeacherForcedBenchmarkInput.readBounded,SSDCacheEpochStore.readRecordmaxRecordBytestotalBoundedProcessstderr (availableDatain areadabilityHandler,StderrTailBox)FanProcessRunner(availableData)FanProcessOutputBoxstops appending at 64 KiB, so it holds at most that; short helper output with a 15 s limitModelScanner,ClusterRuntimeApprovalFacts,Qwen4ExpMmapFootprint,SpecDecStoreheader,DistributedEngineFactory,KVEstimation,DeviceAuth,OwnerOnlyFile,LifecycleMailbox,AddonHoldStore,FanDurableFile,DeviceCheckEvidence,BenchmarkCommand,DeadlinePowerPolicyReader,ClusterSetupWizardCommandstdin,SSDBlockChunkCursor.finish) andreadDataToEndOfFilecallsTests
ProviderCoreTests/ChunkedReadMemoryTests(serial shard): each case reads a 256 MiB sparse file through one site on the test's concurrency thread. It checks the known digest (a6d72ac7…06484, SHA-256 of 256 MiB of zeros), and the size where it applies. It also checks that the process footprint grows by less than 64 MiB, likeUpdateFileHashMemoryTests. Cases:readDrained,ManifestBuilder.hashOne,SpecDecStore.digestRegularFile,hashFile(atPath:),makeRuntimeMetallibSnapshot(copied size and digest),SSDBlockStore.readExactly× 256,LiveContext.workerSHA256,MTPBenchmarkDigest.sha256(file:),WorkerCodeIdentity.sha256Hex(of:).DarkbloomCLITests/ChildOutputReadMemoryTests: a real child writes 256 MiB of zeros, marks a file and waits for a release file. The test measures the footprint while the reader's Dispatch block is still running, then checks the exit and the 32 KiB tail. In the first run both cases failed (270 MB and 268 MB held), which found the tail retention. Cases:DesktopWorker,SandboxChildProcess.ProviderCoreTests/Process/BoundedProcessStderrMemoryTests: a real child writes 256 MiB of zeros to stderr, marks a file and waits for a release file. The test measures the footprint while the child still runs, then checks exit 3 and its 4096-byte stderr tail.ManifestBuilder.hashOne/HashedandSpecDecStore.digestRegularFilego fromprivatetointernal.Diagrams
Before:
After:
Validation
No local build or test (laptop rule). CI runs the provider build and tests. Docs: one sentence each in
ssd-kv-cache.md,model-registry-format.md,configuration.md(metallib snapshot),identity-binding.mdandinference.md(worker digest), with stamps refreshed. Changelog:changelog.d/chunked-read-memory.md.Refactor pass: run separately by the coordinator; not done in this PR yet.
🤖 Generated with Claude Code