Skip to content

Commit 9777634

Browse files
Fixed an issue where disk file handles for memory cached data were held open indefinitely
1 parent bb6772f commit 9777634

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Sources/CodableDatastore/Persistence/Disk Persistence/Datastore/DatastorePage.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ actor MultiplexedAsyncSequence<Base: AsyncSequence & Sendable>: AsyncSequence wh
168168
typealias Element = Base.Element
169169

170170
private var cachedEntries: [Task<Element?, Error>] = []
171-
private var baseIterator: Base.AsyncIterator
171+
private var baseIterator: Base.AsyncIterator?
172172

173173
struct AsyncIterator: AsyncIteratorProtocol & Sendable {
174174
let base: MultiplexedAsyncSequence
@@ -206,10 +206,11 @@ actor MultiplexedAsyncSequence<Base: AsyncSequence & Sendable>: AsyncSequence wh
206206
}
207207
}
208208

209-
nonisolated func nextBase(iterator: Base.AsyncIterator) async throws -> (Element?, Base.AsyncIterator) {
209+
/// Return the next base iterator to use along with the current entry, or nil if we've reached the end, so we don't retail the open file handles in our memory caches.
210+
nonisolated func nextBase(iterator: Base.AsyncIterator?) async throws -> (Element?, Base.AsyncIterator?) {
210211
var iteratorCopy = iterator
211-
let nextEntry = try await iteratorCopy.next()
212-
return (nextEntry, iteratorCopy)
212+
let nextEntry = try await iteratorCopy?.next()
213+
return (nextEntry, nextEntry.flatMap { _ in iteratorCopy })
213214
}
214215

215216
nonisolated func makeAsyncIterator() -> AsyncIterator {

0 commit comments

Comments
 (0)