Skip to content

Commit e9a4d0d

Browse files
committed
Fix concurrency warnings
1 parent 335dc31 commit e9a4d0d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Sources/StreamReader/Conveniences/URLSessionStreamTaskReader.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@ import Foundation
1616
extension URLSessionStreamTask : GenericReadStream {
1717

1818
public func read(_ buffer: UnsafeMutableRawPointer, maxLength len: Int) throws -> Int {
19-
var data: Data?, error: Error?
19+
let readResults = ReadResults()
2020
let group = DispatchGroup()
2121
group.enter()
22-
readData(ofMinLength: 1, maxLength: len, timeout: .infinity, completionHandler: { d, eof, err in
22+
readData(ofMinLength: 1, maxLength: len, timeout: .infinity, completionHandler: { d, atEOF, err in
2323
/* We don’t need the EOF value. */
24-
data = d
25-
error = err
24+
readResults.data = d
25+
readResults.error = err
2626
group.leave()
2727
})
2828
group.wait()
2929
if let e = error {throw e}
30-
data?.withUnsafeBytes{ bytes in buffer.copyMemory(from: bytes.baseAddress!, byteCount: bytes.count) }
31-
return data?.count ?? 0
30+
readResults.data?.withUnsafeBytes{ bytes in buffer.copyMemory(from: bytes.baseAddress!, byteCount: bytes.count) }
31+
return readResults.data?.count ?? 0
32+
}
33+
34+
/* This is not Sendable at all, but we guarantee we’ll use it responsibly. */
35+
private final class ReadResults : @unchecked Sendable {
36+
var data: Data?, error: Error?
3237
}
3338

3439
}

0 commit comments

Comments
 (0)