Skip to content

Commit 6b5f8c9

Browse files
authored
Fix a few more Sendability warnings in Sources (#840)
Motivation: There are a couple of sendability warnings leftover in Sources. Transaction moves a closure into a task. The closure isn't Sendable (and shouldn't be). However, higher up the stack there's a closure which generates the non-sendable closure which can be sendable. Modifications: - Pass the sendable closure generating closure down rather - Add a few more explicit sendable annotations Result: Fewer warnings
1 parent beb2637 commit 6b5f8c9

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest+Prepared.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension HTTPClientRequest {
2424
enum Body {
2525
case asyncSequence(
2626
length: RequestBodyLength,
27-
nextBodyPart: (ByteBufferAllocator) async throws -> ByteBuffer?
27+
makeAsyncIterator: @Sendable () -> ((ByteBufferAllocator) async throws -> ByteBuffer?)
2828
)
2929
case sequence(
3030
length: RequestBodyLength,
@@ -80,7 +80,7 @@ extension HTTPClientRequest.Prepared.Body {
8080
init(_ body: HTTPClientRequest.Body) {
8181
switch body.mode {
8282
case .asyncSequence(let length, let makeAsyncIterator):
83-
self = .asyncSequence(length: length, nextBodyPart: makeAsyncIterator())
83+
self = .asyncSequence(length: length, makeAsyncIterator: makeAsyncIterator)
8484
case .sequence(let length, let canBeConsumedMultipleTimes, let makeCompleteBody):
8585
self = .sequence(
8686
length: length,

Sources/AsyncHTTPClient/AsyncAwait/HTTPClientRequest.swift

+6
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,9 @@ extension HTTPClientRequest.Body {
421421
}
422422
}
423423
}
424+
425+
@available(*, unavailable)
426+
extension HTTPClientRequest.Body.AsyncIterator: Sendable {}
427+
428+
@available(*, unavailable)
429+
extension HTTPClientRequest.Body.AsyncIterator.Storage: Sendable {}

Sources/AsyncHTTPClient/AsyncAwait/Transaction.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ final class Transaction:
7777

7878
private func continueRequestBodyStream(
7979
_ allocator: ByteBufferAllocator,
80-
next: @escaping ((ByteBufferAllocator) async throws -> ByteBuffer?)
80+
makeAsyncIterator: @Sendable @escaping () -> ((ByteBufferAllocator) async throws -> ByteBuffer?)
8181
) {
8282
Task {
83+
let next = makeAsyncIterator()
84+
8385
do {
8486
while let part = try await next(allocator) {
8587
do {
@@ -199,9 +201,9 @@ extension Transaction: HTTPExecutableRequest {
199201

200202
case .startStream(let allocator):
201203
switch self.request.body {
202-
case .asyncSequence(_, let next):
204+
case .asyncSequence(_, let makeAsyncIterator):
203205
// it is safe to call this async here. it dispatches...
204-
self.continueRequestBodyStream(allocator, next: next)
206+
self.continueRequestBodyStream(allocator, makeAsyncIterator: makeAsyncIterator)
205207

206208
case .byteBuffer(let byteBuffer):
207209
self.writeOnceAndOneTimeOnly(byteBuffer: byteBuffer)

Tests/AsyncHTTPClientTests/HTTPClientRequestTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,9 @@ extension Optional where Wrapped == HTTPClientRequest.Prepared.Body {
766766
throw LengthMismatch(announcedLength: announcedLength, actualLength: Int64(buffer.readableBytes))
767767
}
768768
return buffer
769-
case .asyncSequence(length: let announcedLength, let generate):
769+
case .asyncSequence(length: let announcedLength, let makeAsyncIterator):
770770
var accumulatedBuffer = ByteBuffer()
771+
let generate = makeAsyncIterator()
771772
while var buffer = try await generate(ByteBufferAllocator()) {
772773
accumulatedBuffer.writeBuffer(&buffer)
773774
}

0 commit comments

Comments
 (0)