Skip to content

Commit

Permalink
OCI blob compression: fix Data memory leak when using InputFilter (#183)
Browse files Browse the repository at this point in the history
* OCI blob compression: fix Data memory leak when using InputFilter

* Rename mappedDiskOffset to mappedDiskReadOffset

* Update progress.completedUnitCount differently
  • Loading branch information
edigaryev authored Aug 17, 2022
1 parent b1be973 commit fea916d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Sources/tart/VMDirectory+OCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ extension VMDirectory {

// Read VM's compressed disk as chunks
// and sequentially upload them as blobs
let disk = try FileHandle(forReadingFrom: diskURL)
var diskReadBytes: UInt64 = 0
let compressingFilter = try InputFilter<Data>(.compress, using: .lz4, bufferCapacity: Self.bufferSizeBytes) { _ in
let data = try disk.read(upToCount: Self.bufferSizeBytes)
diskReadBytes += UInt64(data?.count ?? 0)
let mappedDisk = try Data(contentsOf: diskURL, options: [.alwaysMapped])
let mappedDiskSize = mappedDisk.count
var mappedDiskReadOffset = 0
let compressingFilter = try InputFilter(.compress, using: .lz4, bufferCapacity: Self.bufferSizeBytes) { (length: Int) -> Data? in
let bytesRead = min(length, mappedDiskSize - mappedDiskReadOffset)
let data = mappedDisk.subdata(in: mappedDiskReadOffset ..< mappedDiskReadOffset + bytesRead)
mappedDiskReadOffset += bytesRead

progress.completedUnitCount += Int64(data?.count ?? 0)
progress.completedUnitCount = Int64(mappedDiskReadOffset)

return data
}
Expand All @@ -145,7 +147,7 @@ extension VMDirectory {
let manifest = OCIManifest(
config: OCIManifestConfig(size: ociConfigJSON.count, digest: ociConfigDigest),
layers: layers,
uncompressedDiskSize: diskReadBytes
uncompressedDiskSize: UInt64(mappedDiskReadOffset)
)

// Manifest
Expand Down

0 comments on commit fea916d

Please sign in to comment.